c#中的XOR解密

时间:2015-07-04 00:59:41

标签: c# python encryption

我正在尝试用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)

3 个答案:

答案 0 :(得分:2)

只要您的输入字符串实际上是代码的十六进制表示,c#代码应如下所示:

// mapCompare :: ((a, a, (* -> Bool)) -> Bool) -> (Map(k:a), Map(k:a)) -> Bool
const mapCompare = f => (m1, m2) => {
  const aux = (it, m2) => {
    let {value, done} = it.next()
    if (done) return true
    let [k, v] = value
    return f (v, m2.get(k), $=> aux(it, m2))
  }
  return aux(m1.entries(), m2) && aux(m2.entries(), m1)
}

// mapDeepCompare :: ((a, a, (* -> Bool)) -> Bool) -> (Map(k:a), Map(k:a)) -> Bool
const mapDeepCompare = f => mapCompare ((a, b, k) => {
  if (a instanceof Map && b instanceof Map)
    return mapDeepCompare (f) (a,b) ? true && k() : false
  else
    return f(a,b,k)
})

// shortCircuitEqualComparator :: (a, a, (* -> Bool)) -> Bool
const shortCircuitEqualComparator = (a, b, k) =>
  a === b ? true && k() : false

// mapEqual :: (Map(k:a), Map(k:a)) -> Bool
const mapEqual = mapCompare (shortCircuitEqualComparator)

// mapDeepEqual :: (Map(k:a), Map(k:a)) -> Bool
const mapDeepEqual = mapDeepCompare (shortCircuitEqualComparator)
  
// fixtures
const a = new Map([['b', 2], ['a', 1]])
const b = new Map([['a', 1], ['b', 2]])
const c = new Map([['a', 3], ['b', 2]])
const d = new Map([['a', 1], ['c', 2]])
const e = new Map([['a', 1], ['b', new Map([['c', 2]])]])
const f = new Map([['b', new Map([['c', 2]])], ['a', 1]])
const g = new Map([['b', new Map([['c', 3]])], ['a', 1]])

// shallow comparison of two equal maps
console.log('true', mapEqual(a, b))
console.log('true', mapEqual(b, a))
// shallow comparison of two non-equal maps (differing values)
console.log('false', mapEqual(a, c))
console.log('false', mapEqual(c, a))
// shallow comparison of two other non-equal maps (differing keys)
console.log('false', mapEqual(a, d))
console.log('false', mapEqual(d, a))
// deep comparison of two equal nested maps
console.log('true', mapDeepEqual(e, f))
console.log('true', mapDeepEqual(f, e))
// deep comparison of two non-equal nested maps
console.log('false', mapDeepEqual(e, g))
console.log('false', mapDeepEqual(g, e))
// shallow comparison of two equal nested maps
console.log('false', mapEqual(e, f))
console.log('false', mapEqual(f, e))

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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
}