我正在尝试解密来自nodejs上的java代码的加密值
以下是java的加密方法
public class EncryptUtil {
public static String getKey()
{
return "somekeysomekey+)"; //key length 16!Use this on nodejs
}
public static String encryptAES(String ID) throws Exception {
Key secretKeySpec = new SecretKeySpec(getKey().getBytes(), "AES");
String transform = "AES/ECB/ISO10126Padding";
String output = "";
try {
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(transform);
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, secretKeySpec);
String originStr = ID;
byte[] input = originStr.getBytes("UTF8");
byte[] output = cipher.doFinal(input);
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
output = encoder.encode(output);
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
return output;
}
}
//This is how to encrypt from above
//String encryptText = (String) EncryptUtil.encryptAES("something");
//System.out.println(encryptText) ---> "47gPeqm+0lvKb0VNXF29yQ==";
这是我在nodejs
上解密上述结果的代码const crypto = require('crypto');
const key = 'somekeysomekey+)';
const algorithm = 'aes-128-ecb';
function decryptFunc(encryptedStr) {
const base64Decoded = new Buffer(encryptedStr, 'base64').toString('binary');
const decipher = crypto.createDecipher(algorithm, key);
decipher.setAutoPadding(false);
let result = decipher.update(base64Decoded, 'binary', 'binary');
result += decipher.final('binary');
return new Buffer(result).toString('utf8');
};
但我的代码无效.. 我如何在nodejs上解密?..
答案 0 :(得分:0)
由于" CryptoJS"我解决了这个问题。模块!
//decryption
import CryptoJS from 'crypto-js';
const key = "somekeysomekey+)";
const decryptParam = function(param) {
const value = param ? param.replace(' ', '+') : param;
if (_.isEmpty(value)) {
return;
}
const hexKey = CryptoJS.enc.Hex.parse(new Buffer(key).toString('hex'));
const decryptOpt = {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Iso10126};
const bytes = CryptoJS.AES.decrypt(value, hexKey, decryptOpt);
return bytes.toString(CryptoJS.enc.Utf8);
};