PHP MCrypt reversible encryption algorithm analysis _php skills

Source: Internet
Author: User
Tags decrypt mcrypt
The role of data encryption in our lives has become increasingly important, especially given the large number of transactions and data transmissions that occur on the web. For information that does not need to be restored to the original data, we can encrypt the data using MD5, SHA1 and other irreversible encryption algorithms, but important information such as transaction information that needs to be restored to the original data must be encrypted using a reversible encryption algorithm. Of course you can write a reversible encryption algorithm to encrypt and decrypt the calculation. In this article we describe the use of the MCrypt module for encryption and decryption operations.
The advantage of MCrypt is not only that it provides more encryption algorithms, is published with the PHP package under Windows, but that it can add/decrypt data, and it also provides 35 functions for processing data, including the DES algorithm.
Copy Code code as follows:

/**
+-----------------------------------------------------
* Mcrypt Encryption/decryption
* @param String $date data to encrypt and decrypt
* @param String $mode encode default to encryption/decode for decryption
* @return String
* @author zxing@97md.net Mon Sep 22:59:28 CST 2009
+-----------------------------------------------------
* @example
*/
function Zxingcrypt ($date, $mode = ' encode ') {
$key = MD5 (' zxing ');//MD5 Hashishen into a key, note that the encryption and decryption key must be unified
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 = = ' encode ') {
$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 = = ' encode ') {
$passcrypt = Base64_encode ($passcrypt);
}
return $passcrypt;
}

Other user's Code
Copy Code code as follows:

<?php
$td = Mcrypt_module_open (Mcrypt_des, ', ' ECB ', ');//Use MCrypt _des algorithm, 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 processing
Mcrypt_generic_init ($TD, $key, $IV);
//decryption
$decrypted = Mdecrypt_generic ($TD, $encrypted);
//End
Mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
//After decryption, there may be subsequent, and need to remove
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.