简体   繁体   English

使用RNCryptor解密base64编码和AES加密的字符串

[英]Decrypt a base64 encoded and aes encrypted string using RNCryptor

I am just new to RNCryptor but I have been given a sample string which is encrypted and base64 encoded. 我只是RNCryptor的新手,但是我得到了一个示例字符串,该字符串经过加密和base64编码。 I am trying to use the RNCryptor decrypt func to see the string in plain text. 我正在尝试使用RNCryptor解密函数以纯文本格式查看字符串。

I tried the following: 我尝试了以下方法:

func decryptStr(_ sample : String){

    let sampleBase64Decoded = sample.fromBase64Data()

    do {
        let decryptedNSData = try RNCryptor.decrypt(data: sampleBase64Decoded!, withPassword: "secretPass")
        let decryptedNSString = NSString(data: decryptedNSData, encoding: String.Encoding.utf8.rawValue)
        print("decrypted : \(decryptedNSString)")
    }
    catch let error as NSError {
        print("issue decrypting :\(error.localizedDescription)")
    }

}

and I am calling it like so: 我这样称呼它:

decryptStr("R79gQDNTt/0+cjU7pduqfA==")

and the fromBase64 looks like so: 和fromBase64看起来像这样:

extension String {

func fromBase64() -> String? {
    guard let data = Data(base64Encoded: self, options: NSData.Base64DecodingOptions(rawValue: 0)) else {
        return nil
    }

    return String(data: data, encoding: String.Encoding.utf8)!
}

}

I am receiving the error connect.RNCryptor.Error error 2. Why would this be? 我收到错误connect.RNCryptor.Error错误2。为什么会这样? Is it in any way related to the fact that the string is both base 64 encoded and also encrypted? 这与字符串既以base 64编码又经过加密的事实有任何关系吗?

The password is correct. 密码正确。

Your Base-64 data ( R79gQDNTt/0+cjU7pduqfA== ) is not an RNCryptor message (the first byte is incorrect, and it's too short). 您的Base-64数据( R79gQDNTt/0+cjU7pduqfA== )不是RNCryptor消息(第一个字节不正确,并且太短了)。

There is no "standard" format for AES encryption. AES加密没有“标准”格式。 You have to match the decryptor to the encryptor. 您必须将解密器与加密器进行匹配。 RNCryptor implements a specific format that implements important security benefits that are missing in most ad hoc solutions. RNCryptor实现了一种特定的格式,该格式实现了大多数临时解决方案所缺少的重要安全优势。 If this data comes from a server, you'll need to use an RNCryptor implementation there as well, or rewrite your Swift code to implement whatever format your server is using (this looks like an ad hoc custom format, so I can't say how to implement that). 如果此数据来自服务器,则也需要在服务器上使用RNCryptor实现,或重写Swift代码以实现服务器使用的任何格式(这看起来是特殊的自定义格式,所以我不能说如何实施)。

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

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