问题描述
我使用CryptoSwift来加密数据。我正在学习如何使用它,但我不能超过第一个基本教程。我无法将加密的数据转换回String - 如果我不能清楚地解密数据,这种方法会破坏它的加密目的
代码:
>
let string =Hi。这是Atlas
让输入:[UInt8] = Array(string.utf8)
print(input)
let key:[UInt8] = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00]
let iv:[UInt8] = AES.randomIV(AES.blockSize)
do {
let encryptedBytes: [UInt8] = try AES(key:key,iv:iv,blockMode:.CBC).encrypt(input,padding:PKCS7())
print(encryptedBytes)
$ b b let decrypted:[UInt8] = try AES(key:key,iv:iv,blockMode:.CBC).decrypt(encryptedBytes,padding:PKCS7())
打印;&需要将此数组的byted转换为一个字符串(应该等于原始输入)
} catch {
} catch {
}
pre> 感谢您的帮助
基础为你解码UTF8,因为没有办法直接生成 String.UTF8View
。因此,首先转换为 NSData
。
let decrypted:[UInt8] = [0x48,0x65,0x6c,0x6c,0x6f]
let data = NSData(bytes:decrypted,length:decrypted.count)
let str = String(data:data,encoding:NSUTF8StringEncoding)
如果你想在没有Foundation的情况下做,你可以,但这是一个小工作。您必须自行管理解码。
扩展字符串{
init?(utf8Bytes:[UInt8]){
var decoder = UTF8()
var g = utf8Bytes.generate()
var characters:[Character] = []
LOOP:
while true {
let result = decoder.decode(& g)
switch result {
case .Result(let scalar):characters.append(Character(scalar))
case .EmptyInput:break LOOP
case .Error:return nil
}
}
self.init(characters)
}
}
让unicode = String(utf8Bytes:bytes)
(我很惊讶, stdlib,因为它是如此常见,可以快速构建的其他部分的Swift stdlib。通常情况下,有一个原因,我只是还没有意识到,所以这里可能有一些微妙的问题。
I am using CryptoSwift to encrypt data. I am learning how to use it however I cannot get past the first basic tutorial. I am unable to convert the encrypted data back to a String - which kind of defeats the purpose of encrypting it in the first place if I cannot legibly decrypt the data
Code:
let string = "Hi. This is Atlas"
let input: [UInt8] = Array(string.utf8)
print(input)
let key: [UInt8] = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
let iv: [UInt8] = AES.randomIV(AES.blockSize)
do {
let encryptedBytes: [UInt8] = try AES(key: key, iv: iv, blockMode: .CBC).encrypt(input, padding: PKCS7())
print(encryptedBytes)
let decrypted: [UInt8] = try AES(key: key, iv: iv, blockMode: .CBC).decrypt(encryptedBytes, padding: PKCS7())
print(decrypted) // << need to convert this array of byted to a string (should be equal to original input)
} catch {
} catch {
}
Thank you for the help
You'll want Foundation to decode the UTF8 for you since there's no way to generate a String.UTF8View
directly. So convert to NSData
first.
let decrypted: [UInt8] = [0x48, 0x65, 0x6c, 0x6c, 0x6f]
let data = NSData(bytes: decrypted, length: decrypted.count)
let str = String(data: data, encoding: NSUTF8StringEncoding)
If you want to do it without Foundation, you can, but it's a little work. You have to manage the decoding yourself.
extension String {
init?(utf8Bytes: [UInt8]) {
var decoder = UTF8()
var g = utf8Bytes.generate()
var characters: [Character] = []
LOOP:
while true {
let result = decoder.decode(&g)
switch result {
case .Result(let scalar): characters.append(Character(scalar))
case .EmptyInput: break LOOP
case .Error: return nil
}
}
self.init(characters)
}
}
let unicode = String(utf8Bytes: bytes)
(I'm very surprised that this isn't built into Swift stdlib since it's so common and can be quickly built out of other parts of Swift stdlib. Often when that's the case, there's a reason that I'm just not aware of yet, so there may be some subtle problem with my approach here.)
这篇关于如何将解密的UInt8转换为String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!