Home >Backend Development >PHP Tutorial >Php Aes encryption program code sharing_PHP tutorial

Php Aes encryption program code sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:47:451131browse

Today I had nothing to do with a Php Aes encryption program. It is an extension suitable for Yii. If it is not used in the Yii framework, replace Yii::app()->params['encryptKey'] in the code with your corresponding default key. That's it.

AES Encryption Algorithm – Algorithm Principle

The AES algorithm is based on permutation and permutation operations. Permutation is the rearrangement of data, and permutation is the replacement of one data unit with another. AES uses several different methods to perform permutation and permutation operations.
AES is an iterative, symmetric key-blocked cipher that can use 128-, 192-, and 256-bit keys and encrypt and decrypt data in 128-bit (16-byte) blocks. Unlike public key ciphers which use key pairs, symmetric key ciphers use the same key to encrypt and decrypt data. The encrypted data returned by the block cipher has the same number of bits as the input data. Iterative encryption uses a loop structure in which the input data is repeatedly permuted and replaced.

The code is as follows Copy code

/**
*php AES encryption and decryption class
* Because Java only supports 128-bit encryption, PHP also uses 128-bit encryption and can be converted to Java.
* At the same time, the AES standard is also 128 bits. Only the RIJNDAEL algorithm can support 128, 192 and 256-bit encryption.
*
* @author Terry
*
*/
class PhpAes
{
/**
* This was AES-128 / CBC / ZeroBytePadding encrypted.
* return base64_encode string
* @author Terry
* @param string $plaintext
* @param string $key
*/
public static function AesEncrypt($plaintext,$key = null)
{
if ($plaintext == '') return '';
if(!extension_loaded('mcrypt'))
throw new CException(Yii::t('yii','AesEncrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$plaintext = self::PKCS5Padding($plaintext, $size);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));
/* Create the IV and determine the keysize length, use MCRYPT_RAND
* on Windows instead */
$iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
/* Initialize encryption */
mcrypt_generic_init($module, $key, $iv);

/* Encrypt data */
$encrypted = mcrypt_generic($module, $plaintext);

/* Terminate encryption handler */
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return base64_encode(trim($encrypted));
}

/**
* This was AES-128 / CBC / ZeroBytePadding decrypted.
* @author Terry
* @param string $encrypted base64_encode encrypted string
* @param string $key
* @throws CException
* @return string
*/
public static function AesDecrypt($encrypted, $key = null)
{
if ($encrypted == '') return '';
if(!extension_loaded('mcrypt'))
throw new CException(Yii::t('yii','AesDecrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));

$ciphertext_dec = base64_decode($encrypted);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));

$iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));

/* Initialize encryption module for decryption */
mcrypt_generic_init($module, $key, $iv);

/* Decrypt encrypted string */
$decrypted = mdecrypt_generic($module, $ciphertext_dec);

/* Terminate decryption handle and close module */
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return self::UnPKCS5Padding($decrypted);
}


private static function strlen($string)
{
return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
}

private static function substr($string,$start,$length)
{
return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
}

private static function PKCS5Padding ($text, $blocksize) {
$pad = $blocksize - (self::strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}

private static function UnPKCS5Padding($text)
{
$pad = ord($text{self::strlen($text)-1});
if ($pad > self::strlen($text)) return false;
if (strspn($text, chr($pad), self::strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}

How to use

The code is as follows
 代码如下 复制代码

require_once('./AES.php');
//$aes = new AES();
$aes = new AES(true);// 把加密后的字符串按十六进制进行存储
//$aes = new AES(true,true);// 带有调试信息且加密字符串按十六进制存储
$key = "this is a 32 byte key";// 密钥
$keys = $aes->makeKey($key);
$encode = "123456";// 被加密的字符串
$ct = $aes->encryptString($encode, $keys);
echo "encode = ".$ct."
";
$cpt = $aes->decryptString($ct, $keys);
echo "decode = ".$cpt;
?>

Copy code
require_once('./AES.php');

//$aes = new AES();

$aes = new AES(true); // Store the encrypted string in hexadecimal $key = "this is a 32 byte key";//Key $keys = $aes->makeKey($key); $encode = "123456"; // Encrypted string $ct = $aes->encryptString($encode, $keys); echo "encode = ".$ct."
"; $cpt = $aes->decryptString($ct, $keys); echo "decode = ".$cpt; ?>
http://www.bkjia.com/PHPjc/632843.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632843.htmlTechArticleToday I have nothing to do with a Php Aes encryption program, which is suitable for Yii extensions. If it is not used in the Yii framework, put In the code, just replace Yii::app()->params['encryptKey'] with your corresponding default key...
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