PHP 有两种方式调用AES加解密,一种是使用mcrypt库,示例如下:
_secret_key = $key; } public function encode($data) { $td = mcrypt_module_open(MCRYPT_RIJNDAEL_256,'',MCRYPT_MODE_CBC,''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); mcrypt_generic_init($td,$this->_secret_key,$iv); $encrypted = mcrypt_generic($td,$data); mcrypt_generic_deinit($td); return $iv . $encrypted; } public function decode($data) { $td = mcrypt_module_open(MCRYPT_RIJNDAEL_256,'',MCRYPT_MODE_CBC,''); $iv = mb_substr($data,0,32,'latin1'); mcrypt_generic_init($td,$this->_secret_key,$iv); $data = mb_substr($data,32,mb_strlen($data,'latin1'),'latin1'); $data = mdecrypt_generic($td,$data); mcrypt_generic_deinit($td); mcrypt_module_close($td); return trim($data); }} $aes = new aes();$aes->setKey('key'); // 加密$string = $aes->encode('string');// 解密$aes->decode($string);?>
还有一种是使用openssl,示例如下:
测试运行效果如下:
sc7ubaY39/e3gxJCRqfn9jJWJkeH1pZHMlZW5/Cw==ecuyhasd8ubcY39/ezEkJDo7OCU5OjIlODslPDE6Mzs7OiR9aWRzYg==ecuayn
来源地址:https://blog.csdn.net/appMaster/article/details/131691575