Php implements aes encryption class sharing. Copy the code as follows :? PhpclassAESMcrypt {public $ ivnull; public $ keynull; public $ bit128; private $ cipher; publicfunction _ construct ($ bit, $ key, $ iv, $ mode) {I
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 ));
The http://www.bkjia.com/PHPjc/825242.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825242.htmlTechArticle code is as follows :? Php class AESMcrypt {public $ iv = null; public $ key = null; public $ bit = 128; private $ cipher; public function _ construct ($ bit, $ key, $ iv, $ mode) {I...