Swift 3中的tripleDES加密

时间:2017-05-18 13:26:08

标签: swift 3des tripledes

任何人都可以帮助我在Swift中为TripleDES解密提供教程或示例代码。我收到了3DES加密的Web服务响应数据,我需要通过我的应用程序解密并在应用程序中显示数据

我正在使用此代码段,但它对我不起作用:

func myDecrypt(decryptData : NSData) -> NSData?{\
    let mydata_len : Int = decryptData.length
    let keyData : NSData = (helperObject.decryptionKey as NSString).data(using: String.Encoding.utf8.rawValue)! as NSData

    let buffer_size : size_t = mydata_len+kCCBlockSizeAES128
    let buffer = UnsafeMutablePointer<NSData>.allocate(capacity: buffer_size)
    var num_bytes_encrypted : size_t = 0

    var _ : [UInt8] = [56, 101, 63, 23, 96, 182, 209, 205]  // I didn't use

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

    let decrypt_status : CCCryptorStatus = CCCrypt(operation, algoritm, options, keyData.bytes, keyLength, nil, decryptData.bytes, mydata_len, buffer, buffer_size, &num_bytes_encrypted)

    if UInt32(decrypt_status) == UInt32(kCCSuccess){\
        let myResult : NSData = NSData(bytes: buffer, length: num_bytes_encrypted)
        free(buffer)
        print("decrypt \(myResult)")

        let stringResult = NSString(data: myResult as Data, encoding:String.Encoding.utf8.rawValue)
        print(stringResult ?? "No string decription")
        return myResult
    }else{
        free(buffer)
        return nil\
    }
}

0 个答案:

没有答案