问题
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?
回答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
回答2:
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)
}
}
}
回答3:
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!")
}
来源:https://stackoverflow.com/questions/50742540/unsafepointeruint8-is-not-convertible-to-unsafepointer