凯撒密码暴力破解
Caesar Cipher bruteforce
这是我的 class 练习,我们必须暴力破解 "hdvb wr euhdn" 并找到有效的密钥,但我的代码破坏了它(我认为正确)但两个不同的密钥产生相同的结果结果。这是正常的还是我做错了?
键 2 和 11 有效,而其他一些无效的键仍然重复相同的字符。我们刚刚介绍了基本的凯撒移位密码,我认为这种方法是最好的蛮力。
#include<iostream>
#include<String>
using std::string;
using std::endl;
void bruteForce(string x);
void bruteForce(string x)
{
int key = 0;
int length = (int)x.length();
for (int i = 0; i < 25; i++)
{
key++;
std::cout << "The key is " << key << endl;
std::cout << endl;
for (int j = 0; j < length; j++)
{
if (x[j] == ' ')
{
x[j] = ' ';
std::cout << x[j];
j++;
}
x[j] = tolower(x[j]);
x[j] -= 97;
x[j] -= key;
if (x[j] < 0)
{
x[j] += 26;
}
x[j] += 97;
std::cout << x[j];
}
std:: cout << endl;
}
}
如 Alexander O'Mara 所述,每个循环都在修改输入 - 重构以将密钥更改与测试分开。
声明一个测试函数(与brute-force分开)
void Test(std::string x, int key);
从 bruteForce 调用它。
void bruteForce(string x)
{
int key = 0;
for (int i = 0; i < 25; i++)
{
key++;
Test( x, key );
}
}
实施
void Test(std::string x, int key)
{
int length = (int)x.length();
std::cout << "The key is " << key << endl;
std::cout << endl;
for (int j = 0; j < length; j++)
{
if (x[j] == ' ')
{
x[j] = ' ';
std::cout << x[j];
}
else {
x[j] = tolower(x[j]);
x[j] -= 97;
x[j] -= key;
if (x[j] < 0)
{
x[j] += 26;
}
x[j] += 97;
}
std::cout << x[j];
}
std::cout << endl;
}
我稍微更改了 space 的跳过,这样连续 2 space 就不会混淆了。
代码之所以有效,是因为每次调用 Test
都是原始字符串的新副本。 cesear 密码的每个连续键都会移动一个字符,因此应该提醒您注意字符串的 re-use。
你可以简单的使用下面的函数来解密凯撒密码
void decrypt(char arr[],int key){
int i,j;
for(i=0;i<strlen(arr);i++){
if(arr[i]==' ')
continue;
for(j=0;j<key;j++){
arr[i]--;
if(arr[i]<97 && arr[i] > 90)
arr[i] = 122;
if(arr[i] < 65)
arr[i] = 90;
}
}}
为了暴力破解,您可以简单地将上述函数放入一个 for 循环中,该循环将从 1 迭代到 26,为解密函数提供所有 26 个可能的密钥。下面是例子
int main(){
int i;
char arr[100],copy[100];
printf("Enter a string: ");
scanf ("%[^\n]s", arr);
strcpy(copy,arr);
for(i=1;i<=26;i++){
encrypt(arr,i);
printf("Bute Force key : %d ---> %s\n",i,arr);
strcpy(arr,copy);
}}
这是我的 class 练习,我们必须暴力破解 "hdvb wr euhdn" 并找到有效的密钥,但我的代码破坏了它(我认为正确)但两个不同的密钥产生相同的结果结果。这是正常的还是我做错了?
键 2 和 11 有效,而其他一些无效的键仍然重复相同的字符。我们刚刚介绍了基本的凯撒移位密码,我认为这种方法是最好的蛮力。
#include<iostream>
#include<String>
using std::string;
using std::endl;
void bruteForce(string x);
void bruteForce(string x)
{
int key = 0;
int length = (int)x.length();
for (int i = 0; i < 25; i++)
{
key++;
std::cout << "The key is " << key << endl;
std::cout << endl;
for (int j = 0; j < length; j++)
{
if (x[j] == ' ')
{
x[j] = ' ';
std::cout << x[j];
j++;
}
x[j] = tolower(x[j]);
x[j] -= 97;
x[j] -= key;
if (x[j] < 0)
{
x[j] += 26;
}
x[j] += 97;
std::cout << x[j];
}
std:: cout << endl;
}
}
如 Alexander O'Mara 所述,每个循环都在修改输入 - 重构以将密钥更改与测试分开。
声明一个测试函数(与brute-force分开)
void Test(std::string x, int key);
从 bruteForce 调用它。
void bruteForce(string x)
{
int key = 0;
for (int i = 0; i < 25; i++)
{
key++;
Test( x, key );
}
}
实施
void Test(std::string x, int key)
{
int length = (int)x.length();
std::cout << "The key is " << key << endl;
std::cout << endl;
for (int j = 0; j < length; j++)
{
if (x[j] == ' ')
{
x[j] = ' ';
std::cout << x[j];
}
else {
x[j] = tolower(x[j]);
x[j] -= 97;
x[j] -= key;
if (x[j] < 0)
{
x[j] += 26;
}
x[j] += 97;
}
std::cout << x[j];
}
std::cout << endl;
}
我稍微更改了 space 的跳过,这样连续 2 space 就不会混淆了。
代码之所以有效,是因为每次调用 Test
都是原始字符串的新副本。 cesear 密码的每个连续键都会移动一个字符,因此应该提醒您注意字符串的 re-use。
你可以简单的使用下面的函数来解密凯撒密码
void decrypt(char arr[],int key){
int i,j;
for(i=0;i<strlen(arr);i++){
if(arr[i]==' ')
continue;
for(j=0;j<key;j++){
arr[i]--;
if(arr[i]<97 && arr[i] > 90)
arr[i] = 122;
if(arr[i] < 65)
arr[i] = 90;
}
}}
为了暴力破解,您可以简单地将上述函数放入一个 for 循环中,该循环将从 1 迭代到 26,为解密函数提供所有 26 个可能的密钥。下面是例子
int main(){
int i;
char arr[100],copy[100];
printf("Enter a string: ");
scanf ("%[^\n]s", arr);
strcpy(copy,arr);
for(i=1;i<=26;i++){
encrypt(arr,i);
printf("Bute Force key : %d ---> %s\n",i,arr);
strcpy(arr,copy);
}}