Analysis of PHP mcrypt Reversible Encryption Algorithm

Source: Internet
Author: User
Tags mcrypt md5 hash types of functions

Data Encryption has become more and more important in our lives, especially considering the large amount of data that has been traded and transmitted over the Internet. For information that does not need to be restored to the original data, we can use MD5, sha1 and other irreversible encryption algorithms to encrypt the data, however, you must use a recoverable encryption algorithm to encrypt important information such as transaction information that needs to be restored to the original data. Of course, you can write a Reversible Encryption Algorithm for encryption and decryption computing. This article describes how to use the mcrypt module for encryption and decryption.
The advantage of Mcrypt is not only that it provides many encryption algorithms, but also that it is released along with the PHP package in windows, but also that it can encrypt/decrypt data. In addition, it also provides 35 types of functions for data processing, including the DES algorithm. Copy codeThe Code is as follows :/**
+ -----------------------------------------------------
* Mcrypt encryption/Decryption
* @ Param String $ date refers to the data to be encrypted and decrypted.
* @ Param String $ mode encode Encryption By default/decode decryption
* @ Return String
* @ Author zxing@97md.net Mon Sep 14 22:59:28 CST 2009
+ -----------------------------------------------------
* @ Example
*/
Function ZxingCrypt ($ date, $ mode = 'encoding '){
$ Key = md5 ('zx'); // use MD5 hash to generate a key. Note that the encryption and decryption keys must be consistent.
If ($ mode = 'decode '){
$ Date = base64_decode ($ date );
}
If (function_exists ('mcrypt _ create_iv ')){
$ Iv_size = mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
$ Iv = mcrypt_create_iv ($ iv_size, MCRYPT_RAND );
}
If (isset ($ iv) & $ mode = 'encoding '){
$ Passcrypt = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ date, MCRYPT_MODE_ECB, $ iv );
} Elseif (isset ($ iv) & $ mode = 'decode '){
$ Passcrypt = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ key, $ date, MCRYPT_MODE_ECB, $ iv );
}
If ($ mode = 'encoding '){
$ Passcrypt = base64_encode ($ passcrypt );
}
Return $ passcrypt;
}

Code of other netizensCopy codeThe Code is as follows: <? Php
$ Td = mcrypt_module_open (MCRYPT_DES, '', 'ecb ',''); // use the MCRYPT_DES Algorithm in ecb mode.
$ Iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($ td), MCRYPT_RAND );
$ Ks = mcrypt_enc_get_key_size ($ td );
$ Key = "ery secret key"; // key
$ Key = substr (md5 ($ key), 0, $ ks );
Mcrypt_generic_init ($ td, $ key, $ iv); // Initial Processing
// Encryption
$ Encrypted = mcrypt_generic ($ td, 'this is very important data ');
// End Processing
Mcrypt_generic_deinit ($ td );
// Initial decryption
Mcrypt_generic_init ($ td, $ key, $ iv );
// Decrypt
$ Decrypted = mdecrypt_generic ($ td, $ encrypted );
// End
Mcrypt_generic_deinit ($ td );
Mcrypt_module_close ($ td );
// After decryption, \ 0 may be removed.
Echo trim ($ decrypted). "\ n ";
?>

Related Article

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.