3

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?

Rohitax Rajguru
  • 893
  • 2
  • 13
  • 35

3 Answers3

1

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
Codo
  • 75,595
  • 17
  • 168
  • 206
  • Can anyone offer the actual solution? When I remove the in my case, it crashes the compiler, so I never get to see "the cause". – uliwitness Jul 10 '20 at 10:04
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)
            }
        }
    }
-1

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!")
    }
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215