将我的加密库从Mcrypt升级到OpenSSL

将我的加密库从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;
    }
拉莫斯之舞
浏览 332回答 1
1回答

当年话下

您的解密例程代码适用于我:public&nbsp;function&nbsp;decrypt($data,&nbsp;$key)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$salt&nbsp;=&nbsp;substr($data,&nbsp;0,&nbsp;128); &nbsp;&nbsp;&nbsp;&nbsp;$enc&nbsp;=&nbsp;substr($data,&nbsp;128,&nbsp;-64); &nbsp;&nbsp;&nbsp;&nbsp;$mac&nbsp;=&nbsp;substr($data,&nbsp;-64); &nbsp;&nbsp;&nbsp;&nbsp;list&nbsp;($cipherKey,&nbsp;$macKey,&nbsp;$iv)&nbsp;=&nbsp;$this->getKeys($salt,&nbsp;$key); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($mac&nbsp;!==&nbsp;hash_hmac('sha512',&nbsp;$enc,&nbsp;$macKey,&nbsp;true))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;$dec&nbsp;=&nbsp;openssl_decrypt($enc,&nbsp;$this->cipher,&nbsp;$cipherKey,&nbsp;OPENSSL_RAW_DATA,&nbsp;$iv); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$dec;}测试:$keys&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;'this&nbsp;is&nbsp;a&nbsp;secret&nbsp;key.', &nbsp;&nbsp;&nbsp;&nbsp;'G906m70p(IhzA5T&5x7(w0%a631)u)%D6E79cIYJQ!iP2U(xT13q6)tJ6gZ3D2wi&0")7cP5', &nbsp;&nbsp;&nbsp;&nbsp;chr(6)&nbsp;.&nbsp;chr(200)&nbsp;.&nbsp;chr(16)&nbsp;.&nbsp;'my&nbsp;key&nbsp;'&nbsp;.&nbsp;chr(3)&nbsp;.&nbsp;chr(4)&nbsp;.&nbsp;chr(192)&nbsp;.&nbsp;chr(254)&nbsp;.&nbsp;'&nbsp;zyx0987!!', &nbsp;&nbsp;&nbsp;&nbsp;'and&nbsp;finally&nbsp;one&nbsp;more&nbsp;key&nbsp;to&nbsp;test&nbsp;with&nbsp;here:',];$data&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;'A', &nbsp;&nbsp;&nbsp;&nbsp;'This&nbsp;is&nbsp;a&nbsp;test', &nbsp;&nbsp;&nbsp;&nbsp;'now&nbsp;test&nbsp;encrypting&nbsp;something&nbsp;a&nbsp;little&nbsp;bit&nbsp;longer&nbsp;with&nbsp;1234567890.', &nbsp;&nbsp;&nbsp;&nbsp;'$length&nbsp;=&nbsp;mcrypt_get_block_size(MCRYPT_BLOWFISH,&nbsp;MCRYPT_MODE_CBC);&nbsp;$last&nbsp;=&nbsp;ord($data[strlen($data)&nbsp;-&nbsp;1]);', &nbsp;&nbsp;&nbsp;&nbsp;'Lorem&nbsp;ipsum&nbsp;dolor&nbsp;sit&nbsp;amet,&nbsp;consectetur&nbsp;adipiscing&nbsp;elit.&nbsp;Donec&nbsp;sit&nbsp;amet&nbsp;pharetra&nbsp;urna.&nbsp;Vestibulum&nbsp;ante&nbsp;ipsum&nbsp;primis&nbsp;in&nbsp;faucibus&nbsp;orci&nbsp;luctus&nbsp;et&nbsp;ultrices&nbsp;posuere&nbsp;cubilia&nbsp;Curae;&nbsp;Ut&nbsp;fringilla,&nbsp;quam&nbsp;sed&nbsp;eleifend&nbsp;eleifend,&nbsp;justo&nbsp;turpis&nbsp;consectetur&nbsp;tellus,&nbsp;quis&nbsp;tristique&nbsp;eros&nbsp;erat&nbsp;at&nbsp;nibh.&nbsp;Nunc&nbsp;dictum&nbsp;neque&nbsp;vel&nbsp;diam&nbsp;molestie&nbsp;fermentum.&nbsp;Pellentesque&nbsp;dignissim&nbsp;dui&nbsp;quis&nbsp;tortor&nbsp;eleifend,&nbsp;ut&nbsp;maximus&nbsp;elit&nbsp;egestas.&nbsp;Donec&nbsp;posuere&nbsp;odio&nbsp;et&nbsp;auctor&nbsp;porta.&nbsp;Quisque&nbsp;placerat&nbsp;condimentum&nbsp;maximus.&nbsp;Curabitur&nbsp;luctus&nbsp;dolor&nbsp;eget&nbsp;sem&nbsp;luctus,&nbsp;in&nbsp;dignissim&nbsp;tortor&nbsp;venenatis.&nbsp;Mauris&nbsp;eget&nbsp;nulla&nbsp;nisl.',];$failures&nbsp;=&nbsp;0;foreach&nbsp;($data&nbsp;as&nbsp;$datum)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;($keys&nbsp;as&nbsp;$key)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$enc&nbsp;=&nbsp;new&nbsp;Encryption(MCRYPT_BLOWFISH,&nbsp;MCRYPT_MODE_CBC); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$encrypted&nbsp;=&nbsp;$enc->encrypt($datum,&nbsp;$key); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$dec&nbsp;=&nbsp;new&nbsp;EncryptionOpenSsl('bf-cbc'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$decrypted&nbsp;=&nbsp;$dec->decrypt($encrypted,&nbsp;$key); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(strcmp($datum,&nbsp;$decrypted)&nbsp;!==&nbsp;0)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"Encryption&nbsp;with&nbsp;key&nbsp;'$key'&nbsp;of&nbsp;'$datum'&nbsp;failed.&nbsp;&nbsp;'$decrypted'&nbsp;!=&nbsp;'$datum'<br><br>\n\n"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$failures++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}}if&nbsp;($failures)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"$failures&nbsp;tests&nbsp;failed.<br>\n";}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"ALL&nbsp;OKAY<br>\n";}如果你能确认它对你有用,我可以整理答案并添加最终的工作代码。
打开App,查看更多内容
随时随地看视频慕课网APP