錯誤:致命錯誤:'嘗試!'表達意外加一個錯誤:CryptoSwift.AES.Error.BlockSizeExceeded:致命錯誤:'試試!'表達式意外地引發了一個錯誤:CryptoSwift.AES.Error.BlockSizeExceeded:
我用這個AES https://github.com/krzyzanowskim/CryptoSwift和我的代碼這樣的:
倍率FUNC viewDidLoad中(){ super.viewDidLoad()
let jsonString = "{ \"device\": \"iphone\", \"req\": \"connect\", \"deviceId\": \"device ID\" }";
let url = UrlManager()
url.setRequestData(jsonString)
url.sendRequest({ (response) -> Void in
print("Response data: \(response)")
let bytes: [UInt8] = Converter.hexstringConvertToBytes(response)
print("Bytes: \(bytes)")
let dencryptedBytes: [UInt8] = try! bytes.decrypt(AES(key: UrlManager.CONNECTION_KEY, iv: UrlManager.CONNECTION_IV))
print("Decoded Bytes: \(dencryptedBytes)")
let jsonData: String = Converter.bytesConvertToString(dencryptedBytes)
print("Received json: \(jsonData)")
})
UrlManager的
方法:
內部FUNC sendRequest將(ResponseHandler所:(響應:字符串) - >空隙){
SocketManager.sharedInstance.sendMessage(self.requestData, handler: responseHandler)
}
SocketManager方法:
內部FUNC的sendMessage(消息:字符串,處理程序:(響應:字符串) - >空隙){
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
// set response data to UrlManager
let response = self.sendRequest(message, client: self.socket)
handler(response: response!)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
//print("This is run on the main queue")
})
})
}
但這是工作(當我測試此代碼):
let hexaString =「 0B8F665884EE009C64A64862E26D33C42F490D6EF227709B1900D42B71FB2169802A27FACDCF2D5587A468E2AE0C14FD7FFC2D0D291A0021814A2D5EA4DC1225BE64B04C8CCA831CB1C97277040F5E36"
var numbers = [UInt8]()
var from = hexaString.startIndex
while from != hexaString.endIndex {
let to = from.advancedBy(2, limit: hexaString.endIndex)
numbers.append(UInt8(hexaString[from ..< to], radix: 16) ?? 0)
from = to
}
print(numbers) // [215, 193, 122, 79]
let dencryptedBytes: [UInt8] = try! numbers.decrypt(AES(key: "be8d5ba39cde4e41", iv: "a5e5ff5dcd2a655c"))
print(dencryptedBytes)
if let str = NSString(bytes: dencryptedBytes, length: dencryptedBytes.count, encoding: NSUTF8StringEncoding) as? String {
print(str)
} else {
print("not a valid UTF-8 sequence")
}
爲什麼呢? PLZ,告訴我。謝謝 !