0

I am working on iOS and flutter application together. There is encryption of data happening on both the sides. Below is the iOS encryption code which is already live,

func encryption(encryptionKey: String) -> String{
        if(self.isEmpty){
            return ""
        }else{
            let key = encryptionKey
            
            let dataBytes : [UInt8] = Array(self.utf8)
            let keyBytes : [UInt8] = Array(key.utf8)
            
            do {
                let encryptedData = try AES(key: keyBytes, blockMode: ECB(), padding: .pkcs7).encrypt(dataBytes)
                let encodedString = Data(encryptedData).base64EncodedString()
                return encodedString
            } catch let error {
                print(error)
                return ""
            }
        }

Below is the flutter encryption code that I am working on now (Using encrypt.dart package),

final key = Key.fromBase64("Some_Key");
final iv = IV.fromLength(16));

final encrypter = Encrypter(AES(key, mode: AESMode.ecb, padding: 'PKCS7'));
final encrypted = encrypter.encrypt(someString, iv: iv); //IV is ignored in ECB mode

The issue here is the encrypted string that I am getting in flutter must be the same as iOS which is not the case. Could someone help me out in getting a compatible encryption version in flutter? Kindly help...

Developer
  • 31
  • 1
  • 8
  • have a look at my answer at :https://stackoverflow.com/questions/61332076/cross-platform-aes-encryption-between-ios-and-kotlin-java-using-apples-cryptokit/61345165#61345165 it may help in solving your issue. – workingdog support Ukraine Aug 06 '21 at 09:51
  • In Kotlin, how do you encode the data to a string? Or in other words, how exactly do you represent your PKCS7? – CouchDeveloper Aug 06 '21 at 10:22

1 Answers1

0

I finally resolved it myself. Posting the answer over here hoping it helps someone.

I had to change the below line,

from

final key = Key.fromBase64("Some_Key");

to

final key = Key.fromUtf8("Some_Key");

That't it. It works!!

Developer
  • 31
  • 1
  • 8