CryptoSwift with Xcode 9 and AES Decryption

6.8k Views Asked by At

I am using Xcode 9.0 and CryptoSwift (0.7.2). I am trying to extend String to decrypt an AES128 encrypted string. I've added CryptoSwift successfully with Pods but I get the following compilation error - what am I doing wrong?

'PKCS7' cannot be constructed because it has no accessible initializers

enter image description here

Here is the extension:

import Foundation
import CryptoSwift

extension String {

    // https://stackoverflow.com/questions/27072021/aes-encrypt-and-decrypt
    func aesDecrypt(key: String, iv: String) throws -> String {
        let data = Data(base64Encoded: self)!
        let decrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt([UInt8](data))
        let decryptedData = Data(decrypted)
        return String(bytes: decryptedData.bytes, encoding: .utf8) ?? "Could not decrypt"
    }

}
1

There are 1 best solutions below

3
On BEST ANSWER

I've checked out CryptoSwift's documentation, and I found a sample code:

let decrypted = try AES(key: key, iv: iv, blockMode: .CBC, padding: .pkcs7).decrypt(encrypted)

and I think it uses .pkcs7, instead of PKCS7().