java - AES/GCM/NoPadding AEADBadTagException 异常

标签 java encryption aes-gcm

我正在尝试使用 AES/GCM/NoPadding 在 Java8 中进行加密。但我无法弄清楚为什么在解密时出现 AEADBadTagException。

这是我的代码:

private final int GCM_IV_LENGTH = 12;
private final int GCM_TAG_LENGTH = 16;

private static String encrypt(String privateString, SecretKey skey) {
    byte[] iv = new byte[GCM_IV_LENGTH];
    (new SecureRandom()).nextBytes(iv);

    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
    cipher.init(Cipher.ENCRYPT_MODE, skey, ivSpec);

    byte[] ciphertext = cipher.doFinal(privateString.getBytes("UTF8"));
    byte[] encrypted = new byte[iv.length + ciphertext.length];
    System.arraycopy(iv, 0, encrypted, 0, iv.length);
    System.arraycopy(ciphertext, 0, encrypted, iv.length, ciphertext.length);

    Base64Encoder encoder = new Base64Encoder();
    String encoded = encoder.encode(encrypted);

    return encoded;
}

private static String decrypt(String encrypted, SecretKey skey) {
    Base64Decoder decoder = new Base64Decoder();
    String decoded = encoder.encode(encrypted);

    byte[] iv = Arrays.copyOfRange(decoded, 0, GCM_IV_LENGTH);

    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
    cipher.init(Cipher.DECRYPT_MODE, skey, ivSpec);

    byte[] ciphertext = cipher.doFinal(decoded, GCM_IV_LENGTH, decoded.length - GCM_IV_LENGTH);

    String newString = new String(ciphertext, "UTF8");

    return newString;
}

希望有人能帮我解决这个异常。谢谢!

最佳答案

我纠正了一些拼写错误,并使用了 Java 8 的 base64 实用程序,它似乎对我来说工作正常。这是我的版本,您可以将其与您的进行比较。

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

public class Main {
    private final static int GCM_IV_LENGTH = 12;
    private final static int GCM_TAG_LENGTH = 16;

    private static String encrypt(String privateString, SecretKey skey) throws Exception {
        byte[] iv = new byte[GCM_IV_LENGTH];
        (new SecureRandom()).nextBytes(iv);

        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
        cipher.init(Cipher.ENCRYPT_MODE, skey, ivSpec);

        byte[] ciphertext = cipher.doFinal(privateString.getBytes("UTF8"));
        byte[] encrypted = new byte[iv.length + ciphertext.length];
        System.arraycopy(iv, 0, encrypted, 0, iv.length);
        System.arraycopy(ciphertext, 0, encrypted, iv.length, ciphertext.length);

        String encoded = Base64.getEncoder().encodeToString(encrypted);

        return encoded;
    }

    private static String decrypt(String encrypted, SecretKey skey) throws Exception {
        byte[] decoded = Base64.getDecoder().decode(encrypted);

        byte[] iv = Arrays.copyOfRange(decoded, 0, GCM_IV_LENGTH);

        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
        cipher.init(Cipher.DECRYPT_MODE, skey, ivSpec);

        byte[] ciphertext = cipher.doFinal(decoded, GCM_IV_LENGTH, decoded.length - GCM_IV_LENGTH);

        String result = new String(ciphertext, "UTF8");

        return result;
    }

    public static void main(String[] args) throws Exception {
        SecretKey key = new SecretKeySpec(new byte[16], "AES"); // key is 16 zero bytes
        String s = decrypt(encrypt("This is the first string to test", key), key);
        System.out.println(s);
    }
}

关于java - AES/GCM/NoPadding AEADBadTagException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46153425/

相关文章:

java - 启用 largeHeap 导致 OOM 时,应用程序中未触发垃圾收集

encryption - 使用 GPU 进行加密硬件加速

c - 如何在 GCM 模式下使用 AES 链接 BCryptEncrypt 和 BCryptDecrypt 调用?

android - Android、AES-GCM 或纯 AES 上的数据加密?

java - 如何在 Java 中匹配一串元组?

java - 为什么实例方法引用适用于不匹配的签名?

java - 使用什么库来构建 HTML 文档?

java - RC4加密java

c++ - 使用命令行提示的 Vigenere 密码

java - 在 Node js 中通过 AES/GCM/NoPadding 算法使用 key 和 iv 加密有效负载并在 java 中解密