How to encrypt/decrypt mcrypt?

Source: Internet
Author: User
Tags mcrypt
Check the Phpinfo () page to see which version of your libmcrypt is. if it is 2.4.0 or later, you cannot use your method. In addition, libmcrypt's supported encryption algorithms depend on different libmcrypt versions. The following is the 2.4.0 + encryption and decryption method ~ It is also an example of PHP code in the manual: functionmake_seed () {view the Phpinfo () page to see which version of your libmcrypt is. if it is later than 2.4.0, you cannot use your method.

In addition, libmcrypt's supported encryption algorithms depend on different libmcrypt versions.

The following is the 2.4.0 + encryption and decryption method ~

It is also an example in the manual.

PHP code:
  
Function make_seed (){
List ($ usec, $ sec) = explode ('', microtime ());
Return (float) $ sec + (float) $ usec * 100000 );
}
Srand (make_seed ());

/* Enable the encryption algorithm /*/
$ Td = mcrypt_module_open ('twofish ', '', 'ECB ','');

/* Create an IV and check the key length */
$ Iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($ td), MCRYPT_RAND );
$ Ks = mcrypt_enc_get_key_size ($ td );

/* Generate the key */
$ Key = substr (md5 ('very secret key'), 0, $ ks );

/* Initialize the encryption program */
Mcrypt_generic_init ($ td, $ key, $ iv );

/* Encrypted, $ encrypted stores encrypted data */
Print $ encrypted = mcrypt_generic ($ td, 'This is very important data ');

/* Check the encryption handle */
Mcrypt_generic_deinit ($ td );

/* Initialize the encryption module for decryption */
Mcrypt_generic_init ($ td, $ key, $ iv );

/* Decrypt */
$ Decrypted = mdecrypt_generic ($ td, $ encrypted );

/* Check the decryption handle and close the module */
Mcrypt_generic_deinit ($ td );
Mcrypt_module_close ($ td );

/* Display the original string */
Echo trim ($ decrypted). "\ n ";

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.