如何解决使用 NodeJS 创建密码时出现未知密码错误
我正在将 Java 逻辑加密转换为 Node JS。
我的加密密钥是 24 个字符的 base64 字符串,如下所示。在java中,它被解码为byte[]并用于加密。所以在 NodeJS 中,我不知道最好的方法是什么。
但是我在创建密码时正在做 Buffer.from(key,'base64')
。这样对吗?我收到了 Unknown Cipher
。我尝试了不同的算法模式。没有运气。
有人可以建议使用哪种算法模式和 IV 密钥来使用 24 个字符基 64 字符串创建密码吗?
在使用加密 URL 之前,JAVA 和 NODE JS 之间的密码最终值是否相同以确保转换正确?
Java:
public class Encrypt {
private Encrypt() {}
public static String getURI() {
String encryptedFinal = "";
String userId = "userID";
String pwd = "aBEMxY2c/UWpoQFGSWSabD=="; //24 chars long base64 key
Date cDate = new Date();
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
simpledateformat.setTimeZone(TimeZone.getTimeZone("GMT"));
try
{
byte[] dBytes = simpledateformat.format(cDate).getBytes();
byte[] bArray = Base64.decodeBase64(pwd.getBytes());
SecretKeySpec keySpec = new SecretKeySpec(bArray,"AES");
System.out.println(simpledateformat.format(date).toString());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(1,keySpec);
byte[] cipher2 = cipher.doFinal(dBytes);
byte[] cipher3 = cipher.doFinal("bloomberge.com/home".getBytes());
encryptedFinal = "http://bloomberge.com/" + id + "&uri=" +
Base64.encodeBase64String(cipher3) + "&date=" +Base64.encodeBase64String(cipher2);
}
catch(Exception e)
{
System.out.println(e);
}
return encryptedFinal;
}
}
NodeJS:
const { createCipheriv } = require('crypto')
const crypto = require('crypto')
const userID = "aBEMxY2c/UWpoQFGSWSabD=="; //24 chars long base64 key
const key = "aBEMxY2c/UWpoQFGSWSabD=="; //24 chars long base64 key
const tDate = new Date().toISOString().split('.')[0] + 'Z'
let cipher = createCipheriv('aes-128-ecm',Buffer.from(key,'base64'),crypto.randomBytes(32)) //not sure if this is correct to read based 64 and how do I find correct iv length?. Getting the unknown cipher
let myTime = cipher.update(tDate,'utf8','base64')
myTime += cipher.final('base64')
let tURI = createCipheriv('aes-128-ecm',crypto.randomBytes(32)) //same unknown cipher,let tpURL = tURI.update(
'bloomberg','base64'
)
tpURL += tURI.final('base64')
return (
'myweburl' +
userId +
'&tURI=' +
tpURL +
'&tDateTime=' +
myTime)
这里的第一个密码出错
let cipher = createCipheriv('aes-128-ecm',crypto.randomBytes(32))
错误信息:
{"msg":"Unknown cipher","stack":"Error: Unknown cipher\n at Cipheriv.createCipherBase (internal/crypto/cipher.js:103:19)\n 在 Cipheriv.createCipherWithIV (internal/ crypto/cipher.js:121:20)\n 在新 Cipheriv (internal/crypto/cipher.js:227:22)\n 在 createCipheriv (crypto.js:124:10)\n 在
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。