&#39; UnsafePointer <UINT8>&#39;不能转换为&#39; UnsafePointer&lt; _&gt;&#39;

时间:2018-06-07 13:28:59

标签: ios iphone swift tripledes

我正在进行tripleDES加密和解密。得到此错误:

UnsafePointer<UInt8>' is not convertible to 'UnsafePointer<_>

我收到错误的代码是:

        let keyString        = "25d1d4cb0a08403e2acbcbe0"
        let keyData = keyString.data(using: .utf8)!
        let message       = pass
        let data = message.data(using: .utf8)!
        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 = keyData.withUnsafeBytes { (keyBytes: UnsafePointer<UInt8>) in
            data.withUnsafeBytes { (dataBytes: UnsafePointer<UInt8>) in
                cryptData.withUnsafeMutableBytes { (cryptBytes: UnsafeMutablePointer<UInt8>) in
                    CCCrypt(operation,
                            algoritm,
                            options,
                            keyBytes,
                            keyLength,
                            nil,
                            dataBytes,
                            data.count,
                            cryptBytes,
                            cryptData.count,
                            &numBytesEncrypted)
                }
            }
        }

任何人都可以提供帮助吗?

3 个答案:

答案 0 :(得分:0)

此错误消息是某种编译器错误的结果:编译器无法编译代码,并发出无效且具有误导性的错误消息。另请参见https://bugs.swift.org/browse/SR-5931

在大多数情况下,您可以:

  • 删除类型注释(<UInt8>)以揭示真正原因
  • 解决原因
  • 再次添加类型注释

答案 1 :(得分:0)

    let keyString        = "25d1d4cb0a08403e2acbcbe0"
    let keyData = keyString.data(using: .utf8)!
    let message       = pass
    let data = message.data(using: .utf8)!
    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 = keyData.withUnsafeBytes { (keyBytes: UnsafePointer<UInt8>) in
        data.withUnsafeBytes { (dataBytes: UnsafePointer<UInt8>) in
            cryptData.withUnsafeMutableBytes { (cryptBytes: UnsafeMutablePointer<UInt8>) in
                CCCrypt(operation,
                        algoritm,
                        options,
                        keyBytes,
                        keyLength,
                        nil,
                        dataBytes,
                        data.count,
                        &cryptBytes, //<-----try to do this
                        cryptData.count,
                        &numBytesEncrypted)
            }
        }
    }

答案 2 :(得分:-1)

cryptData是一个NSMutableData对象,因此可以使用mutableBytes

怎么样:

    let keyString        = "25d1d4cb0a08403e2acbcbe0"
    let keyData = keyString.data(using: .utf8)!
    let message       = pass
    let data = message.data(using: .utf8)!
    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 = keyData.withUnsafeBytes { (keyBytes : UnsafePointer<UInt8>) in
        data.withUnsafeBytes { (dataBytes : UnsafePointer<UInt8>) in

            CCCrypt(operation,
                    algoritm,
                    options,
                    keyBytes,
                    keyLength,
                    nil,
                    dataBytes,
                    data.count,
                    cryptData.mutableBytes,
                    cryptData.length,
                    &numBytesEncrypted)

        }
    }

    if UInt32(cryptStatus) == UInt32(kCCSuccess) {
        print("success!")
    }