Search code examples
iosswiftencryptionaescryptoswift

iOS 13 AES decrypt with Crypto Swift not working


I have an app which uses AES256 decryption for authentication. For this I use the Crypto Swift library.

Until now it always worked, but with iOS 13 coming out, the decryption does not work.

Here's the code I use for decryption:

func aesDecrypt(key: String) throws -> String {
    let data = Serializer.hexaToBytes(self)
    let key = Serializer.hexaToBytes(key)
    let decrypted = try! AES(key: key, blockMode: ECB(), padding: .pkcs7).decrypt(data)
    let decryptedData = Data(decrypted)
    return String(bytes: decryptedData.bytes, encoding: .utf8) ?? "Could not decrypt"
}

String(bytes: decryptedData.bytes, encoding: .utf8) always returns nil...

Why could that be and what should I do to make it working again?

Any help would be appreciated :)


Solution

  • The most common cause of these kinds of problems is that somewhere you're using Data.description to compute a hex string. That was never correct, but it was possible to get away with it prior to iOS 13. In iOS 13, the format was changed (the format was never promised to stay the same), and that's broken lots of things that relied on it being stable.

    The next thing I'd check is Serializer.hexaToBytes to make sure it's correctly implemented and returning what you expect.

    Given that this particular function is written in way that doesn't carefully check for errors (and uses a very dangerous mode like ECB), it likely that related functions are similarly uncareful with errors. Check each to see if it's returning the values you're expecting.