【问题标题】:Encryption in php and AES decryption in angular using crypto JS使用crypto JS的php加密和角度AES解密
【发布时间】:2020-11-24 02:47:47
【问题描述】:

这是我在 PHP 中的以下加密和解密,它工作正常,但我需要在 angular 7 中解密相同的东西,

Encryption in php using aes-256-cbc by following method
$this->data ="plaintext";
$this->
openssl_encrypt("plaintext", "aes-256-cbc", "1234567890",0,"1234567890")

但我尝试使用 crypto-js 但它返回空值,

CryptoJS.AES.decrypt("ciphertext","1234567890".toString(CryptoJS.enc.Utf8);

提出任何想法。

我的目标是加密 php 中的字符串并解密 angular 中的值

【问题讨论】:

  • 你的代码语法完全错误:2个左括号,只右一个。
  • 我放了例子,没关系
  • 在您的 PHP 代码中,您使用了错误的键和 IV 大小。
  • Topaco,但它在 php 中解密
  • 您必须将密钥和 IV 作为WordArray: CryptoJS.AES.decrypt(ciphertext, keyWA, {iv: ivWA}); 传递。 WordArrays 可以使用编码器创建,例如:var ivWA = CryptoJS.enc.Utf8.parse("1234567890123456"); 如果您随后在 both 代码中使用 same 键和 IV,它将起作用。

标签: php angular aes cryptojs


【解决方案1】:

试试下面的代码:

const configuration = {
  keySize: 128 / 8,
  iv: CryptoJS.enc.Utf8.parse('<your initialization vector>'),
  mode: CryptoJS.mode.CBC
};

CryptoJS.AES.decrypt("ciphertext", "<your encryption key>", configuration).toString(CryptoJS.enc.Utf8);

【讨论】:

  • 感谢您的努力@Gopal Zadafiya,我在 CryptoJS.AES.decrypt 之后得到了 wordArray 对象,所以我需要将 wordArray 转换为纯文本的解决方案。您的上述建议返回结果为空
猜你喜欢
  • 2014-09-12
  • 2014-02-06
  • 1970-01-01
  • 2020-06-16
  • 2013-08-16
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多