解密方法不能迅速工作

时间:2018-03-21 11:01:45

标签: swift swift3 swift4

我有加密和解密方法,加密方法工作正常用c#加密和解密方法测试,在android也工作正常但在swift解密方法不工作它显示我null 我尝试多次但是没有成功,任何人帮我解决这个问题高度赞赏,因为。我是swift的新手

PlainString =“5lg3yg7Q + Y8 =”

func tripleDesDecrypt(PlainString :String) -> String? {
       let md5Data = "FIFEDCBA98765432104AABFD"       
        if let keyData = md5Data.data(using: String.Encoding.utf8),
            let data = PlainString.data(using: .utf8),
            let cryptData    = NSMutableData(length: Int(data.count) + kCCBlockSize3DES)  {
            let keyLength              = size_t(kCCKeySize3DES)
            let operation: CCOperation = UInt32(kCCDecrypt)
            let algoritm:  CCAlgorithm = UInt32(kCCAlgorithm3DES)
            let options:   CCOptions   = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)
            var numBytesEncrypted :size_t = 0
            let cryptStatus = CCCrypt(operation,
                                      algoritm,
                                      options,
                                      (keyData as NSData).bytes, keyLength,
                                      nil,
                                      data.bytes, data.count,
                                      cryptData.mutableBytes, cryptData.length,
                                      &numBytesEncrypted)
            if UInt32(cryptStatus) == UInt32(kCCSuccess) {
                cryptData.length = Int(numBytesEncrypted)
                let unencryptedMessage = String(data: cryptData as Data, encoding:String.Encoding.utf8)
                return unencryptedMessage
            }
            else {
                            return nil
            }
        }

        return nil
    }


func tripleDesEncrypt(PlainString: String) -> String{

   let md5Data = "FIFEDCBA98765432104AABFD"
 //   let md5Data = MD5(string:keyvalue)
    let PlainStringvar       = PlainString

    guard let data = PlainStringvar.data(using: .utf8) else {
        return ""
    }
    let cryptData    = NSMutableData(length: Int(data.count) + kCCBlockSize3DES)!

    let keyLength              = size_t(kCCKeySize3DES)
    let operation: CCOperation = UInt32(kCCEncrypt)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithm3DES)
    let options:   CCOptions   = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)

    var numBytesEncrypted :size_t = 0

    let cryptStatus = CCCrypt(operation,
                              algoritm,
                              options,
                              md5Data.bytes, keyLength,
                              nil,
                              data.bytes, data.count,
                              cryptData.mutableBytes, cryptData.length,
                              &numBytesEncrypted)

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

       var base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)


        return base64cryptString//// encodeString(str: base64cryptString)

    } else {
        print("Error: \(cryptStatus)")
    }
    return ""
}

1 个答案:

答案 0 :(得分:1)

您的解密函数中的数据和cryptData参数错误。 dataIn值即首先是加密数据,然后是dataOut值,即解密数据。它应该如下

let cryptStatus = CCCrypt(operation,
                          algoritm,
                          options,
                          (keyData as NSData).bytes, keyLength,
                          nil,
                          cryptData.bytes, cryptData.length,
                          data.mutableBytes, data.count,
                          &numBytesEncrypted)