首页 > 解决方案 > 为什么 node.js 使用 Crypto.js 加密,但解密失败?

问题描述

我使用 json 来加密这些值。但是,我尝试再次解密该值,但没有成功。该值为空。

node.js 源码

import CryptoJS from 'crypto-js';

const EncryptHex = (string, chip) => {
  let result = '';
  try {
    const key = CryptoJS.enc.Hex.parse(STORE_KEY);
    if (chip === 'AES') {
      const iv = CryptoJS.lib.WordArray.create([0x00, 0x00, 0x00, 0x00]);
      result = CryptoJS.AES.encrypt(string, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
      });
      result = result.ciphertext.toString(CryptoJS.enc.Hex);
      DecryptHex(result, 'AES', STORE_KEY);
    } else {
      result = CryptoJS.HmacSHA256(string, key).toString(CryptoJS.enc.Hex);
    }
    return result;
  } catch (error) {
    throw error;
  }
};

const DecryptHex = (string, chip, skey) => {
  let result = '';
  const key = CryptoJS.enc.Hex.parse(skey);
  const iv = CryptoJS.lib.WordArray.create([0x00, 0x00, 0x00, 0x00]);
  try {
    const array = CryptoJS.enc.Hex.parse(string);
    if (chip === 'AES') {
      result = CryptoJS.AES.decrypt(array, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
      }).toString();
      console.log('$$$$');
      console.log('result : ', result);
      console.log('$$$$');
    } else {
      result = array;
    }
    return result;
  } catch (error) {
    throw error;
  }
};

对数值

$$$$
result :  
$$$$

是什么原因?我怎么解决这个问题?

标签: node.jsencryptioncryptography

解决方案


我们需要做几件事,第一,获得正确的密钥长度,这应该是 128,192 或 256 位长。

在这段代码中,我使用的是 256 位密钥。不要在生产中使用它:)

此外,如果您在解密时使用格式:CryptoJS.format.Hex,它将接受十六进制编码的密文。

这应该加密然后解密测试消息:

// Using a 256-bit key to ensure we use AES-256
STORE_KEY = "7246b9b8171743cd9cf285367a2fd5ad";

const EncryptHex = (string, chip, skey) => {
    let result = '';
    try {
        const key = CryptoJS.enc.Hex.parse(skey);
        if (chip === 'AES') {
            const iv = CryptoJS.lib.WordArray.create([0x00, 0x00, 0x00, 0x00]);
            result = CryptoJS.AES.encrypt(string, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
            });
            result = result.ciphertext.toString(CryptoJS.enc.Hex);
        } else {
            result = CryptoJS.HmacSHA256(string, key).toString(CryptoJS.enc.Hex);
        }
        return result;
    } catch (error) {
        throw error;
    }
};

const DecryptHex = (encryptedStringHex, chip, skey) => {
    let result = '';
    const key = CryptoJS.enc.Hex.parse(skey);
    const iv = CryptoJS.lib.WordArray.create([0x00, 0x00, 0x00, 0x00]);
    try {
        const array = CryptoJS.enc.Hex.parse(encryptedStringHex);
        if (chip === 'AES') {
            result = CryptoJS.AES.decrypt(encryptedStringHex, key, {
                iv: iv,
                format: CryptoJS.format.Hex,
                mode: CryptoJS.mode.CBC,
            }).toString(CryptoJS.enc.Utf8);
            console.log('$$$$');
            console.log('result : ', result);
            console.log('$$$$');
        } else {
            result = array;
        }
        return result;
    } catch (error) {
        throw error;
    }
};

let encrypted = EncryptHex("A good story cannot be devised; it has to be distilled", "AES", STORE_KEY);
console.log("Encrypted data:", encrypted);

console.log("Decrypted data:");
DecryptHex(encrypted, "AES", STORE_KEY);

推荐阅读