1

I am encrypting a file in Swift with way (using Apple CryptoKit), in AES-256:

    let jsonData = try Data(contentsOf: jsonPath)
    let skey = SymmetricKey(size: .bits256)
    let dataSkey = skey.withUnsafeBytes {Data(Array($0)).base64EncodedString()}
    print(dataSkey) // DeDGtpqvojYL32bIsEsaaXZITjJp4bAW7aQgtVYARO0=
    let aes = try AES.GCM.seal(jsonData, using: skey)
    try aes.combined!.write(to: encryptedPath)
    // UPLOAD FILE (encryptedPath) TO FIREBASE STORAGEs

I am then trying to decrypt the file in Flutter (.dart) with this pub this way:

import 'package:steel_crypt/steel_crypt.dart';

final File tmpFile = File(//path of file downloaded from storage firebase);
var aesEncrypter = AesCrypt('DeDGtpqvojYL32bIsEsaaXZITjJp4bAW7aQgtVYARO0=', 'gcm', 'pkcs7');
String decrypted = aesEncrypter.decrypt(tmpFile.readAsStringSync());
print(decrypted);

But I always get this error:

"FileSystemException: Failed to decode data using encoding 'utf-8', path =path.file.json"

What am I doing wrong? I tried to encode the file from swift in base64 and read it again in flutter with base64 without success.

DanielZanchi
  • 2,708
  • 1
  • 25
  • 37
  • Your key looks like it's encoded in base64, so wouldn't you use the `Key.fromBase64` constructor? – Richard Heap May 19 '20 at 10:51
  • Also, doesn't CryptoKit only support GCM? You'll need a GCM implementation in Dart. – Richard Heap May 19 '20 at 11:17
  • thank you for the comments, I edited my code and now using GCM, but still that result – DanielZanchi May 19 '20 at 18:04
  • You're trying to read a CryptoKit sealed box as a string! Surely that's not going to work. Is the internal format of sealed box documented? Also, you aren't passing the IV to the decrypt method, but that's not the immediate problem. – Richard Heap May 19 '20 at 18:47
  • https://stackoverflow.com/questions/61332076/cross-platform-aes-encryption-between-ios-and-kotlin-java-using-apples-cryptokit seems to imply that the binary form of `combined` will be `nonce || ciphertext || tag`. If you aren't familiar with how CryptoKit works under the hood, you might want to use the older, lower-level Apple crypto apis where you are more likely to find interoperability examples. – Richard Heap May 19 '20 at 18:53
  • If I encrypt a json, containing a String, I should be able to decrypt it after and read it as a String. I tried the older one, but didn't find the GCM of it. – DanielZanchi May 19 '20 at 19:15
  • yes, but you are trying to read the binary encrypted sealed box as a string here: `tmpFile.readAsStringSync()`. Presumably the file contents is binary. – Richard Heap May 19 '20 at 19:23
  • thank you, again, for the reply. It's asking for a String as you can see here (line 97) https://github.com/AKushWarrior/steel_crypt/blob/master/lib/src/aes.dart – DanielZanchi May 19 '20 at 19:40
  • but it's expecting base64 encoded binary - because it immediately decodes it: `var localInput = base64.decode(encrypted);` Where are you separating the nonce from the ciphertext? – Richard Heap May 19 '20 at 20:00

0 Answers0