c++暴力强制异或密码
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");
将返回密文,解密时: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
中只允许字母和数字,则不是每个明文消息都可以从给定的加密消息中接收到。但是,您将需要更多关于纯文本的信息,以便从错误的消息中检测出"真实"消息(例如,知道纯文本仅包含字母,或者它是有效的英文文本,等等),并且您需要在代码中检查这一点。无论如何,我想即使有了这个额外的启发式,你也会得到许多可能的纯文本。
- c++中的oop(密码生成)
- 密码登录程序将永远循环并显示不正确的结果
- OpenSSL没有共享密码
- Cryptopp:获取密码输入的填充字符串
- 密码长度验证 (c++)
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 仿射密码解密,输出大小写不同
- 替换密码:哪一个?
- 无法使用 openssl 服务器,因为密码已过时...什么的...ERR_SSL_VERSION_OR_CIPHER_MISMATCH
- 如何使用 c++ 在 sqlite3 中打开受密码保护的数据库?
- 根据用户名和密码 Qt C++重新访问数据库值
- C++密码问题
- 无法在 Cocos2dx 中使用受密码保护的 zip 文件
- 如何使用librdkafka设置明文协议(无SASL)的用户名和密码?
- 如何将凯撒密码的元素从字符串更改为字符串
- 如何在C++的凯撒密码程序中包含空格?
- 运行密钥密码解密知道密钥?
- 如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们
- 当PSO细粒度策略对使用AdsGetObject MSDN API的Windows操作系统生效时,如何获取用户密码到期日
- 密码显示双字母 c++ 的问题