C++ 暴力破解 XOR 密码
C++ Brute forcing XOR cipher
我已经实现了这样的异或加密算法:
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");
将 return 密文,解密时:string plaintext = XOR(ciphertext, "1234");
将 return 'test'.
所以,我想创建一个算法来通过暴力破解 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
所以你检查密钥是否正确的方法确实 return 所有可能的密钥。
此外,假设您有一条加密消息 E
。然后,对于任何可能的纯文本消息M
,您总能找到一个密钥K
,这样
E = M xor K
(虽然这样的 K
可以包含任意 char
s,而不仅仅是字母等)因此,如果你允许 K
中的任意 char
s,那么密码即使在理论上也是牢不可破的(见one-time pad)。
如果您只允许 K
中的字母和数字,则并非所有纯文本消息都可以从给定的加密消息中接收。但是,您将需要更多有关纯文本可能是什么的信息,以检测来自错误消息的 "true" 消息(例如,知道纯文本仅包含字母,或者它是有效的英文文本等。 ),您将需要在代码中检查这一点。无论如何,我想即使使用这种额外的启发式方法,您也会得到许多可能的纯文本。
它不起作用,因为你的任务是不可能的。一次性密码本 (xor) 有一个 属性,称为 perfect secrecy,这意味着您的密文可以以相同的概率解密为任何纯文本。所以不可能知道哪个纯文本是原始的。
针对你的计划,(message XOR key) XOR key
永远只是消息再次返回。
我已经实现了这样的异或加密算法:
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");
将 return 密文,解密时:string plaintext = XOR(ciphertext, "1234");
将 return 'test'.
所以,我想创建一个算法来通过暴力破解 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
所以你检查密钥是否正确的方法确实 return 所有可能的密钥。
此外,假设您有一条加密消息 E
。然后,对于任何可能的纯文本消息M
,您总能找到一个密钥K
,这样
E = M xor K
(虽然这样的 K
可以包含任意 char
s,而不仅仅是字母等)因此,如果你允许 K
中的任意 char
s,那么密码即使在理论上也是牢不可破的(见one-time pad)。
如果您只允许 K
中的字母和数字,则并非所有纯文本消息都可以从给定的加密消息中接收。但是,您将需要更多有关纯文本可能是什么的信息,以检测来自错误消息的 "true" 消息(例如,知道纯文本仅包含字母,或者它是有效的英文文本等。 ),您将需要在代码中检查这一点。无论如何,我想即使使用这种额外的启发式方法,您也会得到许多可能的纯文本。
它不起作用,因为你的任务是不可能的。一次性密码本 (xor) 有一个 属性,称为 perfect secrecy,这意味着您的密文可以以相同的概率解密为任何纯文本。所以不可能知道哪个纯文本是原始的。
针对你的计划,(message XOR key) XOR key
永远只是消息再次返回。