php利用mcrypt.so扩展,如何解密AES-CBC模式数据?
本文测试环境:mac/php72/mcrypt。值得注意的是:mcrypt.so中的大量函数,已经是被php官方废弃的状态。在php73中,已经全面不支持mcrypt系列函数了。所以,本文的应用范围是有限的。对于php7以上如果加载了mcrypt.so后,再进行使用相关mcrypt系列函数的话。有可能会有下面的警告提示信息:
Deprecated: Function mcrypt_decrypt() is deprecated in /code/test.php on line 22
PHP
Copy
所以,在本文的范例中,大量使用了@操作符来抑制警告信息的输出。如果您想核对一下结果的话,本文的解密结果都是文本"苏南大叔"。
安装mcrypt
本文中的代码如果要顺利执行,是需要个mcrypt.so扩展的。所以,请确定您的php开启了mcrypt.so扩展。下面的两篇文章,是演示如何编译安装mcrypt扩展的。
centos系统:https://newsn.net/say/centos-php-mcrypt.html
mac系统:https://newsn.net/say/mac-php-mcrypt.html
解密方案一(mdecrypt_generic)(推荐)
基本数据定义,这里定义个要解密的数据,还有对应的key和iv。
$iv = "sunandebokeyuming";
$key = "newsn.net";
$str = "CS+GUTtR9BU+dJypghhSvw==";
解密代码:
$module = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $iv);
@mcrypt_generic_init($module, $key, $iv);
$encryptedData = base64_decode($str);
$encryptedData = @mdecrypt_generic($module, $encryptedData);
print_r($encryptedData);
解密方案二(mcrypt_decrypt)
基础数据:
$iv = "sunandebokeyumin";
$key = "newsn.netnewsn.net123456";
$str = "O2ye5urKS7etgzahkeYxOg==";
解密方案:
$decrypted = @mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$key,
base64_decode($str),
MCRYPT_MODE_CBC,
$iv
);
echo $decrypted;
使用mcrypt_decrypt的时候,对于$key和$iv长度都有限制。否则,您就可能会看到如下提示:
mcrypt_decrypt(): Key of size 9 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported
mcrypt_decrypt(): Received initialization vector of size 19, but size 16 is required for this encryption mode in /home/wwwroot/d9fx.com/index.php on line 22
相关推荐
标签:
留言与评论(共有 0 条评论) |