Introduction to PHP MCrypt Enable, encrypt, and decrypt methods

Source: Internet
Author: User
    1. $str = "I am Levin";
    2. $key = "123qwe.019860905061x";
    3. $cipher = mcrypt_rijndael_128;
    4. $mode = MCRYPT_MODE_ECB;
    5. $iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $mode), Mcrypt_rand);
    6. echo "Original:" $str. "
      ";
    7. $str _encrypt = Mcrypt_encrypt ($cipher, $key, $str, $mode, $IV);
    8. echo "Encrypted content is:". $str _encrypt. "
      ";
    9. $str _decrypt = Mcrypt_decrypt ($cipher, $key, $str _encrypt, $mode, $IV);
    10. echo "Decrypted content:" $str _decrypt. "
      ";
    11. ?>
Copy Code

Operation 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:

    1. Specify the size of the initialization vector IV:
    2. $iv _size = mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);
    3. To create an initialization vector:
    4. $iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
    5. Encrypt password:
    6. $key = "123qwe.019860905061x";
    7. Original content (unencrypted):
    8. $text = "My name is Adam li!";
    9. Echo $text. "
      \ n ";
    10. After the encrypted content:
    11. $crypttext = Mcrypt_encrypt (mcrypt_rijndael_256, $key, $text, MCRYPT_MODE_ECB, $IV);
    12. Echo $crypttext. "\ n
      ";
    13. To decrypt something that is already encrypted:
    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

Examples of Add/Decrypt requests:

    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 in the encryption and decryption functions cipher, key, and mode must correspond to one by one, otherwise the data cannot be restored.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.