简体   繁体   中英

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

I'm doing tripleDES encryption and decryption. Getting this error:

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

The code where I'm getting the error is:

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

Anyone could help?

This error message is the result of a sort of compiler bug: the compiler cannot compile the code and emits an invalid and misleading error message. Also see https://bugs.swift.org/browse/SR-5931

In most cases, you can:

  • remove the type annotation ( <UInt8> ) to reveal the real cause
  • fix the cause
  • add the type annotation again
    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)
            }
        }
    }

cryptData is a NSMutableData object, so it has mutableBytes to work with.

How about:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM