如何使用 RNCryptor (AES128CBC) 解密 NSData?我已经尝试了解文档:https ://github.com/RNCryptor/RNCryptor-Spec/blob/master/draft-RNCryptor-Spec-v4.0.md
编辑:
class Decription{
func AES128(message: String, key: String, iv: String){
let keyData: NSData! = (key as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
let ivData: NSData! = (iv as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
let data: NSData! = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
let cryptData = NSMutableData(length: Int(data.length) + kCCBlockSizeAES128)!
let keyLength = size_t(kCCKeySizeAES256)
let operation: CCOperation = UInt32(kCCDecrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options: CCOptions = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(
operation,
algoritm,
options,
keyData.bytes,
keyLength,
ivData.bytes,
data.bytes, data.length,
cryptData.mutableBytes,
cryptData.length,
&numBytesEncrypted
)
if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
print("Decrypted Result = \(NSString(data: cryptData, encoding: NSUTF8StringEncoding))")
} else {
print("Error: \(cryptStatus)")
}
}
}
我更改了链接到的代码片段@zaph。你可以看到上面的结果。
NSString(data: cryptData, encoding: NSUTF8StringEncoding)
结果nil
。问题是什么?