【发布时间】:2019-12-27 22:26:43
【问题描述】:
我已使用 node.js 加密文件并在 JAVA 中解密。解密是在 JAVA 中使用“AES/GCM/Nopadding”算法完成的,它是第三方应用程序,因此我看不到 JAVA 代码。 我正在使用“aes-128-gcm”算法加密 node.js 中的有效负载。 为此,我正在尝试模仿一个有效的 java 加密代码
我已经尝试过加密和节点锻造。 我正在获取输出,但在提交有效负载时收到错误“加密错误 - 有效负载未正确加密”。
请帮我找出我在这段代码中做错了什么。
java 中的工作代码
public void encrypt(@NonNull final byte[] payload, @NonNull final byte[] key) throws GeneralSecurityException
{
SecretKeySpec codingKey = new SecretKeySpec(key, AES);
Cipher cipher = AEC_GCM_THREAD_CIPHER.get();
byte[] iv = new byte[cipher.getBlockSize()];
RANDOM.nextBytes(iv);
cipher.init(Cipher.ENCRYPT_MODE, codingKey, new IvParameterSpec(iv));
final byte[] encryptedPayload = cipher.doFinal(payload);
byte[] encryptMerchantKey = encryptMerchantKey(key);
String payloadFinal = encodeToUrlString(encryptedPayload); // final payload
String ivFinal = encodeToUrlString(iv); // final iv
String keyFinal = encodeToUrlString(encryptMerchantKey); // final key
System.out.println("Payload");
System.out.println(payloadFinal);
System.out.println("iv");
System.out.println(ivFinal);
System.out.println("key");
System.out.println(keyFinal);
}
我在节点 js 中尝试过的代码
function encrypt(payload) {
let key = forge.random.getBytesSync(16);
let iv = forge.random.getBytesSync(16);
let cipher = forge.cipher.createCipher("AES-GCM", key);
cipher.start({ iv: iv});
cipher.update(forge.util.createBuffer(payload));
cipher.finish();
let encrypted = forge.util.encode64(cipher.output.getBytes());
let tag = forge.util.encode64(cipher.mode.tag.getBytes());
let iv64 = forge.util.encode64(iv);
let encryptedPayload = encrypted+tag;
//RSA Encryption
encryptedkey = RSAencrypt(forge.util.encode64(key));
return {
"payload" : base64url.fromBase64(encryptedPayload) ,
"iv" : base64url.fromBase64(iv64).length,
"key" : base64url.fromBase64(encryptedkey)
};
}
Rsa 描述工作正常,能够解密密钥。 aes加密的一些问题。如代码所示,我将身份验证标签和加密数据一起添加但没有用。
【问题讨论】:
-
toeknToUrlString是做什么的?为什么您转换为 base 64 只是为了在您的 NodeJS 代码中再次对其进行解码?请注意,GCM 的 nonce (IV) 默认大小为 12 字节,而不是 16,因此与块大小不同(但 16 字节的 nonce 应该完全兼容)。请要求接受方的规范,不要让自己猜测它们! -
您还应该发布输出,不要让我们自己生成。您的 Java 显然添加了输出语句,那么它在哪里,为什么您的 NodeJS 没有相同类型的调试语句?
-
“错误加密 - 有效负载未正确加密”,提交有效负载时。
-
那不是函数的输出,那是您提交它的一方的结果(而且由于我们甚至不知道您如何提交数据,我们甚至无法判断该错误是否是在您向我们展示的函数中生成)。
-
因为您甚至没有表现出(学习)如何调试自己的代码的丝毫倾向,例如比较函数的输出,我想我们在这里帮不了你。这不是一个完整且可验证的最小示例,因为它不完整并且缺少输入/输出以及作为输入和输出的要求。
标签: java node.js encryption cryptography aes-gcm