This article mainly introduces the aes Encryption Class implemented by php. There are some usage methods in the Code. For more information, see
This article mainly introduces the aes Encryption Class implemented by php. There are some usage methods in the Code. For more information, see
The Code is as follows:
Class AESMcrypt {
Public $ iv = null;
Public $ key = null;
Public $ bit = 128;
Private $ cipher;
Public function _ construct ($ bit, $ key, $ iv, $ mode ){
If (empty ($ bit) | empty ($ key) | empty ($ iv) | empty ($ mode ))
Return NULL;
$ This-> bit = $ bit;
$ This-> key = $ key;
$ This-> iv = $ iv;
$ This-> mode = $ mode;
Switch ($ this-> bit ){
Case 192: $ this-> cipher = MCRYPT_RIJNDAEL_192; break;
Case 256: $ this-> cipher = MCRYPT_RIJNDAEL_256; break;
Default: $ this-> cipher = MCRYPT_RIJNDAEL_128;
}
Switch ($ this-> mode ){
Case 'ecb ': $ this-> mode = MCRYPT_MODE_ECB; break;
Case 'cfb ': $ this-> mode = MCRYPT_MODE_CFB; break;
Case 'ofb': $ this-> mode = MCRYPT_MODE_OFB; break;
Case 'nofb ': $ this-> mode = MCRYPT_MODE_NOFB; break;
Default: $ this-> mode = MCRYPT_MODE_CBC;
}
}
Public function encrypt ($ data ){
$ Data = base64_encode (mcrypt_encrypt ($ this-> cipher, $ this-> key, $ data, $ this-> mode, $ this-> iv ));
Return $ data;
}
Public function decrypt ($ data ){
$ Data = mcrypt_decrypt ($ this-> cipher, $ this-> key, base64_decode ($ data), $ this-> mode, $ this-> iv );
$ Data = rtrim ($ data), "\ x00.. \ x1F ");
Return $ data;
}
}
// Usage
$ Aes = new AESMcrypt ($ bit = 128, $ key = 'abcdef1234567890', $ iv = '0987654321cba CBA ', $ mode = 'cbc ');
$ C = $ aes-> encrypt ('haowei. me ');
Var_dump ($ aes-> decrypt ($ c ));
,