Home  >  Article  >  Backend Development  >  PHP mcrypt() reversible encryption algorithm analysis

PHP mcrypt() reversible encryption algorithm analysis

怪我咯
怪我咯Original
2017-07-23 10:55:181666browse

Data encryption has become more and more important in our lives, especially considering the large number of transactions that occur and the large amounts of data transmitted over the Internet. For information that does not need to be restored to the original data, we can use irreversible encryption algorithms such as MD5 and SHA1 to encrypt the data. However, important information such as transaction information that needs to be restored to the original data must be encrypted using a restoreable encryption algorithm. Of course, you can write a reversible encryption algorithm yourself to perform encryption and decryption calculations. In this article we introduce the use of the mcrypt module for encryption and decryption operations.

The advantage of Mcrypt is not only that it provides many encryption algorithms and is released with the PHP package under Windows, but also that it can encrypt/decrypt data. In addition, it It also provides 35 functions for processing data including DES algorithm.

/** 
+----------------------------------------------------- 
* Mcrypt 加密/解密 
* @param String $date 要加密和解密的数据 
* @param String $mode encode 默认为加密/decode 为解密 
* @return String 
* @author zxing@97md.net Mon Sep 14 22:59:28 CST 2009 
+----------------------------------------------------- 
* @example 
*/ 
function ZxingCrypt($date,$mode = 'encode'){ 
$key = md5('zxing');//用MD5哈希生成一个密钥,注意加密和解密的密钥必须统一 
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; 
}


The above is the detailed content of PHP mcrypt() reversible encryption algorithm analysis. For more information, please follow other related articles on the PHP Chinese website!

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