简体   繁体   English

参数类型'SecretSpec'不符合预期类型'序列'

[英]Argument type 'SecretSpec' does not conform to expected type 'Sequence'

I want to encrypt a message on playground. 我想在操场上加密消息。 my code is given below. 我的代码如下。 i write another class to generate a secretkey. 我写另一个类来生成一个密钥。 when convert the secret key into string, it gives error. 将密钥转换为字符串时,会出错。

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

I want to get String value of 'skc'. 我想获得'skc'的String值。 and print it. 然后打印出来。 but it gives 'Argument type 'SecretSpec' does not conform to expected type 'Sequence'' error. 但它给'参数类型'SecretSpec'不符合预期类型'序列''错误。 please help me. 请帮我。

Data does not have an initialiser for your custom type. Data没有自定义类型的初始化程序。 Did you mean to get the encoded value? 你的意思是获得编码值吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数类型与预期类型不符 - Argument type does not conform to expected type 参数类型不符合预期的类型 MKAnnotation - Argument type does not conform to expected type MKAnnotation TKChartDataPoint不符合预期的类型序列 - TKChartDataPoint does not conform to expected type Sequence 带有 Swift 4/Xcode 10 的 Facebook SDK:参数类型“SDKLoggingBehavior?” 不符合预期的类型“序列” - Facebook SDK with Swift 4/Xcode 10: Argument type 'SDKLoggingBehavior?' does not conform to expected type 'Sequence' 参数类型&#39;范围 <Int> &#39;不符合预期类型&#39;序列&#39;Swift3 - Argument type 'Range<Int>' does not conform to expected type 'Sequence' Swift3 参数类型&#39;customClass.Type&#39;不符合预期类型&#39;NSItemProviderWriting&#39; - Argument type 'customClass.Type' does not conform to expected type 'NSItemProviderWriting' 参数类型&#39;String&#39;不符合期望类型&#39;Sequence&#39; - Argument type 'String' does not conform to expect type 'Sequence' 参数类型“ AnyObject”不符合预期的类型NSCopying - Argument Type 'AnyObject' does not conform to expected type NSCopying 参数类型“字符串?” 不符合预期的类型“StringProtocol” - Argument type 'String?' does not conform to expected type 'StringProtocol' 参数类型'[String?]'不符合预期类型'AnyObject' - Argument type '[String?]' does not conform to expected type 'AnyObject'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM