在 Javascript 中使用 CryptoJS 加密字符串并使用 openssl_decrypt 在 PHP 中解密 - 不起作用

问题描述 投票:0回答:1

在我的 Angular 应用程序中,我需要加密一个字符串并将其发送到 PHP 应该解密的远程服务器。我正在使用 javascript 库 CryptoJS。 PHP 版本是 7.4。我尝试的所有组合都失败了。

我在这里阅读了类似的问题,并尝试在我的应用程序中实现他们的代码,但我找不到完成它的方法。

我总是在 PHP 中遇到 openssl 错误:

错误:06065064:数字信封例程:EVP_DecryptFinal_ex:解密错误。

这是我的最后一个代码:

const q = 'hello world';
const cryptKey = 'a';
const secretKey = CryptoJS.MD5(cryptKey).toString();
const iv = CryptoJS.enc.Hex.parse(CryptoJS.MD5(CryptoJS.MD5(cryptKey).toString()).toString());
let encrypted = CryptoJS.AES.encrypt(q, secretKey, {
  iv: iv
});
let encoded = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
$cryptKey = 'a'; 
$encoded = "r+crKP3pmlRoAovRPCK2dQ=="; //= encoded in javascript

$decrypted = openssl_decrypt(base64_decode($encoded), "aes-256-cbc", md5($cryptKey), 0, md5(md5($cryptKey)));
javascript php encryption cryptojs cryptdecrypt
1个回答
0
投票

我发现了this问题,其答案可以帮助我解决问题。 谢谢@Topaco

© www.soinside.com 2019 - 2024. All rights reserved.