小编Dav*_*ire的帖子

我得到javax.crypto.AEADBadTagException:标签不匹配!使用AES/GCM/NoPadding解密大文件时

我一直在使用BouncyCastle的提供程序来加密文件,使用"AES/GCM/NoPadding"作为转换.它适用于许多较小的文件.但是,对于较大的文件,我得到了例外:

javax.crypto.AEADBadTagException:标签不匹配!

这是我用来加密的代码:

    try (DataOutputStream os = new DataOutputStream(new FileOutputStream(encryptedFile))) {
        os.writeInt(CURRENT_ENCRYPTION_FILE_VERSION);
        os.writeInt(CURRENT_RSA_KEY_VERSION);

        // Generate a new random symmetric key for the AES encryption cipher
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey aesKey = keyGenerator.generateKey();

        // Write the RSA encrypted AES key to the file
        os.write(doRsaTransformation(aesKey.getEncoded(), publicRsaKey, Cipher.ENCRYPT_MODE));

        // Generate an initialization vector (IV)
        SecureRandom sr = new SecureRandom();
        byte[] iv = new byte[16];
        sr.nextBytes(iv);

        // Write the RSA encrypted IV to the file
        os.write(doRsaTransformation(iv, publicRsaKey, Cipher.ENCRYPT_MODE));

        // Write the encrypted …
Run Code Online (Sandbox Code Playgroud)

java encryption

7
推荐指数
0
解决办法
6261
查看次数

标签 统计

encryption ×1

java ×1