简体   繁体   English

C#中的XOR解密

[英]XOR decryption in c#

I'm trying to decrypt xor string with key in c# but decrypting is wrong and i'm getting wrong value. 我正在尝试使用C#中的密钥解密xor字符串,但是解密是错误的,并且我得到的值是错误的。

string text = "xorhash";
string key = "xorkey";
var result = new StringBuilder();

  for (int c = 0; c < text.Length; c++)
  result.Append((char)((uint)text[c] ^ (uint)key[c % key.Length]));

  return result.ToString();

I've get it from this python code, which working good. 我从此python代码中获得了它,效果很好。

def xor(message, key):
    return "".join(chr(ord(message[i]) ^ ord(key[i % len(key)])) for i in xrange(len(message)))
key = "my_xor_key"
message = "my_xor_hash".decode("hex")
print xor(message, key)

as soon as your input string is actually hex representation of codes, c# code should look like this: 输入的字符串实际上是代码的十六进制表示形式后,C#代码应如下所示:

for (int c = 0; c < text.Length; c+=2)
    result.Append((char)(Convert.ToUInt16(text.Substring(c, 2), 16) ^ (ushort)key[ (c/2) % key.Length]));
private static string xor(string text, string key) {
    var result = new StringBuilder();
    for (int c = 0; c < text.Length; c++)
      result.Append((char)((uint)text[c] ^ (uint)key[c % key.Length]));
    return result.ToString();
}

string text = "my_xor_hash";
string key = "my_xor_key";
string encrypt = xor(text, key);
string decrypt = xor(encrypt, key);
System.Console.Write("Encrypt " + encrypt);
System.Console.Write("Decrypt " + decrypt);

Prints: 打印:

Encrypt 
Decrypt my_xor_hash

And I didn't change a single line, only indentation. 而且我没有更改任何一行,仅更改了缩进。

Edit: 编辑:

private static string xor(string text, string key) {
    var result = new StringBuilder();
    for (int c = 0; c < text.Length; c++)
      result.Append((char)((uint)text[c] ^ (uint)key[c % key.Length]));
    return result.ToString();
}

private static string FromHex(string hex) {
    byte[] raw = new byte[hex.Length / 2];
    for (int i = 0; i < raw.Length; i++) {
        raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
    }
    return Encoding.ASCII.GetString(raw);
}

public static void Main() {
    string text = FromHex("xor_hash");
    string key = "xor_key";
    string decrypt = xor(text, key);
    System.Console.Write("Decrypt " + decrypt);
}

Prints: 打印:

Decrypt HARPERS
public static byte[] EncryptOrDecrypt(byte[] text, byte[] key)
{
    byte[] xor = new byte[text.Length];
    for (int i = 0; i < text.Length; i++)
    {
        xor[i] = (byte)(text[i] ^ key[i % key.Length]);
    }
    return xor;
}

static void Main(string[] args){
    string input;
    byte[] inputBytes;

    string inputKey;
    byte[] key;

    do
    {
        input = System.Console.ReadLine();
        inputBytes = Encoding.Unicode.GetBytes(input);

        inputKey = System.Console.ReadLine();
        key = Encoding.Unicode.GetBytes(inputKey);

        //byte[] key = { 0, 0 }; if key is 0, encryption will not happen

        byte[] encryptedBytes = EncryptOrDecrypt(inputBytes, key);
        string encryptedStr = Encoding.Unicode.GetString(encryptedBytes);

        byte[] decryptedBytes = EncryptOrDecrypt(encryptedBytes, key);
        string decryptedStr = Encoding.Unicode.GetString(decryptedBytes);

        System.Console.WriteLine("Encrypted string:");
        System.Console.WriteLine(encryptedStr);
        System.Console.WriteLine("Decrypted string:");
        System.Console.WriteLine(decryptedStr);

    } while (input != "-1" && inputKey != "-1");
    //test:
    //pavle
    //23
    //Encrypted string:
    //BRD_W
    //Decrypted string:
    //pavle
}

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

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