【发布时间】:2017-07-12 08:56:27
【问题描述】:
我已将我的 php 版本更新到 7.1。 我有使用 mcrypt 加密数据的功能。 现在此功能已弃用。
我怎样才能在不回到旧版本的 php 的情况下解密数据。
这是我使用的代码:
public function encrypt($plaintext) {
$ivSize = mcrypt_get_iv_size(self::CIPHER, self::MODE);
$iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM);
$ciphertext = mcrypt_encrypt(self::CIPHER, $this->key, $plaintext, self::MODE, $iv);
return base64_encode($iv.$ciphertext);
}
public function decrypt($ciphertext) {
$ciphertext = base64_decode($ciphertext);
$ivSize = mcrypt_get_iv_size(self::CIPHER, self::MODE);
if (strlen($ciphertext) < $ivSize) {
throw new Exception('Missing initialization vector');
}
$iv = substr($ciphertext, 0, $ivSize);
$ciphertext = substr($ciphertext, $ivSize);
$plaintext = mcrypt_decrypt(self::CIPHER, $this->key, $ciphertext, self::MODE, $iv);
return rtrim($plaintext, "\0");
}
使用常量:
const CIPHER = MCRYPT_RIJNDAEL_128; // Rijndael-128 is AES
const MODE = MCRYPT_MODE_CBC;
我看到建议使用 OpenSSL。这就是我从现在开始使用的。但是如何使用这种方法解密旧数据呢?
谢谢
编辑: 我知道我可以使用 OpenSSL 作为替代方案。 这就是我从现在开始为内容所做的事情。 但我需要从我的旧内容中解密我的 mcrypted 代码。
*编辑请求@symcbean
尝试像这样使用 OpenSSL 解密:
public function decrypt($ciphertext) {
$ciphertext = base64_decode($ciphertext);
if (!function_exists("openssl_decrypt")) {
throw new Exception("aesDecrypt needs openssl php module.");
}
$key = $this->key;
$method = 'AES-256-CBC';
$ivSize = openssl_cipher_iv_length($method);
$iv = substr($ciphertext,0,$ivSize);
$data = substr($ciphertext,$ivSize);
$clear = openssl_decrypt ($data, $method, $key, 'OPENSSL_RAW_DATA'|'OPENSSL_ZERO_PADDING', $iv);
return $clear;
}
【问题讨论】:
-
不是。我不需要替代品。我知道我可以使用 openssl。但我需要解密我的 mcrypted 内容。
-
@Ben:不同意 - 这是关于密码的讨论,接受的答案没有解决解密的具体问题。
-
你试过用openssl解密mcrypt密文吗?你的代码在哪里?发生了什么?
-
如果您以前使用过
MCRYPT_RIJNDAEL_128,我认为您的$method应该是"AES-128-CBC"。openssl_decrypt标志也是常量,而不是字符串,因此请删除OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING之类的引号。
标签: php deprecated mcrypt php-openssl