我正在尝试加密字符串并将其保存在钥匙串中。这就是我所做的
//Generate RSA private and public keys
let parameters: [String: AnyObject] = [
kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits as String: 1024 as AnyObject
]
SecKeyGeneratePair(parameters as CFDictionary, &publicKey, &privateKey)
let blockSize = SecKeyGetBlockSize(publicKey!)
var messageEncrypted = [UInt8](repeating: 0, count: blockSize)
var messageEncryptedSize = blockSize
status = SecKeyEncrypt(publicKey!, SecPadding.PKCS1, data, data.characters.count, &messageEncrypted, &messageEncryptedSize)
let encryptedString = String(data: messageEncrypted, encoding: .utf8)
在最后一行中,我收到此错误“无法将类型[UInt8]转换为数据”
我基本上想将messageEncrypted转换为String,以便将其保存在钥匙串中
我正在使用此Keychain Lib - https://github.com/jrendel/SwiftKeychainWrapper
这要求我以字符串形式输入数据
任何帮助将不胜感激。我知道在密钥链中保存加密数据可能不是可行的方法,但这是客户要求。
提前致谢
答案 0 :(得分:1)
似乎只需要将messageEncrypted
从[UInt8]
转换为Data
个实例,幸运的是有一个fitting initializer:
let encryptedData = Data(bytes: messageEncrypted)
let encryptedString = String(data: encryptedData, encoding: .utf8)