我有下面的代碼,在情況1中我首先嘗試:注冊(cè)一個(gè)新密碼并將其寫(xiě)入文件,此方法有效,在情況2中我遇到的問(wèn)題是:我正在嘗試使用戶(hù)提供一個(gè)密碼以通過(guò)crypt函數(shù)對(duì)其進(jìn)行加密,然后使用先前文件中的所有加密密碼對(duì)其進(jìn)行身份驗(yàn)證。但是,當(dāng)我對(duì)此進(jìn)行編譯時(shí),出現(xiàn)錯(cuò)誤“ Segmentation Fault”。在我看來(lái),問(wèn)題將出在log_password和對(duì)加密消息的轉(zhuǎn)換中。任何幫助將是巨大的。#define _GNU_SOURCE#include <stdio.h>#include <time.h>#include <unistd.h>#include <crypt.h>#include <string.h>#include <sys/stat.h>intmain(void){FILE *f=fopen("shadow.txt","a+"); char * line=NULL; unsigned long seed[2]; char *log_password,salt[] = "$1$........"; const char *const seedchars = "./0123456789ABCDEFGHIJKLMNOPQRST" "UVWXYZabcdefghijklmnopqrstuvwxyz"; char *password,*pass; int i,ans; /* Generate a (not very) random seed. You should do it better than this... */ seed[0] = time(NULL); seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000); /* Turn it into printable characters from ‘seedchars’. */ for (i = 0; i < 8; i++) salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f]; printf("Press 1 for Register.\n"); printf("Press 2 for Login.\n"); printf("Press 3 for Exit.\n"); scanf("%d",&ans); switch(ans) { case 1: password = crypt(getpass("Password:"), salt); fprintf(f,"%s \n",password); printf("Succesfull Register in file\n"); fclose(f); break; case 2: fgets(pass,34,f); log_password = crypt(getpass("Login Password:"), pass); int ok; ok = strcmp (log_password, pass) == 0; puts(ok ? "Access granted." : "Access denied."); return ok ? 0 : 1; break; case 3: printf("Bye\n"); exit(1); break; default: printf("Try Again.\n"); break; } return 0;}
1 回答

慕神8447489
TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
答案是我忘了分配內(nèi)存。
#define pass_size 1000
pass=calloc(pass_size,sizeof(char));
添加回答
舉報(bào)
0/150
提交
取消