I am trying to implement AES/GCM/NoPadding encryption in flutter. I have successfully implemented it in JAVA but when I tried to decrypt it in flutter, I got no success.
I have tried writing decryption code in dart but I got Unhandled Exception: SecretBox has wrong message authentication code (MAC)
My Java Code
public class GCMEncryption {
public static final int AES_KEY_SIZE = 128;
public static final int GCM_IV_LENGTH = 12;
public static final int GCM_TAG_LENGTH = 16;
public String getEncryptedText(String plainText) {
try {
byte[] IV = new byte[GCM_IV_LENGTH];
SecureRandom random = new SecureRandom();
random.nextBytes(IV);
String iv = Base64.getEncoder().encodeToString(IV);
System.out.println("IV : "+iv);
byte[] cipherText = encrypt(plainText.getBytes(), getKeySpec(), IV);
String text = Base64.getEncoder().encodeToString(cipherText);
text = iv+text; // Concating iv and encrypted text together
return text;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public String getDecryptedText(String cipherText) {
try {
// Splitting IV and Encrypted text
String iv = cipherText.substring(0,16);
System.out.println("IV : "+iv);
byte[] IV = Base64.getDecoder().decode(iv);
cipherText = cipherText.substring(16);
byte[] data = Base64.getDecoder().decode(cipherText);
return decrypt(data, getKeySpec(), IV);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
private SecretKeySpec getKeySpec() {
SecretKeySpec spec = null;
try {
byte[] bytes = new byte[32];
String pwd = "Test!ng012345678"; //Temporary
bytes = pwd.getBytes();
spec = new SecretKeySpec(bytes, "AES");
return spec;
} catch (Exception e) {
e.printStackTrace();
}
return spec;
}
private byte[] encrypt(byte[] plaintext, SecretKey key, byte[] IV) throws Exception {
// Get Cipher Instance
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
// Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
// Create GCMParameterSpec
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, IV);
// Initialize Cipher for ENCRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);
// Perform Encryption
byte[] cipherText = cipher.doFinal(plaintext);
return cipherText;
}
private String decrypt(byte[] cipherText, SecretKey key, byte[] IV) throws Exception {
// Get Cipher Instance
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
// Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
// Create GCMParameterSpec
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, IV);
// Initialize Cipher for DECRYPT_MODE
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
// Perform Decryption
byte[] decryptedText = cipher.doFinal(cipherText);
return new String(decryptedText);
}
}
My Dart Code
Future<String> decrypt(String textBlock) async {
Uint8List data = base64.decode("4NA09I5VpmdD0o1k3VP8eIfyZRKDtzOwgJv5nh0nmEPZ/Q==");
Uint8List passphrase = utf8.encode('Test!ng012345678');
SecretKey secretKey = new SecretKey(passphrase);
Uint8List iv = utf8.encode('/U0OI/AdHDM4QFVC');
SecretBox secretBox = new SecretBox(data, nonce: iv);
List<int> decrypted = await AesGcm.with128bits().decrypt(secretBox, secretKey: secretKey);
String dec = utf8.decode(decrypted);
print("DATA : "+dec);
return dec;
}
For this I got below error in Flutter
Unhandled Exception: SecretBox has wrong message authentication code (MAC) #0 DartAesGcm.decryptSync (package:cryptography/src/dart/aes_gcm.dart:112:7) #1 DartAesGcm.decrypt (package:cryptography/src/dart/aes_gcm.dart:58:12) #2 EncryptionHandler.decrypt (package:investment_app/encryption/encryption_handler.dart:45:27)
Can you please help me to solve this. Sample code will be helpful.