c# - 串行 key 生成异或字符串加密

标签 c# string xor key-generator

当我对字符串执行异或时,我会得到特殊字符

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 异或!%QV VT#7&%$#"!/.'

我想用它来生成仅包含字母数字字符的序列 key 我想传递 "62FA7AC4"、一个 日期 和一个介于 05000 之间的 数字 code> 和一些随机的虚拟数字

输入将为“62FA7AC4”+“2500”+“21/05/2018”

输出应类似于“5QBK-J4D1-8CF6-5MW2-78FZ-2FPL-4S6S-CTGB”

我做错了什么?

最佳答案

尝试使用数字,而不是字符串:

  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;

最后代表基36中的:

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

关于c# - 串行 key 生成异或字符串加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50448158/

相关文章:

c# - 没有指定输入参数的 Moq 模拟方法

c# - 如何在表格中插入图像路径

python - 如何在python中生成16字节的随机数并对它们进行异或运算

c++ - 按位异或以 SIGSEGV 结束

java - 放入完整英语词典的数据结构,以便从 Android 中的 String 快速返回所有可能的单词完成

c - 解析数组,查找值并使用校验和

c# - Linq-to-SQL ToDictionary

c# - 从azure表存储中获取所有记录

java - 在 Java 中使用正则表达式转义双斜杠

c - WIN32和其他c字符串的区别