NodeJS 使用加密库看起来像密码更新不按预期工作?

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

我有加密模块做字符串加密时,通过字符串不是16字节我假设密码.更新()应该做一些填充,并创建16字节字符串,但这是不是发生在调试密码.更新返回空字符串,有其他方法来做填充使用加密.
index.js

function paymentIdencryption(text)

        const secret_key = "EiE0BVQle0xFjZvYOupKjFGHAcAwBaTjlZ7G7rryNos=";
        const secretKeyWordArray = CCrypto.enc.Utf8.parse(secret_key);

        const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(secret_key, 'base64'), iv);
        let encrypted = cipher.update(JSON.stringify(text), 'utf8', 'base64');
        encrypted += cipher.final('base64');
        const encryptedData = { iv: iv.toString('base64'), encrypted: encodeURIComponent(encrypted)};
       return encryptedData.encrypted;
}

console.log(paymentIdencryption("1384220_8089105");

字符串
预期结果键大小:
GwJPNUfPXZZsuc0iqOFhn%2BYhMJKxXBUGl9g3iKqL8CE%3D
但它返回:jJtKhwjqS5N4ABIrZev8Ng%3D%3D
执行解密的Java代码

var imp = new JavaImporter(com.vordel.mime,org.json.simple.JSONObject,com.vordel.trace);
 
with(imp) {
function invoke(msg)         {            
 
var encodedKey = "${env.SPL.paymentMethodId.key}";
Trace.info ("env.SPL.paymentMethodId.key -- encodedKey  = " + encodedKey); 
var encodedPaymentInfo= msg.get("cvs.paymentMethodId");
 
var encryptedPaymentInfo = java.net.URLDecoder.decode(encodedPaymentInfo,"UTF-8");

var decodedKey = java.util.Base64.getDecoder().decode(encodedKey.getBytes("UTF-8"));
var decodedcipher = java.util.Base64.getDecoder().decode(encryptedPaymentInfo.getBytes("UTF-8"));

 
var cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding"); 
var secretKey = new javax.crypto.spec.SecretKeySpec(decodedKey, "AES"); 
var iv = new javax.crypto.spec.IvParameterSpec(decodedcipher,0,16);
cipher.init(javax.crypto.Cipher.DECRYPT_MODE,secretKey,iv);
 
var decryptedPaymentInfo = cipher.doFinal(decodedcipher, 16, decodedcipher.length-16);
var paymentInfo = new java.lang.String(decryptedPaymentInfo,"UTF-8"); 
var patientId = paymentInfo.substring(paymentInfo.indexOf("_")+1, paymentInfo.length());
var paymentId = paymentInfo.substring(0, paymentInfo.indexOf("_"));
 
msg.put("cvs.spl.patientId",patientId);
msg.put("cvs.spl.paymentId",paymentId);
 
return true;         
 
}
};

2q5ifsrm

2q5ifsrm1#

Java代码执行加密数据的URL解码,然后执行Base64解码。结果的前16个字节用作IV,其余的用作密文。
密钥是Base64解码的。
使用密钥和IV,密文现在使用CBC模式和PKCS#7填充的AES解密。
解密后的数据使用UTF-8进行解码。
从结果字符串中提取patientid_之后的数据)和paymentid_之前的数据)。
由此,可以确定加密的逻辑:

  • 密钥的Base64解码
  • 生成随机16字节IV
  • 使用AES-256-cbc(AES-256,因为密钥为32字节)加密,使用密钥和IV
  • IV和密文的级联
  • 密文的Base64编码和随后的URL编码

可能的NodeJS实现:

var crypto = require('crypto')
function paymentIdencryption(text) {
    const secret_key = "EiE0BVQle0xFjZvYOupKjFGHAcAwBaTjlZ7G7rryNos=";
    const iv = crypto.randomBytes(16);
    const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(secret_key, 'base64'), iv);
    const encrypted = Buffer.concat([iv, cipher.update(text, 'utf8'), cipher.final()]);
    return encodeURIComponent(encrypted.toString('base64'))
}
console.log(paymentIdencryption("1384220_8089105"));

字符串
可能的输出(由于随机IV,每次加密的变化):

5I43wWYs84XQk9Pe0IMl4g%2B7s%2BgwcJrPsWo9gcX25y8%3D

使用Java代码测试:

public class Main {
    public static void main(String[] args) throws Exception {
        
        var encodedPaymentInfo = "5I43wWYs84XQk9Pe0IMl4g%2B7s%2BgwcJrPsWo9gcX25y8%3D"; 
        var encodedKey = "EiE0BVQle0xFjZvYOupKjFGHAcAwBaTjlZ7G7rryNos=";
        
        var encryptedPaymentInfo = java.net.URLDecoder.decode(encodedPaymentInfo,"UTF-8");

        var decodedKey = java.util.Base64.getDecoder().decode(encodedKey.getBytes("UTF-8"));
        var decodedcipher = java.util.Base64.getDecoder().decode(encryptedPaymentInfo.getBytes("UTF-8"));
         
        var cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding"); 
        var secretKey = new javax.crypto.spec.SecretKeySpec(decodedKey, "AES"); 
        var iv = new javax.crypto.spec.IvParameterSpec(decodedcipher,0,16);
        cipher.init(javax.crypto.Cipher.DECRYPT_MODE,secretKey,iv);
         
        var decryptedPaymentInfo = cipher.doFinal(decodedcipher, 16, decodedcipher.length-16);
        var paymentInfo = new java.lang.String(decryptedPaymentInfo,"UTF-8"); 
        var patientId = paymentInfo.substring(paymentInfo.indexOf("_")+1, paymentInfo.length());
        var paymentId = paymentInfo.substring(0, paymentInfo.indexOf("_"));
         
        System.out.println("cvs.spl.patientId: " + patientId);
        System.out.println("cvs.spl.paymentId: " + paymentId);
    }
}


输出量:

cvs.spl.patientId: 8089105
cvs.spl.paymentId: 1384220


其等于原始明文的数据。

相关问题