小编bre*_*mes的帖子

CryptoSwift - 将UInt8数组转换为String解析为nil

(Xcode 8,Swift 3)

使用CryptoSwift库,我想加密字符串并将其存储在coredata中,出于某种原因,密码字符串结果为nil,尽管密文具有128个值:

let aes = try AES(key: pw, iv: nil, blockMode: .CBC, padding: PKCS7())
let ciphertext = try aes.encrypt(token.utf8.map({$0}))
let cipherstring = String(bytes:ciphertext, encoding: String.Encoding.utf8) // always nil
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用数据:字符串重载,将字节数组转换为数据对象.这也是零结果.

编辑/解决方案(根据Rob Napier的回答)

// encode/convert to string
let aes = try AES(key: pw, iv: nil, blockMode: .CBC, padding: PKCS7())
let ciphertext = try aes.encrypt(token.utf8.map({$0}))
let cipherstring = Data(bytes: ciphertext).base64EncodedString()
// decode
let aes = try AES(key: pw, iv: nil, blockMode: .CBC, padding: PKCS7())
let cipherdata = Data(base64Encoded: cipherstring)
let ciphertext …
Run Code Online (Sandbox Code Playgroud)

cryptography ios swift cryptoswift

5
推荐指数
1
解决办法
955
查看次数

标签 统计

cryptography ×1

cryptoswift ×1

ios ×1

swift ×1