NodeJS 如何使用crypto-js库加密和编码?

yuvru6vn  于 5个月前  发布在  Node.js
关注(0)|答案(1)|浏览(58)

我试图使用crypto-js的帮助下使用密钥加密字符串,有两个问题,一个是它总是生成相同的密钥,这是不期望的,它应该是唯一的eveytime,第二个问题是加密密钥的大小比预期的要长,我试图打印这种格式和大小GwJPNUfPXZZsuc0iqOFhn%2BYhMJKxXBUYl9g3iKqL8CE%3D这总是包含2%的特殊字符。
试着用sha 256做encrpt和编码,并返回上面的模式有帮助吗?

index.js 

const CCrypto = require('crypto-js');

function encryptSHA256(data, secretKey) {
    const secretKeyWordArray = CCrypto.enc.Utf8.parse(secretKey);
    return CCrypto.AES.encrypt(data, secretKeyWordArray, {
        mode: CCrypto.mode.CBC,
        padding: CCrypto.pad.Pkcs7,
        iv: CCrypto.lib.WordArray.create([0]),
    }).toString();
}

function generateEncryptedValue(paymentId, specialtyID, secretKey) {
    const concatString = paymentId.toString() + "_" + specialtyID;
    const encryptedResult = encryptSHA256(concatString, secretKey);
    const base64Result = CCrypto.enc.Base64.stringify(CCrypto.enc.Utf8.parse(encryptedResult));
    const percentEncodedResult = encodeURIComponent(base64Result);
    return percentEncodedResult;
}

// Example usage
const paymentId = 15680298; // paymentId is a number
const specialtyID = '8018290';
const secretKey = "EiD0BVQle0xFjZvYOupQsXCWAcAwBaTjlZ7G7rryNos=";
const result = generateEncryptedValue(paymentId, specialtyID, secretKey);
console.log('Encrypted and Percent Encoded Result:', result);

字符串
Java代码,这是在遗留应用程序中,我们正在移动到Nodejs:

<paymentMethodId>
                                                                  
                                                                   <xsl:value-of select="xsutil:encryptSha256(concat(string(paymentMethodId),'_',$patientIdentifier))"/>
                </paymentMethodId>

here parameter is concatenated string which is (paymentMehtodId_specialtyID) there is some inbuilt function is being used beyond this.

<func:function name="xsutil:encryptSha256">
      <xsl:param name="parameter"/>
      <func:result select="dp:encode(dp:encrypt-string('http://www.w3.org/2001/04/xmlenc#aes256-cbc',$secrerKey,$parameter),'url')"/>
    </func:function>

9udxz4iz

9udxz4iz1#

几个问题:
1.常数密钥:您正在使用一个恒定的密钥(secretKey)进行加密。要每次生成一个唯一的密钥,您需要生成一个随机密钥或使用不同的密钥生成机制。

  1. Base64编码:您正在使用Base64对加密结果进行两次编码(一次是在加密时,一次是在转换为UTF-8时)。您应该只执行一次Base64编码。
    更新了以下代码修复示例:
const CCrypto = require('crypto-js');

function generateSecretKey() {
  const keySize = 32; // 256 bits
  return CCrypto.lib.WordArray.random(keySize).toString();
}

function encryptSHA256(data, secretKey) {
    const secretKeyWordArray = CCrypto.enc.Utf8.parse(secretKey);
    const encrypted = CCrypto.AES.encrypt(data, secretKeyWordArray, {
        mode: CCrypto.mode.CBC,
        padding: CCrypto.pad.Pkcs7,
        iv: CCrypto.lib.WordArray.create([0]),
    });
    
    return encrypted.toString();
}

function generateEncryptedValue(paymentId, specialtyID, secretKey) {
    const concatString = paymentId.toString() + "_" + specialtyID;
    const encryptedResult = encryptSHA256(concatString, secretKey);
    const percentEncodedResult = encodeURIComponent(encryptedResult);
    return percentEncodedResult;
}

const paymentId = 15680298; 
const specialtyID = '8018290';
const secretKey = generateSecretKey(); 
const result = generateEncryptedValue(paymentId, specialtyID, secretKey);
console.log('Encrypted and Percent Encoded Result:', result);

字符串

相关问题