我正在使用以下代码段在 PHP 中为 CBC 256 模式进行 AES 加密。
$iv_real = "ahc/2u6F0Yvww12fyQiZWA==";
$decoded_iv = base64_decode($iv_real);
$plaintext_shared_secret = "9b8a3e600073de05e5d095b5d909043e50f5047ffcd0048c01c65ca690b7b4e981e51b59641d4ffd5a140c27f25a761ab0f99e601b59c5ae3427c751bfae9331";
echo "Shared secret: {$plaintext_shared_secret}\r\n";
$aes_key = hash("sha256", "s@keypact.appa62f1bed41166b2c455d82337222723b0287d920");
$encrypted_shared_secret = openssl_encrypt(
$plaintext_shared_secret,
"aes-256-cbc",
$aes_key,
OPENSSL_ZERO_PADDING,
$decoded_iv //Binary data
);
//Base64 encoded, encryped shared secret
echo "\r\nEncrypted, base64_encoded, shared secret\r\n";
var_dump($encrypted_shared_secret);
我得到的加密响应与我在 React Native 中得到的响应相差甚远。我需要在 ReactNative 中解密相同的数据。我试过了
- React-native-simple-crypto
- react-native-crypto-js
但是它们似乎都没有工作并提供解密数据,甚至没有像我使用 PHP 一样获得加密数据。
填充支持不在任何反应模块中。
我选择了 ReactNative Framework,以为它是最先进的框架,但是我已经浪费了很多天来弄清楚这个问题,所以不知道如何解决这个问题。
以下是反应代码
async DecryptSharedSecret() {
// var plaintext_secret = "9b8a3e600073de05e5d095b5d909043e50f5047ffcd0048c01c65ca690b7b4e981e51b59641d4ffd5a140c27f25a761ab0f99e601b59c5ae3427c751bfae9331";
var strIV = 'ahc/2u6F0Yvww12fyQiZWA==';
var iv_bin = Base64.Decode(strIV, strIV.length);
// Decoded value IV is in Binary when converted to Hex its 6a173fdaee85d18bf0c35d9fc9089958
var shared_secret =
'n4gIdzwY5UsOpzGtslpRlyNjLwLla7sJWfGkfH0GHadPFjtsOxNGJgqRe9WnjYkbNjCgwvvAvQtYV1kFHTpCIS4zDJa3h/2ADHrDgC7ym3HUyMeVIWgFKRCZYeVKz8SEHmullNxWG6dCjsWEbK6yqVmpNfwJSeh0XHHDFe0/Sak=';
var shared_secret_bin = Base64.Decode(shared_secret, shared_secret.length);
// Decoded Value shared_secret is in Binary when converted to HEx for printing, Value is
// '9f8808773c18e54b0ea731adb25a519723632f02e56bbb0959f1a47c7d061da74f163b6c3b1346260a917bd5a78d891b3630a0c2fbc0bd0b585759051d3a42212e330c96b787fd800c7ac3802ef29b71d4c8c79521680529109961e54acfc4841e6ba594dc561ba7428ec5846caeb2a959a935fc0949e8745c71c315ed3f49a9'
var strKey = 's@keypact.appa62f1bed41166b2c455d82337222723b0287d920';
var keysha256 = await RNSimpleCrypto.SHA.sha256(strKey);
console.log('Key SHA 256 ===== >> ', keysha256);
// var key =
// '074bf4849734a4c3a653fc213f2734d23f5dc63ce9d6244f386276097f032ad7';
let bytes = CryptoJS.AES.decrypt(
shared_secret_bin,
CryptoJS.enc.Hex.parse(keysha256),
{
iv: iv_bin,
mode: CryptoJS.mode.CBC,
},
);
var decrypted = bytes.toString(CryptoJS.enc.Utf8);
console.log('Decrypted Data ', decrypted);
} }