参数类型 'SecretSpec' 不符合预期类型 'Sequence'

Argument type 'SecretSpec' does not conform to expected type 'Sequence'

我想在 playground 上加密一条消息。我的代码如下。我写另一个 class 来生成一个密钥。将秘钥转成字符串时报错

import UIKit

import Foundation
import CommonCrypto



class SecretSpec {
    var algorithm: String = "AES/ECB/PKCS5padding"
    var key = [UInt8]()


    func SecretSpec(key: [UInt8], algorithm: String){
        self.key = key
        self.algorithm = algorithm
    }

    func getAlgorithm() -> String {
        return self.algorithm
    }

    func getFormat() -> String {
        return "RAW"
    }

    func getEncoded() -> [UInt8] {
        return self.key
    }



    func hashCode() -> Int {
        var retval: Int = 0
        for i in 1...key.count-1 {
            retval = retval + Int(key[i]) * Int(i)
        }

        if (algorithm.lowercased() == "tripledes"){
            retval = retval ^ Int("desede".hashValue)
            return retval
        }else{
            retval = retval ^ Int(algorithm.lowercased().hashValue)
            return retval
        }
    }
}



extension String {

    func aesEncrypt(key: String, options:Int = (kCCOptionECBMode + kCCOptionPKCS7Padding)) -> String? {
        if let keyData = key.data(using: String.Encoding.utf8),
            let data = self.data(using: String.Encoding.utf8),
            let cryptData = NSMutableData(length: Int((data.count)) + kCCBlockSizeAES128) {

            let keyLength = size_t(kCCKeySizeAES128)
            let operation: CCOperation = UInt32(kCCEncrypt)
            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,
                                      nil, (data as NSData).bytes, data.count,
                                      cryptData.mutableBytes, cryptData.length,
                                      &numBytesEncrypted)


            if UInt32(cryptStatus) == UInt32(kCCSuccess) {
                cryptData.length = Int(numBytesEncrypted)

                var bytes = [UInt8](repeating: 0, count: cryptData.length)
                cryptData.getBytes(&bytes, length: cryptData.length)

                var hexString = ""
                for byte in bytes {
                    hexString += String(format:"%02x", UInt8(byte))
                }

                return hexString
            }
            else {
                return nil
            }
        }
        return nil
    }
}



func MD5(_ string: String) -> String? {
    let length = Int(CC_MD5_DIGEST_LENGTH)
    var digest = [UInt8](repeating: 0, count: length)

    if let d = string.data(using: String.Encoding.utf8) {
        _ = d.withUnsafeBytes { (body: UnsafePointer<UInt8>) in
            CC_MD5(body, CC_LONG(d.count), &digest)
        }
    }


    return (0..<length).reduce("") {
        [=12=] + String(format: "%02x", digest[])
    }
}



var mdT = "YourStrongKeyasdfghjklzxcvbnm"
var algorithm: String = "AES/ECB/PKCS7padding"

var s = MD5(mdT)
let buf: [UInt8] = Array(s!.utf8)

var skcSpec = SecretSpec()
skcSpec.SecretSpec(key: buf, algorithm: algorithm)



print("hello: \(skcSpec)")


skcSpec.getEncoded()
skcSpec.getFormat()
skcSpec.getAlgorithm()
skcSpec.hashCode()


let msg: NSMutableDictionary = NSMutableDictionary()

msg.setValue("uttam kumar", forKey: "name")
msg.setValue("1001", forKey: "id")

let msgData: NSData
var msgStr: String = ""
var requestUrl: String = ""

do {
    msgData = try JSONSerialization.data(withJSONObject: msg, options: JSONSerialization.WritingOptions()) as NSData
    msgStr = NSString(data: msgData as Data, encoding: String.Encoding.utf8.rawValue)! as String

} catch _ {
    print ("JSON Failure")
}


var skc = String(data: Data(skcSpec), encoding: .utf8)!
var encoded = msgStr.aesEncrypt(key: String(skc))!

print("encoded: \(encoded)")

我想获取 'skc' 的字符串值。并打印出来。但它给出了“参数类型 'SecretSpec' 不符合预期类型 'Sequence'”错误。请帮助我。

Data 没有您的自定义类型的初始化程序。您是要获取编码值吗?

var skc = String(data: Data(skcSpec.getEncoded()), encoding: .utf8)!