简体   繁体   English

RNCryptor:Swift中的AES128CBC解密

[英]RNCryptor: AES128CBC decrypting in Swift

How can I decrypt NSData with RNCryptor (AES128CBC)? 如何使用RNCryptor(AES128CBC)解密NSData? I already tried to understand the documentation: https://github.com/RNCryptor/RNCryptor-Spec/blob/master/draft-RNCryptor-Spec-v4.0.md 我已经尝试了解文档了: https : //github.com/RNCryptor/RNCryptor-Spec/blob/master/draft-RNCryptor-Spec-v4.0.md

Edit: 编辑:

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)")
        }
    }
}

I changed the code snippet @zaph linked to. 我更改了链接到的代码段@zaph。 You can see the result above. 您可以在上面看到结果。

NSString(data: cryptData, encoding: NSUTF8StringEncoding) results nil . NSString(data: cryptData, encoding: NSUTF8StringEncoding)结果为nil What is the problem? 问题是什么?

First you encrypt using RNCryptor, not some other method because RNCryptor is more than just AES encryption, it an entire secure encryption method including key derivation and authentication. 首先,您使用RNCryptor进行加密,而不是其他某种方法,因为RNCryptor不仅是AES加密,它是一种完整的安全加密方法,包括密钥派生和身份验证。 See the RNCryptor-Spec for more information. 有关更多信息,请参见RNCryptor-Spec

If you just want decryption use Common Crypto with Swift, see this SO Answer for example code. 如果您只想使用Swift的Common Crypto进行解密,请参阅此SO Answer示例代码。

Note: The sample code is just that, it should not be used as-is in production code. 注意:示例代码仅是示例代码,不应在生产代码中按原样使用。 In particular ECB mode is not secure, use CBC with a random iv and consider adding authentication and key derivation to the password such as PBKDF2. 特别是ECB模式不安全时,请将CBC与随机iv一起使用,并考虑向密码(例如PBKDF2)添加身份验证和密钥派生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM