gpt4 book ai didi

ios - 解码 base64 + 解密 - Swift

转载 作者:搜寻专家 更新时间:2023-11-01 07:13:21 32 4
gpt4 key购买 nike

let originalMsg:String = "THIS IS A TEST"
let sessionData = originalMsg.dataUsingEncoding(NSUTF8StringEncoding)
var encryptedPublic:NSData = RNCryptor.encryptData(sessionData!, password: ProjectConstants.MessageEncrypt.PUBLIC_KEY)
var base64EncodedPublic:NSData = encryptedPublic.base64EncodedDataWithOptions([])
var encryptedPrivate = RNCryptor.encryptData(base64EncodedPublic, password: privateKey)
var base64EncodedString:String = encryptedPrivate.base64EncodedStringWithOptions([])

您将如何解码和解密 base64EncodedString 以获得原始输入?我想我正在为 base64 解码而苦苦挣扎。

最佳答案

这是在 Swift 2 中从 base64 字符串获取 UTF-8 编码的 NSString 的方法:

let decodedData = NSData(base64EncodedString: base64EncodedString,
options: NSDataBase64DecodingOptions.fromRaw(0)!)
let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding)

但如果你想解密,你只需要数据:

do {
let decodedData = NSData(base64EncodedString: base64EncodedString,
options: NSDataBase64DecodingOptions.fromRaw(0)!)
let decryptedPrivate = RNDecryptor.decryptData(decodedData, password: privateKey)
let decodedPrivate = NSData(base64EncodedData: decryptedPrivate,
options: NSDataBase64DecodingOptions.fromRaw(0)!)
let decryptedPublic = RNDecryptor.decryptData(decodedPrivate, password: ProjectConstants.MessageEncrypt.PUBLIC_KEY)
let decryptedMessage = NSString(data: decryptedPublic, encoding: NSUTF8StringEncoding) as String
} catch {
// handle decryption errors...
}

关于ios - 解码 base64 + 解密 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645453/

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