問題描述
由于 Mcrypt 已被棄用,我想在我的代碼中使用 OpenSSL,因為我們已經在我們的服務器中使用了 php 7.0.17 并且不知道他們何時升級它.
Since Mcrypt is deprecated, I want to use OpenSSL instead in my code since we already using php 7.0.17 in our server and there's no tell when they upgrade it.
某些第三方 API(可能托管在 PHP 5.x 上并使用 mcrypt)正在獲取加密數據.他們提供了用于加密/解密字符串的方法.
Some third party API (hosted on PHP 5.x probably and using mcrypt), is taking encrypted data. They've provided methods which they are using to encrypt/decrypt strings.
他們在這里
$secret = 'a0a7e7997b6d5fcd55f4b5c32611b87c' ;
public function encrypt128($str)
{
$block = mcrypt_get_block_size("rijndael_128", "ecb");
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret, $str, MCRYPT_MODE_ECB));
}
public function decrypt128($str)
{
$str = base64_decode($str);
$str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $secret, $str, MCRYPT_MODE_ECB);
$len = strlen($str);
$pad = ord($str[$len - 1]);
return substr($str, 0, strlen($str) - $pad);
}
使用這些方法字符串 small1
如果加密變成 v7IXp5vVaFVXXlt/MN8BVw==
using these methods string small1
if encrypted becomes v7IXp5vVaFVXXlt/MN8BVw==
我們想在我們這邊使用 openssl_encrypt
這樣如果我們用 OpenSSL 加密相同的字符串,它必須給出與 Mcrypt 相同的結果.我研究過使用 rijndael-128 模式 ecb 的 mcrypt 應該與 OpenSSL aes-128-ecb
兼容.
We want to use openssl_encrypt
in our side such that if we encrypt same string with OpenSSL it must give same results as Mcrypt. I've researched that mcrypt using rijndael-128 Mode ecb should be compatible with OpenSSL aes-128-ecb
.
在過去的幾個小時里,我一直在嘗試使用自己的方法來加密使用 OpenSSL 提供相同結果的字符串.到目前為止,我已經到了這個
For last few hours, I've been trying to make my own method to encrypt strings serving same result by using OpenSSL. So far I've come to this
public function sslEncrypt128($str)
{
$secret = 'a0a7e7997b6d5fcd55f4b5c32611b87c';
return base64_encode(openssl_encrypt($str, 'aes-128-ecb', $secret, OPENSSL_RAW_DATA));
}
但它產生不同的字符串 SxJ3+EdaeItZx3/EwGTUbw==
與上述輸入相同.不知道是flag的問題還是padding的問題,歡迎指點.
But it produces different string SxJ3+EdaeItZx3/EwGTUbw==
for same as above input. I don't know if it is flag's problem or padding's, any pointers will be welcome.
我在此處添加了代碼以進行在線測試 https://3v4l.org/v2J2N
I've added the code here to test online https://3v4l.org/v2J2N
提前致謝.
推薦答案
在您的具體示例中,我發現通過將 aes-128-ecb
更改為 aes-256-ecb
,它產生與傳統 mcrypt_encrypt
相同的輸出.
In your specific example I've found that by changing aes-128-ecb
to aes-256-ecb
, it produces the same output as the legacy mcrypt_encrypt
.
這篇關于MCrypt rijndael-128 到 OpenSSL aes-128-ecb 的轉換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!