I’ve a requirement the place a string can be encrypted from flutter net app utilizing Dart. The encrypted string goes to server and the identical can be consumed by iOS Swift.
Dart file
String encryptString(Uint8List key) {
attempt {
// Convert the important thing bytes into an encryption key
ultimate encryptionKey = Key(key);
// Generate a random IV (Initialization Vector)
ultimate iv = IV.fromSecureRandom(16); // 16 bytes IV for AES-GCM
ultimate macValue = Uint8List.fromList(utf8.encode("take a look at"));
// Create an AES encrypter with the important thing
ultimate encrypter = Encrypter(AES(encryptionKey));
// Encrypt the plaintext utilizing AES-GCM
ultimate encrypted = encrypter.encrypt(
this,
iv: iv,
associatedData: macValue
);
// Concatenate IV and ciphertext, then encode to base64
ultimate concatenatedBytes = Uint8List.fromList([...iv.bytes, ...encrypted.bytes, ...utf8.encode("test")]);
ultimate base64Encoded = base64.encode(concatenatedBytes);
//ultimate mixed = iv.bytes + encrypted.bytes;
return (base64Encoded);
} catch (e) {
print('Error whereas encrypting: $e');
return '';
}
}
Nonetheless the encrypted textual content I’m not in a position to decrypt in Swift. I all the time get CryptoKit.CryptoKitError.authenticationFailure
Swift code
func decrypt(withKey key: SymmetricKey) throws -> String {
guard let encryptedData = Knowledge(base64Encoded: self) else { throw EncryptionError.invalidData }
// Open the sealed field utilizing the encryption key
do {
let sealedBox = attempt AES.GCM.SealedBox(mixed: encryptedData)
// Decrypt the info
let decryptedData = attempt AES.GCM.open(sealedBox, utilizing: key)
// Convert decrypted information to string
guard let decryptedString = String(information: decryptedData, encoding: .utf8) else {
throw EncryptionError.decryptionFailed
}
return decryptedString
} catch {
print(error.localizedDescription)
throw EncryptionError.invalidData
}
}
I attempted the above snippets. Appears to not work, Please assist 🙁