0

I am using CommonCrypto with AES128/CBC/PKCS7Padding for encryption/decryption.

AES encryption in swift

Referred to the above link, code working fine for below iOS 13 version, but not working for iOS version 13 and above. Please suggest a working solution for iOS 13.Thanks in advance.

func aesDecrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String?
{

    if let keyData = key.data(using: String.Encoding.utf8),
        let data = NSData(base64Encoded: self, options: .ignoreUnknownCharacters),
        let cryptData    = NSMutableData(length: Int((data.length)) + kCCBlockSizeAES128)
    {
        let keyLength              = size_t(kCCKeySizeAES128)
        let operation: CCOperation = UInt32(kCCDecrypt)
        let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
        let options:   CCOptions   = UInt32(options)

        var numBytesEncrypted :size_t = 0

        let cryptStatus = CCCrypt(operation,
                                  algoritm,
                                  options,
                                  (keyData as NSData).bytes, keyLength,
                                  iv,
                                  data.bytes, data.length,
                                  cryptData.mutableBytes, cryptData.length,
                                  &numBytesEncrypted)

        if UInt32(cryptStatus) == UInt32(kCCSuccess) {
            cryptData.length = Int(numBytesEncrypted)
            let unencryptedMessage = String(data: cryptData as Data, encoding:String.Encoding.utf8)
            print("Decrypt Result unencryptedMessage:::",unencryptedMessage as Any)
            return unencryptedMessage
        }
        else {
            print("\(UInt32(cryptStatus))")
            return nil
        }
    }
    else {
        Logger.log(message: "Faild to decrypt the string", event: .e) // Error
        return nil
    }
}
Vasu
  • 886
  • 2
  • 11
  • 28
  • Can you add some more information? Like what's the code that you're using? What about it doesn't work? What have you done to try and fix it? Any errors you can share? – donnywals Nov 08 '19 at 12:10
  • @donnywals please see my edited question.code is working fine for below iOS version 13 but not working for iOS version 13.0 and above. – Vasu Nov 11 '19 at 04:58

0 Answers0