-
- $str = "I am Li Yun";
- $key = "123qwe.019860905061X";
- $cipher = MCRYPT_RIJNDAEL_128;
- $mode = MCRYPT_MODE_ECB;
- $iv = mcrypt_creat e_iv(mcrypt_get_iv_size( $cipher,$mode),MCRYPT_RAND);
-
- echo "Original text:".$str."
";
- $str_encrypt = mcrypt_encrypt($cipher,$key,$str,$mode,$iv);
- echo "The encrypted content is: ".$str_encrypt."
";
-
- $str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$mode,$iv);
-
- echo "After decryption Content: ".$str_decrypt."
";
- ?>
Copy the code
Run the results:
Original text: I am Li Yun
The encrypted content is: B @鴹?=(I褣Z%
Decrypted content: I am Li Yun
1) Before using the PHP encryption extension library Mcrypt to encrypt and decrypt data, first create an initialization vector, referred to as iv for short. From $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND); it can be seen that creating an initialization vector requires two parameters: size specifies the size of iv; source is the source of iv, where the value MCRYPT_RAND is the system random number.
2) The function mcrypt_get_iv_size($cipher,$modes) returns the initialization vector size. The parameters cipher and mode refer to the algorithm and encryption mode respectively.
3), encryption function $str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv); The five parameters of this function are as follows: cipher——encryption algorithm, key——key, data( str)——data that needs to be encrypted, mode——algorithm mode, iv——initialization vector
4), decryption function mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv); The parameters of this function and the encryption function are almost the same. The only difference is data, which means data is the data that needs to be decrypted$ str_encrypt, not the raw data $str.
//Writing in the manual:
-
-
- //Specify the size of the initialization vector iv:
- $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
- //Create the initialization vector:
- $iv = mcrypt_create_iv($iv_size, MCRY PT_RAND);
- //Encrypted password:
- $key = "123qwe.019860905061x";
- //Original content (unencrypted):
- $text = "My name is Adam Li!";
- echo $text. "
n ";
- //Encrypted content:
- $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
- echo $crypttext. "n
";
- //Decrypt the encrypted content :
- $str_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
- echo $str_decrypt;
- //Original link http://bbs.it-home.org/article/7382.html
- ? >
Copy code
Example of encryption/decryption request:
-
- $request_params = array(
- 'controller' => 'todo',
- 'action' => 'read',
- 'username' => "bl",
- 'userpass' => "a1"
- );
-
- $private_key = "28e336ac6c9423d946ba02d19c6a2632";
-
- //encrypt request
- $enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $ private_key, json_encode($request_params), MCRYPT_MODE_ECB));
- echo "CRYPT:".$enc_request."
";
-
- //decrypt request
- $params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $private_key, base64_decode($enc_request), MCRYPT_MODE_ECB )), true);
- echo "ENCRYPT:
";
-
- //print result
- var_dump($params);
- ?>
Copy code
Note:
The parameters cipher, key and mode in the encryption and decryption functions must correspond one to one, otherwise the data cannot be restored.
|