AES is a group key. the algorithm inputs 128-bit data and the key length is also 128-bit. The number of rounds encrypted by a data group using Nr (the relationship between the number of encryption rounds and the key length is shown in Table 1 ). Each round requires an extension key Expandedkey (I) parameter with the same length as the input group to share a standard PHP AES encryption algorithm class, in which mcrypt_get_block_size ('rijndael-100 ', 'ECB '); if you do not understand the principle, you can use the mcrypt_list_algorithms function to view the ID of the encryption algorithm you need.
The code is as follows:
<? Php
/**
* AES128 encryption/decryption class
* @ Author dy
*
*/
Defined ('ejbuy') or exit ('Access Invalid! ');
Class Aes {
// 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 data encryption
$ 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 data encryption
$ 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;
}
/**
* Filling algorithm
* @ Param string $ source
* @ Return string
*/
Function addPKCS7Padding ($ source ){
$ Source = trim ($ source );
$ Block = mcrypt_get_block_size ('rijndael-100', 'ECB ');
$ Pad = $ block-(strlen ($ source) % $ block );
If ($ pad <= $ block ){
$ Char = chr ($ pad );
$ Source. = str_repeat ($ char, $ pad );
}
Return $ source;
}
/**
* Removing the padding 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 all the content described in this article. I hope it will help you learn the AES encryption algorithm class of php.