Home > Backend Development > PHP Tutorial > Introduction to how to enable, encrypt and decrypt php mcrypt

Introduction to how to enable, encrypt and decrypt php mcrypt

WBOY
Release: 2016-07-25 09:00:01
Original
1043 people have browsed it
  1. $str = "I am Li Yun";
  2. $key = "123qwe.019860905061X";
  3. $cipher = MCRYPT_RIJNDAEL_128;
  4. $mode = MCRYPT_MODE_ECB;
  5. $iv = mcrypt_creat e_iv(mcrypt_get_iv_size( $cipher,$mode),MCRYPT_RAND);
  6. echo "Original text:".$str."
    ";
  7. $str_encrypt = mcrypt_encrypt($cipher,$key,$str,$mode,$iv);
  8. echo "The encrypted content is: ".$str_encrypt."
    ";
  9. $str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$mode,$iv);
  10. echo "After decryption Content: ".$str_decrypt."
    ";
  11. ?>
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:

  1. //Specify the size of the initialization vector iv:
  2. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  3. //Create the initialization vector:
  4. $iv = mcrypt_create_iv($iv_size, MCRY PT_RAND);
  5. //Encrypted password:
  6. $key = "123qwe.019860905061x";
  7. //Original content (unencrypted):
  8. $text = "My name is Adam Li!";
  9. echo $text. "
    n ";
  10. //Encrypted content:
  11. $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
  12. echo $crypttext. "n
    ";
  13. //Decrypt the encrypted content :
  14. $str_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
  15. echo $str_decrypt;
  16. //Original link http://bbs.it-home.org/article/7382.html
  17. ? >
Copy code

Example of encryption/decryption request:

  1. $request_params = array(
  2. 'controller' => 'todo',
  3. 'action' => 'read',
  4. 'username' => "bl",
  5. 'userpass' => "a1"
  6. );
  7. $private_key = "28e336ac6c9423d946ba02d19c6a2632";
  8. //encrypt request
  9. $enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $ private_key, json_encode($request_params), MCRYPT_MODE_ECB));
  10. echo "CRYPT:".$enc_request."
    ";
  11. //decrypt request
  12. $params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $private_key, base64_decode($enc_request), MCRYPT_MODE_ECB )), true);
  13. echo "ENCRYPT:
    ";
  14. //print result
  15. var_dump($params);
  16. ?>
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.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template