I encrypt data in android phone using AES GCM mode and sent it to java application in windows The cipher text is created and received successfully at decryption process an exception appeared(Tag miss match)
I tried to remove associated data update I tried to set the provider to SunJCE
The code works great between java desktop applications but the error appears when use encryption decryption process between android and java
I use openjdk1.8
public static byte[] gcmMode(int gcmCipherMode, byte[] aadHeader,char[] password, byte[] inputBytes) {
byte[] outputBytes = new byte[0];
byte[] salt;
byte[] iv;
byte[] res;
SecretKeySpec ks;
try {
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
switch (gcmCipherMode) {
case Cipher.ENCRYPT_MODE:
salt = new byte[16]; random.nextBytes(salt);
iv = new byte[12]; random.nextBytes(iv);
res = salt(password, salt);
ks = new SecretKeySpec(res, "AES");
c.init(Cipher.ENCRYPT_MODE, ks, new GCMParameterSpec(128, iv));
if (aadHeader != null) {
c.updateAAD(aadHeader);
}
outputBytes = arrayByteConcatenate(salt, iv);
outputBytes = arrayByteConcatenate(outputBytes, c.doFinal(inputBytes));
break;
case Cipher.DECRYPT_MODE:
if (inputBytes.length > 44) {
salt = arrayByteSplit(inputBytes, 0, 16);
iv = arrayByteSplit(inputBytes, 16, 28);
res = salt(password, salt);
ks = new SecretKeySpec(res, "AES");
c.init(Cipher.DECRYPT_MODE, ks, new GCMParameterSpec(128, iv));
if (aadHeader != null) {
c.updateAAD(aadHeader);
}
// Return our Decrypted String
outputBytes = c.doFinal(arrayByteSplit(inputBytes, 28, inputBytes.length));
}else{
System.out.println("Wrong cipher");
}
break;
default:
System.out.println("UnKnown");
break;
}
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | IllegalBlockSizeException
| BadPaddingException | InvalidParameterException
| InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return outputBytes;
}
static byte[] salt (char[] password, byte[] salt) {
SecretKeyFactory skf = null;
byte[] res = new byte[0];
try {
skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec spec = new PBEKeySpec(password, salt, 100000, 128);
SecretKey key = skf.generateSecret(spec);
res = key.getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
}
return res;
}
Error
javax.crypto.AEADBadTagException: Tag mismatch!
at com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:578)
at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1116)
at com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1053)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
at algorithm.encryption.AESencryption.gcmMode(AESencryption.java:91)
at ServerWorker.handleAuthentication(ServerWorker.java:97)
at ServerWorker.startMessageReader(ServerWorker.java:67)
at ServerWorker.handleClientSocket(ServerWorker.java:44)
at ServerWorker.run(ServerWorker.java:36)