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

标签 ios swift swift-playground

我想在 Playground 上加密一条消息。我的代码如下。我编写另一个类来生成 key 。当将 key 转换为字符串时,出现错误。

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("") {
        $0 + String(format: "%02x", digest[$1])
    }
}



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’不符合预期类型‘序列’”错误。请帮助我。

最佳答案

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

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

关于ios - 参数类型 'SecretSpec' 不符合预期类型 'Sequence',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56338550/

相关文章:

ios - 具有自定义对象原始值的 Swift 枚举

ios - 当我点击表格 View 中的任何单元格时,单元格不会替换

ios - 使用 Swift 4 和 UIView 的奇怪动画行为

ios - iOS 中的 XIB 状态栏不可见 - 为什么?

swift - .playground 是一个 swift 文件吗?谁能 'see'呢?

ios - 是否可以使用脚本在Xcode中引发错误?

ios - CoreData 模型选项选择树

swift - 从 Swift (3.1) TableView 中动态创建的单元格获取有序文本字段数据

ios - 在 iOS Swift 项目中从 "2015-01-17T20:00Z"创建 NSDate

Xcode:评论中的链接可以点击吗?