【问题标题】:Java AES GCM javax.crypto.AEADBadTagException: Tag mismatchJava AES GCM javax.crypto.AEADBadTagException:标签不匹配
【发布时间】:2020-05-25 13:56:44
【问题描述】:

我不明白为什么我总是得到以下代码的异常:

public class AES {

    /*Some private vars (not shown)*/

    public AES(String secretKey, String initVector, String plainText, String cipherText, String addAuthData, String authTag) throws NoSuchAlgorithmException, NoSuchPaddingException {

        //Initialize test values
        this.cipher = Cipher.getInstance("AES/GCM/NoPadding");
        this.K = new SecretKeySpec(Dump.hexString2byteArray(secretKey), "AES");
        this.IV = Dump.hexString2byteArray(initVector);
        this.C = Dump.hexString2byteArray(cipherText);
        this.A = Dump.hexString2byteArray(addAuthData);
        this.T = Dump.hexString2byteArray(authTag);
        this.t = this.T.length*8;
        this.gcmIv = new GCMParameterSpec(t, IV);


    }
    public String testDecryption() throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

        cipher.init(Cipher.DECRYPT_MODE, K, gcmIv);

        cipher.updateAAD(A);
        cipher.init(Cipher.DECRYPT_MODE, K, gcmIv);
        cipher.doFinal(C);
        return "Return later some string";

   }

这是我如何运行代码的示例:

AES testCase4 = new AES(
            "feffe9928665731c6d6a8f9467308308", 
            "cafebabefacedbaddecaf888", 
            "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39", 
            "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091", 
            "feedfacedeadbeeffeedfacedeadbeefabaddad2", 
            "5bc94fbc3221a5db94fae95ae7121a47"
        );

        String TC4_p = testCase4.testDecryption();

有人可以解释一下吗? 错误来自 cipher.doFinal(C);

【问题讨论】:

  • 在这里检查这个问题:stackoverflow.com/questions/53621994/…。你得到的例外似乎很受欢迎。
  • 线程“主”javax.crypto.AEADBadTagException 中的异常:标签不匹配!
  • 在 java.base/com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:623) 在 java.base/com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore. java:1116) 在 java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1053) 在 java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)
  • 在 java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446) 在 java.base/javax.crypto.Cipher.doFinal(Cipher.java:2260)在 com.bernhard.lab4.AES.testDecryption(AES.java:123) 在 com.bernhard.lab4.Exc2.main(Exc2.java:140)

标签: java aes


【解决方案1】:

您的源代码中有 2 个问题。在本机 Java 中,身份验证标记(“authTag”)与密文(“C”)连接,因此您对类的初始化应该是

this.C = Dump.hexString2byteArray(cipherText + authTag);

其次,在 test.Decryption 中初始化密码两次,省略 second

cipher.init(Cipher.DECRYPT_MODE, K, gcmIv);

这样解密才能正常工作

"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39"

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2018-02-11
    • 2013-07-17
    • 2022-01-04
    • 2021-09-19
    相关资源
    最近更新 更多