- $str = "I am Levin";
- $key = "123qwe.019860905061x";
- $cipher = mcrypt_rijndael_128;
- $mode = MCRYPT_MODE_ECB;
- $iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $mode), Mcrypt_rand);
- echo "Original:" $str. "
";
- $str _encrypt = Mcrypt_encrypt ($cipher, $key, $str, $mode, $IV);
- echo "Encrypted content is:". $str _encrypt. "
";
- $str _decrypt = Mcrypt_decrypt ($cipher, $key, $str _encrypt, $mode, $IV);
- echo "Decrypted content:" $str _decrypt. "
";
- ?>
Copy CodeOperation Result: Original: I am Levin encrypted content is: b@ 鴹? = (I argue 蝣 z% decrypted content: I'm Levin. 1), using PHP encryption extension library MCrypt to encrypt and decrypt data, first create an initialization vector, referred to as IV. by $iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand); the creation of an initialization vector requires two parameters: size Specifies the magnitude of IV; Source is the origin of IV , where the value Mcrypt_rand is the system random number. 2), function mcrypt_get_iv_size ($cipher, $modes) returns the initialization vector size, parameter cipher and mode respectively refer to the algorithm and encryption mode. 3), cryptographic function $str_encrypt = Mcrypt_encrypt ($cipher, $key, $str, $modes, $IV); The 5 parameters of the function are as follows: cipher--encryption algorithm, key--key, Data (str)--required to encrypt, mode--algorithm mode, iv--initialization vector 4), decryption function Mcrypt_decrypt ($cipher, $key, $str _encrypt, $modes, $IV); The function is almost the same as the parameters of the cryptographic function, except that the data is the only one that is $str_encrypt, rather than the raw data $str, which is to be decrypted. The wording in the handbook:
- Specify the size of the initialization vector IV:
- $iv _size = mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);
- To create an initialization vector:
- $iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
- Encrypt password:
- $key = "123qwe.019860905061x";
- Original content (unencrypted):
- $text = "My name is Adam li!";
- Echo $text. "
\ n ";
- After the encrypted content:
- $crypttext = Mcrypt_encrypt (mcrypt_rijndael_256, $key, $text, MCRYPT_MODE_ECB, $IV);
- Echo $crypttext. "\ n
";
- To decrypt something that is already encrypted:
- $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 CodeExamples of Add/Decrypt requests:
- $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 CodeNote: The parameters in the encryption and decryption functions cipher, key, and mode must correspond to one by one, otherwise the data cannot be restored. |