简体   繁体   中英

CryptoSwift with Xcode 9 and AES Decryption

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

在此输入图像描述

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM