我正在开发一个程序,将加密的密码与测试用相同Salt值加密的单词进行比较。 Salt是用于crypt()函数的2字符密码,用于进一步卷积输入,并保存为加密密码的前2位数字。预期结果是密码/ Salt组合,加密到与我正在寻找的密码相同的值 - 等效密码。但是,Salt无法正确打印,因为在从加密密码读取2个字符后会出现其他(不可读)信息。虽然程序运行时没有出现错误,但无论使用什么密码,它都会产生相同的结果,编译时会出现2个警告:
Crack1.c:54:27: warning: implicit declaration of function 'crypt' is invalid in
C99 [-Wimplicit-function-declaration]
*decyphered = crypt(wordtest, salt);
^
Crack1.c:60:53: warning: comparison of array 'wordtest' not equal to a null
pointer is always true [-Wtautological-pointer-compare]
while(!memcmp(enc1, decyphered, (k + 2)) && wordtest != NULL);
^~~~~~~~ ~~~~
2 warnings generated.
运行时:
Test
Print encrypted text: 50zPJlUFIYY0o
Encrypted text: 50zPJlUFIYY0o
Salt value #0 = 5
Salt value #1 = 0
Full Salt: 50?Z?V?
Password: aA
Password: A&A
Password: A&NV
Password: A&NXM
Password:
Password: A&DwSOD
Password: A&BQVANT
我猜测salt问题正在影响crypt()函数。
源代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
This program take an encrypted input
before encrypting a host of strings
and comparing those encryptions to
the original input. All files read
from are to be kept in the same
folder.
*/
int main(int argc, char *argv[])
{
//Get and print text to be encyphered
printf("\nTest\n");
char enc1[50];
char salt[2];
char decyphered[50];
char filename[8][15] = {"two.txt","three.txt", "four.txt","five.txt","six.txt","seven.txt","eight.txt"};
char wordtest[10];
printf("\nPrint encrypted text: ");
scanf("%s", enc1);
printf("Encrypted text: %s\n", enc1);
//Fill salt
for (int i = 0; i < 2; i++)
{
salt[i] = enc1[i];
printf("Salt value #%i = %c\n", i, salt[i]);
}
printf("Full Salt: %s", salt);
//Run all possibilites and check result
FILE *fp;
for (int k = 0; k < 8; k++)
{
fp = fopen(filename[k],"r"); // opens files in read mode
if(fp == NULL)
{
perror("\nError while opening the file.\n");
exit(EXIT_FAILURE);
}
do
{
fgets(wordtest, k + 3, (FILE*)fp);
*decyphered = crypt(wordtest, salt);
if (memcmp(enc1, decyphered, (k + 2)))
{
printf("\nPassword: %s\n", wordtest);
}
}
while(!memcmp(enc1, decyphered, (k + 2)) && wordtest != NULL);
}
return 0;
}
非常感谢任何帮助!
答案 0 :(得分:0)
盐问题的最短解决方案是:
printf("Full Salt: %c%c", salt[0],salt[1]);
或者您可以定义salt[3]
并设置salt[2] = '\0'
然后使用printf打印%s
关于wordtest != NULL warning
,当您将wordtest
分配为char[10]
时,您已经在堆栈中为该变量分配空间,因此将其与null进行比较是不合理的,但可能您想测试是否wordtest
的内容为空或不可以使用:
strcmp(wordtest, "");