晚上好,我对这个C代码有疑问,我正在制作我的uni项目。
它是一个加密类,我制作了一个脚本,它将从shadow.txt中获取散列密码及其盐,然后将散列密码与.txt中可能的密码进行比较。 这一切都很好,直到我调用decryptwithTXT()函数。它只是给我一个核心转储问题。我已经在函数中放了一些打印件,看看它崩溃的地方但它从不打印任何东西,这意味着它只是因为它调用了函数而崩溃,但我无法弄清楚原因。
如果您对我做错了什么并导致其崩溃有任何提示,我将非常感激。如果你还有关于我的代码质量的任何其他提示,请随时给我一些提示,我试图在C上做得更好,所以我很容易接受批评。
这是我的主要内容。
#include <stdio.h>
#include <crypt.h>
#include <string.h>
int getSaltnPassword(char* name,char* salt,char* pwd);
int decryptwithTXT(char* pwd,char* hpwd,char* salt,char* file);
int main()
{
int x;
char salt[8],hashedpwd[18],pwd[255],name[255],answer,flag,filename[25];
do{
printf(" Give username of targeted account.\n");
scanf("%s",name);
if (getSaltnPassword(name,salt,hashedpwd)==1){
printf("Salt: %s\nHashed Password: %s\n",salt,hashedpwd);
printf(" Proceed with attack on selected user? Y/N \n");
scanf("%c",&answer);
scanf("%c",&answer);
}
else
printf("User not found, try again.\n");
}while (answer!='Y'&&answer!='y');
printf(" Choose one of the following:\n");
printf(" a) Attack using datamined passwords.\n");
printf(" b) Dictionary attack.\n");
printf(" c) 4 character brute-force attack\n");
scanf("%c",&flag);
scanf("%c",&flag);
switch(flag){
case 'a':
printf("Datamine\n");
x=decryptwithTXT(pwd,hashedpwd,salt,"tom_datamine.txt");
case 'b':
printf("Dictionary.\n");
case 'c':
printf("Brute force.\n");
}
return 0;
}
这是我的decryptwithTXT功能。现在我只想让它打印tom_datamine.txt
中的所有密码int decryptwithTXT(char* pwd,char* hpwd,char* salt,char* file){
printf("entered function");
int flag=0;
FILE *fp;
char temppwd[255];
printf("declarations");
fp=fopen(file,"r");
printf("fopen");
while(!feof(fp)){
printf("loop");
fgets(temppwd,255,(FILE*) fp);
printf("fgets");
printf("%s",temppwd);
printf("print pwd");
}
fclose(fp);
return 0;
}
这是从shadow.txt获取salt和哈希密码的函数。它现在按预期工作。
int getSaltnPassword(char* name,char* salt, char* pwd){
FILE *fp;
char line[255],name_txt[255];
int i,salta,saltb,pwda,pwdb;
int j=0;
fp=fopen("my_shadow.txt","r");
while(!feof(fp)){
i=0;
fgets(line,255,(FILE*) fp);
while (line[i]!=':'){
name_txt[i]=line[i];
i++;
}
name_txt[i] = '\0';
if (strcmp(name_txt,name)==0){
printf("User found.\n");
salta=i+4;
saltb=i+12;
for (i=salta;i<saltb;i++){
salt[j]=line[i];
j++;
}
salt[8]='\0';
pwda=saltb+1;
pwdb=pwda+18;
j=0;
for (i=pwda;i<pwdb;i++){
pwd[j]=line[i];
j++;
}
pwd[18]='\0';
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
这就是我的shadow.txt看起来像
babis:$1$asff83lt$gggggg9nvR6civ7fZP.tt/:
anna:$1$kwif83lt$ZaNUkA9nvR6civ7fZP.tt/:
这就是我的tom_datamine.txt就好了。
tom
marousi
anna
pinkfloyd
bowling
tommarousi
tomarousi
tomanna
tompf
tompinkfloyd
bowlingfloyd
pink floyd
tombowling
答案 0 :(得分:1)
因为输出位于缓冲区中,直到通过填充或换行或fflush(stdout,当程序崩溃时,消息永远不会被打印出来)放到设备中。请注意,它经常是看错了写,比如printf(&#34; \ n我的消息&#34;);而不是printf(&#34;我的消息\ n&#34;)
正因为如此,我输入的调试printf,从未打印过,我认为我的函数调用有问题,而毕竟这是一个糟糕的文件读取。
答案归功于@WeatherVane,他在解决这个问题上提供了宝贵的帮助,并在评论部分提高了我的编程知识。