c++暴力强制异或密码

C++ Brute forcing XOR cipher

本文关键字:密码 c++      更新时间:2023-10-16

我实现了异或加密算法,如下所示:

string XOR(string data, const char* key)
{
    string xorstring = data; //initialize new variable for our xordata
    for (int i = 0; i < xorstring.length(); i++) { //for loop for scrambling bits in the string
    xorstring[i] = data[i] ^ key[i]; //scrambling the string/descrambling it
    } 
    return xorstring;
}

这工作得很好,像:string ciphertext = XOR("test", "1234");将返回密文,解密时:string plaintext = XOR(ciphertext, "1234");将返回'test'。

所以,我想创建一个算法,可以通过暴力破解xor密码,所以基本上是尝试用每一个可能的密钥组合解密密文。

它应该是这样工作的:

  • 从字母
  • 的字符数组生成字符串
  • xor(解密)给定的密文与给定的字符串得到明文
  • 之后,xor(加密)生成的明文,并在if语句中比较它是否与原始密文
  • 匹配。
  • 如果匹配,则找到正确的密钥来解密密文。

就是这么简单,但我发现自己在算法上很挣扎:

const char Numbers[11] = "0123456789";
const char AlphabetUpper[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char AlphabetLower[27] = "abcdefghijklmnopqrstuvwxyz";
string XOR(string data, const char* key)
{
   string xorstring = data; //initialize new variable for our xordata
   for (int i = 0; i < xorstring.length(); i++) { //for loop for scrambling   bits in the string
    xorstring[i] = data[i] ^ key[i]; //scrambling the string/descrambling it
   }
   return xorstring;
 }
 string cipher, plain; //store the ORIGINAL ciphertext and plaintext
 int main()
 {
    plain = "test"; //set the plaintext
    cipher = XOR(plain, "1234"); //encrypt it with the xor function
    cout << plain << endl;  //output
    cout << cipher << endl; //output
    cout << "press enter to start bruteforcing!" << endl;
    getchar();
    while (true) //loop for bruteforcing
    {
       static int stringlength = 1; //the keylength starts from 1 and then
       //expands to 2,3,4,5, etc...
       BruteForce(stringlength, cipher, ""); //call the brute force function
       stringlength++; //increment the keylength
    }
    return 0;
}
void BruteForce(int length, string ciphertext,  string tempKey)
{
    static int count = 0; // for counting how many times key was generated
    string decipher, recipher; //for storing new XORed strings.
    if (length == 0) 
    {
       //decrypt the given ciphertext with the random key
       decipher = XOR(ciphertext, tempKey.c_str());
       //encrypt it again with the same key for comparison
       recipher = XOR(decipher, tempKey.c_str());
       cout << deciphered << endl; //output
       //compare the two ciphertexts
       if (ciphertext == recipher)
       {
         //....
          cout << "Key found! It was: '" << tempKey << "'" << endl;
          cout << "it took " << count << " iterations to find the key!";
          getchar();
       }
      return;
   }
   count++;
   //generate the keys.
   for (int i = 0; i < 26; i++) {
       std::string appended = tempKey + AlphabetLower[i];
       BruteForce(length - 1, ciphertext, appended);
    }
    for (int i = 0; i < 26; i++) {
       std::string appended = tempKey + AlphabetUpper[i];
       BruteForce(length - 1, ciphertext, appended);
     }
    for (int i = 0; i < 10; i++) {
       std::string appended = tempKey + Numbers[i];
       BruteForce(length - 1, ciphertext, appended);
   }
}

由于未知的原因,算法工作。

很奇怪,理论上应该行得通。当程序运行时,它说在每次执行bruteforce()函数时都找到了密钥。你自己试试吧。有人能指出我在这里做错了什么吗?帮助是值得感激的。谢谢。

它不起作用,因为你的任务是不可能的。一次性填充(xor)具有称为完全保密的属性,这意味着您的密文可以以相同的概率解密为任何纯文本。所以不可能知道哪个纯文本是原始的。

对于您的方案,特别是(message XOR key) XOR key总是只是消息返回。

这是异或操作的属性:对于每个键,您会发现

message XOR key XOR key == message

所以你检查键是否正确的方法确实会返回所有可能的键。

此外,假设您有一个加密消息E。然后,对于任何可能的纯文本消息M,您将始终找到密钥K,以便

E = M xor K

(尽管这样的K可以包含任意的char s,而不仅仅是字母等)因此,如果你在K中允许任意的char s,那么这个密码即使在理论上也是不可破解的(参见一次性pad)。

如果在K中只允许字母和数字,则不是每个明文消息都可以从给定的加密消息中接收到。但是,您将需要更多关于纯文本的信息,以便从错误的消息中检测出"真实"消息(例如,知道纯文本仅包含字母,或者它是有效的英文文本,等等),并且您需要在代码中检查这一点。无论如何,我想即使有了这个额外的启发式,你也会得到许多可能的纯文本。