简体   繁体   English

C# 和 Swift 中的 RNCryptor,RNCryptorError 错误 2

[英]RNCryptor in C# and Swift, RNCryptorError error 2

I am using RNCryptor in Swift and C#.NET .我在 Swift 和 C#.NET 中使用 RNCryptor。 I need a cross platform AES encryption and because of this, I am using RNCryptor.我需要跨平台 AES 加密,因此,我正在使用 RNCryptor。 When I encrypt some plain text in Swift,I can decrypt it in Swift correctly without any error.当我在 Swift 中加密一些纯文本时,我可以在 Swift 中正确解密它,没有任何错误。 But when I encrypt some text in C# and then I want to decrypt it in Swift,I got an error " The operation couldn't be completed. (RNCryptorError error 2.)"但是当我在 C# 中加密一些文本然后我想在 Swift 中解密它时,我得到一个错误“操作无法完成。(RNCryptorError 错误 2。)”

My code in C# :我在 C# 中的代码:

public static string EncryptQRCode(string qrCodeString){
        var qrEncryptor = new Encryptor ();
    return  qrEncryptor.Encrypt (qrCodeString, "password");

    }
public static string DecryptQRCode(string qrEncryptedString){
        var qrDecryptor = new Decryptor();
      return qrDecryptor.Decrypt (qrEncryptedString, "password");
    }

My Code in Swift:我的 Swift 代码:

func Encrypt(msg:String, pwd:String) -> String{
    let data = msg.dataUsingEncoding(NSUTF8StringEncoding,      allowLossyConversion: false)
    let chiper = RNCryptor.encryptData(data!, password: pwd)
    let base = chiper.base64EncodedDataWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
    let stringBase = String(data: base, encoding: NSUTF8StringEncoding)
    return stringBase!
}
func Decrypt(msg:String, pwd:String) -> String{
    let encodedData:NSData = NSData(base64EncodedString: msg, options: NSDataBase64DecodingOptions(rawValue: 0))!
    do{
        let decryptedData = try RNCryptor.decryptData(encodedData, password: pwd)
        let decrypytedString = String(data: text, encoding: NSUTF8StringEncoding)
        return decryptedString!
    }
    catch let error as NSError{
        print(error.localizedDescription)
        print(error.localizedDescription)
        
    }
    return "AN ERROR OCCURED"

}

For example:例如:

"ABC", with password "behdad" in C#, Encryptor returned: “ABC”,在 C# 中使用密码“behdad”,加密器返回:

"AgHfT2VvVOorlux0Ms47K46fG5lQOP2YhYWq2KeIKh+MisCDqZfrLF+KsJyBR2EBNC3wQpaKev0X4+9uuC5vliVoHkLsEi6ZI7ZIZ8qVUEkYGQ==" “AgHfT2VvVOorlux0Ms47K46fG5lQOP2YhYWq2KeIKh+MisCDqZfrLF+KsJyBR2EBNC3wQpaKev0X4+9uuC5vliVoHkLsEi6ZI7ZIZ8qVUEkYGQ==”

When I decrypt it in C#, it returned "ABC".当我用 C# 解密它时,它返回“ABC”。

But when I pass this Base64Encoded string to my Swift Decryptor function,it returned:但是当我将此 Base64Encoded 字符串传递给我的 Swift Decryptor 函数时,它返回:

RNCryptorError error 2. RNCryptorError 错误 2。

For Example: qrCodeString = "ABC".例如:qrCodeString = "ABC"。 public static string EncryptQRCode returns =公共静态字符串 EncryptQRCode 返回 =

"AgF6P5Ya0SifSymd3LqKdH+kGMCFobiziUhwwB6/lfZgAA9N+F5h350MyigoKo9qgUpMXX3x9FxZXwUOJODL4is3R62EGvZWdJBzjSNCef7Ouw==" “AgF6P5Ya0SifSymd3LqKdH+kGMCFobiziUhwwB6/lfZgAA9N+F5h350MyigoKo9qgUpMXX3x9FxZXwUOJODL4is3R62EGvZWdJBzjSNCef7Ouw==”

The "msg" is returned data from EncryptQRCode(The Base64 Encoded String). “msg”是从 EncryptQRCode(Base64 编码字符串)返回的数据。

pwd = "password" pwd = "密码"

encoded data = <02017a3f 961ad128 9f4b299d dcba8a74 7fa418c0 85a1b8b3 894870c0 1ebf95f6 60000f4d f85e61df 9d0cca28 282a8f6a 814a4c5d 7df1f45c 595f050e 24e0cbe2 2b3747ad 841af656 7490738d 234279fe cebb>编码数据= <02017a3f 961ad128 9f4b299d dcba8a74 7fa418c0 85a1b8b3 894870c0 1ebf95f6 60000f4d f85e61df 9d0cca28 282a8f6a 814a4c5d 7df1f45c 595f050e 24e0cbe2 2b3747ad 841af656 7490738d 234279fe cebb>

decryptedString and decryptedData do not have values due to the error occurred.由于发生错误,decryptedString 和decryptedData 没有值。

RNCryptorError error 2 RNCryptorError 错误 2

UnknownHeader = 2未知标题 = 2
Unrecognized data format.无法识别的数据格式。 Usually this means the data is corrupt.通常这意味着数据已损坏。

This means that the data passed is not in the correct format.这意味着传递的数据格式不正确。

The best programming advice I ever got was one night in the computer room when I asked Rick Cullman for help and he said: "Read the documentation."我得到的最好的编程建议是在计算机房的一个晚上,当我向 Rick Cullman 寻求帮助时,他说:“阅读文档。”

That is why I suggested displaying the inputs and outputs, you will catch that.这就是为什么我建议显示输入和输出的原因,你会明白的。

There are many places where hexadecimal is need to see what is happening and to debug.有很多地方需要使用十六进制来查看正在发生的事情并进行调试。

Opened an issue in RNCryptor Swift to add the error codes to the documentation.在 RNCryptor Swift 中打开了一个问题以将错误代码添加到文档中。

In C# when you want to encrypt the text,you have to use Schema.V3 for encryption.在 C# 中,要加密文本时,必须使用 Schema.V3 进行加密。 Decryptor in Swift cannot identify the Schema Version of Base 64 encoded string. Swift 中的解密器无法识别 Base 64 编码字符串的架构版本。

string encrypted = encryptor.Encrypt (YOUR_PLAIN_TEXT, YOUR_PASSWORD,Schema.V3);

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

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