c++ - 密码破解代码优化;

标签 c++ arrays optimization passwords

对于一个类(class)项目,我决定使用 asci 表中的所有可打印字符(减号除外)制作一个密码生成器,然后程序将尝试破解它。 密码字符存储在一个数组中,破解它的尝试存储在另一个数组中。尝试只是随机字符,然后与整个密码数组进行比较。如果两个数组中的所有字符都匹配,则程序终止……否则循环继续。我的程序的问题是匹配数组花费的时间太长,因为尝试只是随机数。你能通过开发一种检查密码的战略方法来帮助我优化代码吗?甚至伪代码也有帮助。谢谢。

... 只是一个加载栏。

#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>
using namespace std;

int main()
{
int a[1000], length, random, b[1000], c[1000], tries = 0; 
bool cracked = false;

cout << "Enter a password length: ";
cin >> length;

srand(time(NULL));
for (int i =0; i<length; i++){
    do {
        random = (rand()%94)+33;
    }while(random==45);
    a[i] = random;
    cout << char(a[i]);
}cout << endl;

do{
    for(int i =0; i<length; i++){
        do{
            random = (rand()%94)+33;
        }while(random==45);
        b[i] = random;
    }
    for(int k=0; k<length; k++){
        if(b[k]==a[k])
            c[k]=0;
        else
            c[k]=1;
        cracked=false;
        if(c[length-1]==0)
            cracked = true;
        else if(c[length-1]==1){
            k=0;
            cracked = false;
            tries++;
            cout << "... ";
        }
    }
}while(cracked==false);

for(int i=0; i<length; i++)
    cout << char (b[i]);
cout << "\nNumber of tries: " << tries << endl;

return 0;
}

最佳答案

您的代码只是进行随机猜测,您没有阻止它进行重复猜测。

您尝试过顺序暴力破解吗?长度为 5 需要半分钟,比您的方法略好。

#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>
using namespace std;

int main()
{
int a[1000];
int length;
int random;
int b[1000] = { 33 };
unsigned long long tries = 0;
bool cracked = false;

cout << "Enter a password length: ";
cin >> length;

srand(time(NULL));
for (int i =0; i<length; i++){
    do {
        random = (rand()%94)+33;
    }while(random==45);
    a[i] = random;
    cout << char(a[i]);
}cout << endl;

do{
    b[0]++;
    for(int i =0; i<length; i++){
        if (b[i] >= 94 + 33){
            b[i] -= 94;
            b[i+1]++;
        }else break;
    }
    cracked=true;
    for(int k=0; k<length; k++)
        if(b[k]!=a[k]){
            cracked=false;
            break;
        }
    if( (tries & 0x7ffffff) == 0 )
        cout << "\r       \r   ";
    else if( (tries & 0x1ffffff) == 0 )
        cout << ".";
    tries++;
}while(cracked==false);

cout << "\r       \n";
for(int i=0; i<length; i++)
    cout << char (b[i]);
cout << "\nNumber of tries: " << tries << endl;

return 0;
}

附注在 windows 平台上,打印文本到控制台非常慢。

关于c++ - 密码破解代码优化;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22523640/

相关文章:

c++ - 如何使用 .c 文件而不是 .cpp 文件在 google test 中编写测试类?

PHP:array_search 返回 null

c - 如何使用内存分配通过指针在数组中存储值

Java:如何实现3和?

javascript - 浏览器对拼接到切片的优化

带有计数的 for 和 while 循环中的 C++ 哨兵。陷入无限循环

c++ - 在派生类中更改功能访问模式

java - 计算最大 10^16 的 totient 函数之和

html - 在浏览器中有效地表示大网格

c++ - 具有动态分配成员的结构 vector