javax.crypto.AEADBadTagException:标签不匹配!加密字符串时出错

卡尔顿:

我写了一个简单的加密和解密辅助类为我的Android应用程序存储并加密字符串安全。

它由一个单一的静态公共方法进行加密,然后调用一个私有静态方法来解密加密的消息,并返回它。我写的方法,这种方法来检查,如果消息是加密/解密后完好无损。

我之前并将其发送到加密的加密方法之后写了一个String对String一个简单的JUnit测试,并呼吁的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)

我是新来的密码,但我读了不同的计算器答复,并不能找到什么帮助。更有网友提出调用cipher.update(someByteArray)之前调用cipher.doFinal(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;
}
Topaco:

有你的代码的几个问题:

1)在您的加密法除去下面的行(或移位它的解密 - 呼叫后面)。

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

否则,对于加密和解密的密钥不同。

2)在您的加密法也通过associatedData在解密呼叫如更换

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

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

该associatedData通过加密和解密必须匹配的有效性。对于associatedData的目的,例如见https://crypto.stackexchange.com/questions/6711/how-to-use-gcm-mode-and-associated-data-properly

3)在您的解密 - 方法除去线

 cipher.update(cipherText);

对于更新方法的目的例如见什么cipher.update在java中吗?

所有这三个问题引起的AEADBadTagException。

4)我怀疑用于测试目的的加密方法会返回decrypted.toString(),它可是只给你对象的类和hashCode。它会更有意义返回例如新的String(解密)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Java AES GCM javax.crypto.AEADBadTagException:标记不匹配

javax.crypto.AEADBadTagException - AES/GCM/NoPadding 工作,然后不

解密时出现 javax.crypto.IllegalBlockSizeException

javax.crypto.BadPaddingException

AES加密期间出现javax.crypto.BadPaddingException错误

javax.crypto.IllegalBlockSizeException:解密中的最后一个块不完整-解密加密的AES字符串

解密时出现错误javax.crypto.IllegalBlockSizeException:

javax.crypto.BadPaddingException:解密错误:仅从数据库解密到一个已经加密的字符串时

javax.crypto.BadPaddingException:当我尝试使用私钥解密RSA字符串时发生解密错误

javax.crypto.BadPaddingException:解密错误使用Java RSA加密时,

解密RSA数据Java时出错:javax.crypto.BadPaddingException:解密错误

使用SSL连接到Tibco EMS时出现javax.crypto.BadPaddingException

获取javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须是16的倍数吗?

javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须是16的倍数

RSA解密期间的javax.crypto.BadPaddingException

javax.crypto.Cipher有多安全?

javax.crypto.BadPaddingException:解密错误

Java RSA实现:javax.crypto.BadPaddingException

如何使用 Bouncy Castle ElGamal 和 javax.crypto.Cipher 使加密 (c1, c2) 元组显式

不断获取javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须为16的倍数

javax.crypto库中AAD的GCM模式

javax.crypto.spec.SecretKeySpec线程安全吗?

javax.crypto.Cipher对RSA使用哪种填充

在哪里可以找到javax.crypto源代码?

Python与Kotlin AES通信(javax.crypto.BadPaddingException)

javax.crypto.BadPaddingException:填充块损坏的异常

JavaScript中用于javax.crypto的备用代码

我有javax.crypto.BadPaddingException:RSA密码术

执行 PBKDF2WithHmacSHA256 + AES/GCM/NoPadding 抛出的最小示例:javax.crypto.AEADBadTagException:GCM 中的 mac 检查失败