gpt4 book ai didi

C++ 暴力破解 XOR 密码

转载 作者:搜寻专家 更新时间:2023-10-31 00:11:45 24 4
gpt4 key购买 nike

我已经实现了这样的异或加密算法:

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"); 它将返回“测试”。

所以,我想创建一种算法,通过暴力破解来破解异或密码,所以基本上是尝试使用所有可能的 key 组合来解密密文。

它(应该)像这样工作:

  • 从字母表的字符数组生成字符串
  • 将给定的密文与给定的字符串异或(解密)得到明文
  • 然后对生成的明文进行异或(加密),并在if语句中进行比较,看是否与原始密文匹配
  • 如果匹配,则找到用于解密密文的正确 key 。

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

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() 函数时都找到了 key 。你自己试试吧。有人可以指出我在这里做错了什么吗?感谢帮助。谢谢。

最佳答案

它不起作用,因为您的任务是不可能完成的。一次一密 (xor) 具有称为 perfect secrecy 的属性这意味着您的密文可以等概率地解密为任何纯文本。所以不可能知道哪个纯文本是原始的。

对于您的方案,(message XOR key) XOR key 始终只是再次返回的消息。

关于C++ 暴力破解 XOR 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32889067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com