我有以下的AES加密在我的Angular 项目中执行,我使用的是CroyptoJS Example_aes

我的上述加密代码如下-

let keyTool = {
  "vector": "njdsslndlsnlkds",
  "keySource": "abchjkolg@#!djfhrye"
}
let iv = keyTool.vector;

let key = CryptoJS.SHA256(keyTool.keySource).toString().substring(0, 31);

let newkey = CryptoJS.enc.Utf8.parse(key);
let value = CryptoJS.enc.Utf8.parse("abc@gmail.com");
let newIv = CryptoJS.enc.Utf8.parse(iv)

let newencrypted = CryptoJS.AES.encrypt(value, newkey, 
  {  
    iv: newIv, 
    mode: CryptoJS.mode.CBC, 
    padding: CryptoJS.pad.Pkcs7 });


console.log(newencrypted)
console.log(newencrypted.toString())

我的代码的加密串是-51d3A1w5NWHy4uagWeRm+C9GYcesDVMSqcsEi9T2c1Y= 预期为-RBmAWXyJm8d9zV4XaR9BJcLQxTVTM3XEHSHFhGcV75E=


更新:

textToEncrypt - "abc@gmail.com"
iv - "njdsslndlsnlkds"
key - "6779b2624c837845a4ec16e7cd1fa40"
other details - CBC, PKCS7, 256 bits (key-length)
cipherText - 51d3A1w5NWHy4uagWeRm+C9GYcesDVMSqcsEi9T2c1Y= (my o/p) 
             Yv9wOiDtSdBS+nWwSqsitg== (website o/p)

推荐答案

key的UTF-8编码长度仅为31字节

e35417cb037f9911089ec4b94d2360b

网站implicitly通过将0x00附加到末尾来填充到AES-256所需的32字节长度.

在JavaScript代码中,0x00的值必须是explicitly:

key = key + "\0";

测试:

let riskLogicKeyTool = {
    "vector": "HmK9U6Y8t8seuwI5",
    "keySource": "risklogic@#!encrypt"
}
let iv = riskLogicKeyTool.vector;

let key = CryptoJS.SHA256(riskLogicKeyTool.keySource).toString().substring(0, 31);
key = key + "\0"; // Fix!

let newkey = CryptoJS.enc.Utf8.parse(key);
let value = CryptoJS.enc.Utf8.parse("nqsuper+cat@gmail.com");
let newIv = CryptoJS.enc.Utf8.parse(iv)

let newencrypted = CryptoJS.AES.encrypt(value, newkey, 
    {  
        iv: newIv, 
        mode: CryptoJS.mode.CBC,      // default 
        padding: CryptoJS.pad.Pkcs7   // default    
    });


//console.log(newencrypted);
console.log(newencrypted.toString()); // RBmAWXyJm8d9zV4XaR9BJcLQxTVTM3XEHSHFhGcV75E=
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>


Both the website and CryptoJS make it harder to identify the problem because they both accept invalid keys. Valid key lengths for AES are 16, 24 and 32 bytes. In case of invalid key lengths, as here with 31 bytes, an appropriate error message/exception should be generated.
In the case of CryptoJS this is a (known) bug, see issue #293. Care should be taken with crypto websites in general, most are not reliable.


这很奇怪(但当然可以满足您的要求),从十六进制编码的SHA-256散列只使用31个字节,而不是32个字节.因此,仅为完整起见:考虑32字节key的值为:

e35417cb037f9911089ec4b94d2360bf

网站和JavaScript代码以密文形式返回:

YcJO+j3ID9eLbGkFR+TkVDsaO57y28IcHCLzuHOH4zA=

Security: It is a vulnerability if a fast hash like SHA256 is used as key derivation function. Instead, a reliable key derivation function like PBKDF2 or the more modern scrypt or Argon2 should be used (in combination with a random salt).
Similarly, the use of a static IV is a vulnerability.
In addition, deriving the key from a hex encoded string via UTF-8 encoding reduces security because the range of values per byte is reduced from 256 to 16 values. Also, there may be cross-platform compatibility issues (uppercase, lowercase hex digits).

Angular相关问答推荐

Goto锚定在嵌套容器中,具有溢出自动

Swiper 11角17 SSR Swiper断点不工作

HTTP POST响应失败Angular 16

是否自动处理Angular /RxJS观测数据的取消订阅

如何防止打印后的空格后的HTML元素的Angular ?

Angular 17+独立配置中main.ts bootstrap 调用的最佳实践

从 Angular 应用程序中删除散列标签后无法从 Apache 访问 API

组件的导入不能以Angular 工作

将 autoSpy 与 ng-mocks 一起使用时,如何重置 jasmine 间谍调用?

iss 声明无效 Keycloak

Angular 13 中