我在JAVA中有以下加密函数。我正试图用密码(从密码)在Node.js中编写同样的加密。但是,输出是不一样的。它使用相同的键和输入。
JAVA
public static String encrypt(String input, String key) {
byte[] crypted = null;
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes());
} catch (Exception e) {
System.out.println(e.toString());
}
String result = new String(Base64.encodeBase64(crypted));
return result.replace("+", "-");
}样本输出:0HCkcjWj/PoCZ4ZUFJARs/m4kpuffk8dQnT0uNhog=(44个字符)
Node.js
encrypt = (input, key) => {
const algorithm = 'aes-128-cbc';
key = crypto.scryptSync(key, 'salt', 16);
const iv = Buffer.alloc(16, 0);
const cipher = crypto.createCipheriv(algorithm, key, iv);
cipher.setAutoPadding(true);
let encrypted = cipher.update(input, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted.replace('+','-');
}示例输出: ZHtEbAhrIo7vWOjdMNgW6Q== (24个字符)
提前谢谢。
发布于 2020-01-20 08:26:21
因此,NodeJS代码在功能上与NodeJS代码中的Java代码相同:
https://stackoverflow.com/questions/59817296
复制相似问题