【问题标题】:javax.crypto.AEADBadTagException: Tag mismatch! Error when encrypting Stringjavax.crypto.AEADBadTagException:标签不匹配!加密字符串时出错
【发布时间】:2018-11-15 13:27:24
【问题描述】:

我为我的 android 应用程序编写了一个简单的加密和解密帮助程序类来安全地加密和存储字符串。

它包含一个单独的静态公共方法来加密,然后它调用一个私有静态方法来解密加密的消息并返回它。我这样写方法是为了检查加密/解密后消息是否完好。

我用一个字符串编写了一个简单的 JUnit 测试,并在将它发送到 Crypto 加密方法之前和之后对该字符串调用了 AssertEquals。

我在运行测试时收到以下错误:

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)
at util.Crypto.decrypt(Crypto.java:94)
at util.Crypto.encrypt(Crypto.java:64)
at com.example.ali.meappley.CryptoTest.encryptAndDecryptTest(CryptoTest.java:29)

我是密码学新手,但我阅读了不同的 stackoverflow 回复,但找不到任何帮助。一些用户建议在调用cipher.doFinal(someByteArray) 之前先调用cipher.update(someByteArray),但我无法让它工作。有什么建议吗?

这是我的助手类

public class Crypto {

//public methods

//public static encrypt method
public static String encrypt(String messageToEncrypt, @Nullable byte[] associatedData) throws NoSuchPaddingException,
        NoSuchAlgorithmException,
        InvalidAlgorithmParameterException,
        InvalidKeyException,
        BadPaddingException,
        IllegalBlockSizeException {

    byte[] plainBytes = messageToEncrypt.getBytes();
/////////////////////////////////////////////////////////////////
    SecureRandom secureRandom = new SecureRandom();
    byte[] key = new byte[16];
    secureRandom.nextBytes(key);
    SecretKey secretKey = new SecretKeySpec(key, "AES");

    byte[] iv = new byte[12]; //NEVER REUSE THIS IV WITH SAME KEY
    secureRandom.nextBytes(iv);

    final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv); //128 bit auth tag length
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);

    if (associatedData != null) {
        cipher.updateAAD(associatedData);
    }

    byte[] cipherText = cipher.doFinal(plainBytes);

    ByteBuffer byteBuffer = ByteBuffer.allocate(4 + iv.length + cipherText.length);
    byteBuffer.putInt(iv.length);
    byteBuffer.put(iv);
    byteBuffer.put(cipherText);
    byte[] cipherMessage = byteBuffer.array();

    Arrays.fill(key,(byte) 0); //overwrite the content of key with zeros
    ///////////////////////////////////////////////////////////////////

    byte[] decrypted = decrypt(cipherMessage, null, key);

    return decrypted.toString();
}

//public static decrypt method
private static byte[] decrypt(byte[] cipherMessage, @Nullable byte[] associatedData, byte[] key) throws NoSuchPaddingException,
        NoSuchAlgorithmException,
        InvalidAlgorithmParameterException,
        InvalidKeyException,
        BadPaddingException,
        IllegalBlockSizeException {

    ByteBuffer byteBuffer = ByteBuffer.wrap(cipherMessage);
    int ivLength = byteBuffer.getInt();
    if(ivLength < 12 || ivLength >= 16) { // check input parameter
        throw new IllegalArgumentException("invalid iv length");
    }
    byte[] iv = new byte[ivLength];
    byteBuffer.get(iv);
    byte[] cipherText = new byte[byteBuffer.remaining()];
    byteBuffer.get(cipherText);

    final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(128, iv));
    if (associatedData != null) {
        cipher.updateAAD(associatedData);
    }

    cipher.update(cipherText);
    byte[] plainText= cipher.doFinal(cipherText);

    return plainText;
}

【问题讨论】:

    标签: java encryption cryptography


    【解决方案1】:

    您的代码存在一些问题:

    1) 在您的加密方法中删除以下行(或将其移到解密调用后面)。

     Arrays.fill(key, (byte) 0); // overwrite the content of key with zeros
    

    否则加解密密钥不同。

    2) 在您的加密方法中,还在您的解密调用中传递关联数据,即替换

     byte[] decrypted = decrypt(cipherMessage, null, key);
    

     byte[] decrypted = decrypt(cipherMessage, associatedData, key);
    

    为加密和解密传递的相关数据必须匹配有效性。对于相关数据的目的,请参见例如https://crypto.stackexchange.com/questions/6711/how-to-use-gcm-mode-and-associated-data-properly

    3) 在您的解密方法中删除该行

     cipher.update(cipherText);
    

    有关更新方法的目的,请参见例如What does cipher.update do in java?

    所有三个问题都会导致 AEADBadTagException。

    4) 我怀疑出于测试目的,您的 encrypt-method 返回decrypted.toString() 但它只为您提供对象的类和哈希码。返回例如更有意义新字符串(已解密)。

    【讨论】:

    • 非常感谢您提供详细答案和链接的回复。它有帮助,我学到了一些新东西。似乎该类现在工作正常,并且您使用 new String(decrypt) 是正确的。再次感谢!
    • 嗨@Topaco 比你更适合解决方案,但是当我们尝试从 Android 到 Ios 时,会引发 AEADBadTagException 异常。仅供参考 Android => Android 工作正常。
    • @SathishGadde - 无法根据这几行注释进行分析。请在 SO 上发布一个包含所有所需信息的问题。有需要的可以参考这个答案。
    • 当然@Topaco,感谢您的快速回复。我会这样做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2014-04-26
    • 2012-08-16
    • 1970-01-01
    • 2011-02-04
    • 2015-07-10
    相关资源
    最近更新 更多