gpt4 book ai didi

swift - fatal error : 'try!' expression unexpectedly raised an error: CryptoSwift. AES.Error.BlockSizeExceeded:

转载 作者:行者123 更新时间:2023-11-28 06:56:14 30 4
gpt4 key购买 nike

错误: fatal error :“尝试!”表达式意外引发错误:CryptoSwift.AES.Error.BlockSizeExceeded:

我使用了这个 AES https://github.com/krzyzanowskim/CryptoSwift我的代码是这样的:

覆盖函数 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 方法:

内部函数 sendRequest(responseHandler: (response: String)-> Void) {

SocketManager.sharedInstance.sendMessage(self.requestData, handler: responseHandler)

套接字管理器方法:

internal func sendMessage(message: String, handler: (response: String)-> Void) {

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

这是为什么?请告诉我。谢谢!

最佳答案

struct Error: ErrorType {

}

func f() throws -> Void {
throw Error()
}

/*
try! f() // expression unexpectedly raised an error: Error()
*/

let a = try? f()
a == nil // true
do {
try f()
} catch {
print("ERROR")
}
// print "ERROR"

在你的情况下 CryptoSwift.AES.Error.BlockSizeExceeded 结果为零

关于swift - fatal error : 'try!' expression unexpectedly raised an error: CryptoSwift. AES.Error.BlockSizeExceeded:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563890/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com