Share a standard PHP AES encryption algorithm class, where Mcrypt_get_block_size (' rijndael-128 ', ' ECB '), if it is easier to get it wrong without understanding the principle, you can pass Mcrypt_list_ The algorithms function looks at the encryption algorithm ID you need.
Copy Code code as follows:
<?php
/**
* AES128 encryption and decryption class
* @author dy
*
*/
Defined (' Inejbuy ') or exit (' Access invalid! ');
Class aes{
Secret key
Private $_secrect_key;
Public Function __construct () {
$this->_secrect_key = ' MYGGNQE2JDFADSFFDSEWSDD ';
}
/**
* Encryption method
* @param string $str
* @return String
*/
Public function Encrypt ($STR) {
AES, 128 ECB mode encrypted data
$screct _key = $this->_secrect_key;
$screct _key = Base64_decode ($screct _key);
$str = Trim ($STR);
$str = $this->addpkcs7padding ($STR);
$iv = Mcrypt_create_iv (Mcrypt_get_iv_size (MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB), Mcrypt_rand);
$encrypt _str = Mcrypt_encrypt (mcrypt_rijndael_128, $screct _key, $str, MCRYPT_MODE_ECB, $IV);
Return Base64_encode ($encrypt _str);
}
/**
* Decryption method
* @param string $str
* @return String
*/
Public function Decrypt ($STR) {
AES, 128 ECB mode encrypted data
$screct _key = $this->_secrect_key;
$str = Base64_decode ($STR);
$screct _key = Base64_decode ($screct _key);
$iv = Mcrypt_create_iv (Mcrypt_get_iv_size (MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB), Mcrypt_rand);
$encrypt _str = Mcrypt_decrypt (mcrypt_rijndael_128, $screct _key, $str, MCRYPT_MODE_ECB, $IV);
$encrypt _str = Trim ($encrypt _str);
$encrypt _str = $this->strippksc7padding ($encrypt _str);
return $encrypt _str;
}
/**
* Fill algorithm
* @param string $source
* @return String
*/
function addpkcs7padding ($source) {
$source = Trim ($source);
$block = mcrypt_get_block_size (' rijndael-128 ', ' ECB ');
$pad = $block-(strlen ($source)% $block);
if ($pad <= $block) {
$char = Chr ($pad);
$source. = Str_repeat ($char, $pad);
}
return $source;
}
/**
* Remove Fill algorithm
* @param string $source
* @return String
*/
function strippksc7padding ($source) {
$source = Trim ($source);
$char = substr ($source,-1);
$num = Ord ($char);
if ($num ==62) return $source;
$source = substr ($source, 0,-$num);
return $source;
}
}
The above is the entire content described in this article, I hope that you learn PHP AES encryption algorithm classes help.