当前位置: 首页 > 知识库问答 >
问题:

在节点中加密文件,在java中使用“aes/gcm/nopadding”进行解密。在Node.js中基本上是AES/GCM/Nopadding等价的

殳智志
2023-03-14
const
 algorithm = 'aes-256-gcm',
 randomKey = crypto.randomBytes( 32 ),
 randomIv = crypto.randomBytes( 16 );
const
 cipher = crypto.createCipheriv( algorithm, randomKey, randomIv ),
 input = fs.createReadStream( './imageTest.jpg.gz' ), //gzip image 
 output = fs.createWriteStream( './imageTest.jpg.gz.enc' );

input.pipe( cipher ).pipe( output );
byte[] decrypt(byte[] encrptedData, byte[] key, byte[] iv) {

    GCMParameterSpec ivSpec = new GCMParameterSpec(128, iv);
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), ivSpec);
    return cipher.doFinal(encryptedData);
}
Caused by: javax.crypto.AEADBadTagException: Tag mismatch!
    at com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:571)
    at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1046)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:983)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)

共有1个答案

荣俊
2023-03-14

解密完成后,将这一行添加到NodeJs中,以获得身份验证标记。

const tag = cipher.getAuthTag();

也传送这个标签。

还有。在Java部分,将其追加到dofinal之前

cipher.update(textBytes);
 类似资料: