【发布时间】:2019-01-20 14:04:04
【问题描述】:
尝试使用暴力破解密码,但程序在找到密码时似乎没有意识到。
所以我尝试编写一个小程序来使用暴力破解密码。 基本上我使用嵌套的 for 循环遍历所有字母组合(密码不必超过 4 个字母),然后我在密码上使用给定盐的 crypt(假设盐是固定的)来检查我是否破解了密码(我可以访问加密的密码)。现在我有代码输出它生成的内容,看起来它确实遍历了所有小写变体。但不知何故,它永远找不到密码。
现在我自己创建了加密版本,所以我知道密码是 4 个字母长,这不是问题,我认为程序确实似乎遍历了所有可能性,那么问题可能是什么? if条件错了吗?
现在我想这不是解决问题的最优雅的解决方案,但我认为一般的想法是正确的。但是,如果嵌套的 for 循环方法存在问题,我很乐意知道这一点:)。
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <cs50.h>
int main(int argc, string argv[]) {
if(argc < 2) {
printf("Please enter a password to crack! \n");
return 1;
}
char s[4] = "";
for(int i = 0; i <=27; i++) {
if(i == 0)
s[0] = 0;
else
s[0] = i - 1 + 'a';
if(strcmp(crypt(s, "50"), argv[1]) == 0) {
break; }
for(int j = 0; j <=26; j++) {
if(j == 0)
s[1] = 0;
else
s[1] = (j - 1 + 'a');
if(strcmp(crypt(s, "50"), argv[1]) == 0)
break;
for(int k = 0; k <= 26; k++) {
if(k == 0)
s[2] = 0;
s[2] = (k - 1 + 'a');
if(strcmp(crypt(s, "50"), argv[1]) == 0)
break;
for(int l = 0; l <= 26; l++) {
printf("%s \n", s);
if(l == 0)
s[3] = 0;
else
s[3] = (l - 1 + 'a');
if(strcmp(crypt(s, "50"), argv[1]) == 0)
break;
}
}
}
}
if (strcmp(crypt(s, "50"), argv[1]) != 0)
printf("Password not found");
else
printf("%s \n", s);
【问题讨论】: