c语言crypt,通过C中的文件通过crypt函数进行身份验证(分段错误)

这篇博客探讨了一个C语言程序中遇到的问题,该程序试图读取文件中的加密密码并要求用户输入登录密码进行匹配。在尝试用crypt函数加密用户输入并验证时,程序出现了“SegmentationFault”错误。作者怀疑问题可能出在log_password变量和加密消息的转换上。博客内容涉及密码管理、文件操作和C语言编程中的错误调试。
摘要由CSDN通过智能技术生成

我有下面的代码,在情况1中我首先尝试:注册一个新密码并将其写入文件,此方法有效,在情况2中我遇到的问题是:我正在尝试使用户提供一个密码以通过crypt函数对其进行加密,然后使用先前文件中的所有加密密码对其进行身份验证。但是,当我对此进行编译时,出现错误“ Segmentation Fault”。在我看来,问题将出在log_password和对加密消息的转换中。任何帮助将是巨大的。

#define _GNU_SOURCE

#include

#include

#include

#include

#include

#include

int

main(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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值