我写了一个简单的加密和解密辅助类为我的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;
}
有你的代码的几个问题:
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] 删除。
我来说两句