将我的加密库从Mcrypt升级到OpenSSL
我使用下面常用的库进行加密。我想从Mcrypt将其升级到OpenSSL,以便我不再使用已弃用的库。
我试图找出这是否可能。我对此做了一些研究,但我发现了相互矛盾的信息:
这篇文章说,使用用Mcrypt加密的OpenSSL无法解密数据。https://stackoverflow.com/a/19748494/5834657
但是,这篇文章说可以使用填充。看来我的函数使用了填充。这是否是使这项工作所需的正确填充类型? https://stackoverflow.com/a/31614770/5834657
<?php namespace Utilities\Encryption;/*** A class to handle secure encryption and decryption of arbitrary data** Note that this is not just straight encryption. It also has a few other* features in it to make the encrypted data far more secure. Note that any* other implementations used to decrypt data will have to do the same exact* operations. ** Security Benefits:** - Uses Key stretching* - Hides the Initialization Vector* - Does HMAC verification of source data**/class Encryption {/**
* @var string $cipher The mcrypt cipher to use for this instance
*/protected $cipher = '';/**
* @var int $mode The mcrypt cipher mode to use
*/protected $mode = '';/**
* @var int $rounds The number of rounds to feed into PBKDF2 for key generation
*/protected $rounds = 100;/**
* Constructor!
*
* @param string $cipher The MCRYPT_* cypher to use for this instance
* @param int $mode The MCRYPT_MODE_* mode to use for this instance
* @param int $rounds The number of PBKDF2 rounds to do on the key
*/public function __construct($cipher, $mode, $rounds = 100) {
$this->cipher = $cipher;
$this->mode = $mode;
$this->rounds = (int) $rounds;}/**
* Decrypt the data with the provided key
*
* @param string $data The encrypted datat to decrypt
* @param string $key The key to use for decryption
*
* @returns string|false The returned string if decryption is successful
* false if it is not
*/public function decrypt($data, $key) {
$salt = substr($data, 0, 128);
$enc = substr($data, 128, -64);
$mac = substr($data, -64);
list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key);
if ($mac !== hash_hmac('sha512', $enc, $macKey, true)) {
return false;
}
拉莫斯之舞
当年话下
随时随地看视频慕课网APP