MCrypt rijndael-256到OpenSSL AES-256-ECB转换 [英] MCrypt rijndael-256 to OpenSSL aes-256-ecb conversion

查看:372
本文介绍了MCrypt rijndael-256到OpenSSL AES-256-ECB转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于不推荐使用Mcrypt,因此我想在代码中使用OpenSSL,因为我们已经在服务器中使用php 7.2.4.

Since Mcrypt is deprecated, I want to use OpenSSL instead in my code since we already using php 7.2.4 in our server.

我已使用以下代码进行加密/解密.

I have used following code for Encryption/Decryption.

//加密

function encrypt($text, $salt='') {
        if ($text == "") return "";
        if ($salt == "") $salt = 'DiAo74dOO09T48YESmuvbS0T';
        return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv   

(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
    }

//DECRYPTION

//DECRYPTION

function decrypt($text, $salt = '') {
        if ($text == "")
            return "";
        if ($salt == "")
            $salt = 'DiAo74dOO09T48YESmuvbS0T';
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv

(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
    }

"AFdT9sa81krHkp/GoYCSwh7/lZn/gLZLHJSldi5/QCU ="我已经使用上述加密功能对该字符串进行了加密,但是我希望它使用OPENSSL对其进行解密.我使用以下代码对其进行解密.

"AFdT9sa81krHkp/GoYCSwh7/lZn/gLZLHJSldi5/QCU=" This String I had encrypted using above encryption function, But I want it to decrypt it using OPENSSL. I used following code to decrypt it.

    $string = 'AFdT9sa81krHkp/GoYCSwh7/lZn/gLZLHJSldi5/QCU=';   
    $output = false;
    $secret_key = 'DiAo74dOO09T48YESmuvbS0T';   
    $secret_iv1 = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-256-ECB'));
    $secret_iv = bin2hex($secret_iv1);
    $key = hash('sha256', $secret_key);    
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    $output = base64_encode(openssl_encrypt($string, 'aes-256-ecb', $key, OPENSSL_RAW_DATA));

我希望解密输出为:"durhs-14767-w0163j1-89047" 预先感谢您的答复.

I want decrypted output as : "durhs-14767-w0163j1-89047" Thanks in advance for your reply.

推荐答案

很遗憾,您的方法有误.

Saddly, you are on the wrong way.

请参阅:

http://php.net/manual/en/function. mcrypt-encrypt.php#117667

MCRYPT_RIJNDAEL_256不是AES-256,它是 Rijndael分组密码.

MCRYPT_RIJNDAEL_256 is not AES-256, it's a different variant of the Rijndael block cipher.

https://en.wikipedia.org/wiki/Advanced_Encryption_Standard

AES是Rijndael的一种变体,具有128位的固定块大小, 密钥大小为128、192或256位.相比之下,Rijndael 规范本身是由块和密钥大小指定的,可能是 32位的任意倍数,最小为128,最大为256 位.

AES is a variant of Rijndael which has a fixed block size of 128 bits, and a key size of 128, 192, or 256 bits. By contrast, the Rijndael specification per se is specified with block and key sizes that may be any multiple of 32 bits, with a minimum of 128 and a maximum of 256 bits.

因此,您不能使用OpenSSL的AES-256解密MCrypt的输出.

So you can not use OpenSSL's AES-256 to decrypt the MCrypt's output.

一些可能的方法:

  1. 继续使用PECL的mcrypt扩展名使用mcrypt(幸运的是,它仍然存在),直到您可以完全替换旧数据为止.

  1. Keep using mcrypt by PECL's mcrypt extension (luckily, it is still there), until you can replace the legacy data totally.

用PHP重写正确的RIJNDAEL-256密码.

Rewrite a correct RIJNDAEL-256 cipher in PHP.

这篇关于MCrypt rijndael-256到OpenSSL AES-256-ECB转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆