2012-07-20 106 views
-1
public static function getEncryptedData($value){ 
     if(!$value){return false;} 
     $text = $value; 
     $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); 
     $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); 
     $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, _PR_ACCOUNT_ACTIVATION_SECURE_KEY_, $text, MCRYPT_MODE_ECB, $iv); 
     return trim(base64_encode($crypttext)); //encode for cookie 
    } 

我在PHP中遇到了上述代碼。
我需要了解:
1.它在做什麼?
2.如何在Java中使用Apache Shiro做同樣的事情?PHP加密代碼解釋

+0

它是做什麼部分是不敏感的:它產生IV的模式(ECB)不使用的IV – 2012-07-21 14:12:26

回答

1
/*Sets the function with the ability to be called globally without instantiating the object. Take one value into method through classname::getEncryptedData($value)*/ 
public static function getEncryptedData($value){ 
    /*Checks to see if value is populated and an erroneous value hasn't been passed in such as null returns false if it has*/ 
    if(!$value){return false;} 
/* instantiated a local variable called text and populate it with the value $value*/ 
    $text = $value; 
/*as per the docs http://php.net/manual/en/function.mcrypt-get-iv-size.php gets the size of the iv and sets it in the var $iv_size*/ 
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); 
/*Creates an initialization vector as per http://uk3.php.net/manual/en/function.mcrypt-create-iv.php*/ 
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); 
/*Encrypt the data $text(from $value passed it) and store it in $crypttext */ 
    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, _PR_ACCOUNT_ACTIVATION_SECURE_KEY_, $text, MCRYPT_MODE_ECB, $iv); 
/*return a base64 encoded string with the whitespace trimmed from front and back*/ 
    return trim(base64_encode($crypttext)); //encode for cookie 
} 

至於如何做到這一點在Java中我不知道:-(

+0

。 Downvoted for what?Anon downvoting是最糟糕的SO特性,如果沒有解釋爲什麼你低估,怎麼能改善任何事情! – 2012-07-23 10:30:22