串行密钥生成 xor 字符串加密

Serial key generation xor string encryption

当我在 string 上执行 xor 时,我得到特殊字符

private void btnEncryptDecrypt_Click(object sender, EventArgs e)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(keyTextBox.Text);
        textTextBox.Text = string.Empty;

        foreach (byte i in bytes)
        {
            byte c = Convert.ToByte(i ^ Convert.ToByte(textBox1.Text));

            textTextBox.Text = textTextBox.Text + Convert.ToChar(c);

        }         

    }

62FA7AC4 1234567890 xor!%QV VT#7&%$#"! /.'

我想将其用于 序列号生成 只有字母数字字符 我想在 05000 之间传递 "62FA7AC4"datenumber 以及一些随机虚拟数字

输入将是"62FA7AC4"+"2500"+"21/05/2018"

Output 应该类似于 "5QBK-J4D1-8CF6-5MW2-78FZ-2FPL-4S6S-CTGB"

我做错了什么?

尝试使用 numbers,而不是 strings:

  using System.Numerics;

  ... 

  // Let's use BigInteger for arbitrary long values
  BigInteger left = BigInteger.Parse("62FA7AC4", NumberStyles.HexNumber);
  BigInteger right = BigInteger.Parse("1234567890", NumberStyles.HexNumber);

  string result = (left ^ right).ToString("X");

  Console.Write(result);

所以你有

  1256AC0254

编辑:据我所知你想要字母数字(即Base 36 == 26 字母 + 10 数字 输出)。您可以使用相同的方法:使用整数操作,而不是字符串

  private static Random s_Random = new Random();

  ...

  BigInteger value = BigInteger.Parse(
      "0"        +                             // we don't want negative values  
      "62FA7AC4" +                             // header 
       s_Random.Next(5001).ToString("0000") +  // random in [0..5000] range
       DateTime.Today.ToString("ddMMyyyy"),    // Date like 21052018
    NumberStyles.HexNumber);                    

然后执行异或运算(如果你愿意):

 value ^= some_secret_value;

最后表示base 36中的value

private static String ToBase36(BigInteger value) {
  List<char> list = new List<char>();

  for (int index = 0; value > 0; value /= 36, index++) {
    if (index > 0 && index % 4 == 0)
      list.Add('-');

    BigInteger v = value % 36;

    list.Add(v < 10 ? (char)('0' + v) : (char) ('A' + v - 10));
  }

  list.Reverse();

  return string.Concat(list);
}

测试:

 BigInteger value = BigInteger.Parse(
    "0" + 
    "62FA7AC4" + 
    "2500" + 
     DateTime.Today.ToString("ddMMyyyy"), 
   NumberStyles.HexNumber);

 string result = ToBase36(value);

 Console.Write(result);

结果:

2443-WNC5-AVBB-M32W

编辑2:恢复原数

private static BigInteger FromBase36(string value) {
  BigInteger result = 0;
  BigInteger power = 1;

  for (int i = value.Length - 1; i >= 0; --i) {
    char item = value[i];

    if (item >= '0' && item <= '9') {
      result += power * (item - '0');
      power *= 36;
    }
    else if (item >= 'A' && item <= 'Z') {
      result += power * (item - 'A' + 10);
      power *= 36;
    }
    else if (item >= 'a' && item <= 'z') {
      result += power * (item - 'a' + 10);
      power *= 36;
    }
  }

  return result;
}

例如

BigInteger value = BigInteger.Parse(
    "0"        + 
    "62FA7AC4" + 
    "2500" + 
     DateTime.Today.ToString("ddMMyyyy"), 
   NumberStyles.HexNumber);

string result = ToBase36(value);

BigInteger back = FromBase36(result);

Console.WriteLine(string.Join(Environment.NewLine, value, result, back));

结果:

467412447575903165554712
2443-WNC5-AVBB-M32W
467412447575903165554712