c中的XOR解密

本文关键字:解密 XOR 中的 | 更新日期: 2023-09-27 18:01:03

我试图用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();

我从这个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)

c中的XOR解密

一旦您的输入字符串实际上是代码的十六进制表示,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);

打印:

Encrypt 
Decrypt my_xor_hash

我没有更改任何一行,只更改了缩进。

编辑:

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

打印:

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
}