2

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"
    }

}
jww
  • 97,681
  • 90
  • 411
  • 885
PhilBot
  • 748
  • 18
  • 85
  • 173
  • Please check https://github.com/krzyzanowskim/CryptoSwift#swift-versions-support whether you are using correct version. – Vini App Sep 25 '17 at 07:02

1 Answers1

7

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().

sCha
  • 1,454
  • 1
  • 12
  • 22