C语言实现密码本
功能简述:
- 账号登陆(密码验证,三次锁定账号)
- 功能选择:1、查看所有密码 2、新增密码 3、删除密码 4、修改密码 5、查询密码 6、解除锁定 7、退出登陆
- 保存密码,文件加密
- 流程图:
数据定义部分
#pragma once
#define NUM 100
extern int g_nNum;
extern int g_lock_flag;
typedef struct _PASSWORD
{
char website[100];
char userName[40];
char passWord[20];
char note[100];
}PASSWORD,*PPASSWORD;
#define 查看所有密码 1
#define 新增密码 2
#define 删除密码 3
#define 修改密码 4
#define 查询密码 5
#define 解除锁定 6
#define 退出 7
界面与用户交互
int main()
{
g_nNum = 0;
g_lock_flag = 1;
PPASSWORD pPwd = nullptr;
int nCount = 0;
int op = 0;
readInfo(&pPwd, &nCount);
if (!g_lock_flag)
{
printf("账户已锁定,请解锁!\n");
unLock(pPwd, nCount);
}
else
{
login(pPwd, &nCount);
}
while (1)
{
Dlg_print();
printf("请输入操作: ");
scanf_s("%d", &op);
switch (op)
{
case 查看所有密码: searchAllInfo(pPwd,nCount); break;
case 新增密码:insertInfo(pPwd, &nCount); break;
case 删除密码:deleInfo(pPwd, &nCount); break;
case 修改密码:reviseInfo(pPwd, nCount); break;
case 查询密码:queryInfo(pPwd, nCount); break;
case 解除锁定:unLock(pPwd, nCount); break;
case 退出:admExit(pPwd, nCount); break;
}
}
return 0;
}
读取和保存
void readInfo(PPASSWORD *ppPwd, int *pnCount)
{
FILE *fp = NULL;
errno_t err;
if ((err = fopen_s(&fp,"password.txt", "rb")) != 0)
{
printf("文件不存在,请注册账号……\n");
*ppPwd = (PPASSWORD)malloc(sizeof(PASSWORD)*NUM);
memset(*ppPwd, 0, sizeof(PASSWORD)*NUM);
g_nNum = NUM;
return;
}
fread(pnCount, sizeof(int), 1, fp);
fread(&g_lock_flag, sizeof(int), 1, fp);
g_nNum = *pnCount;
*ppPwd = (PPASSWORD)malloc(sizeof(PASSWORD)*g_nNum);
memset(*ppPwd, 0, sizeof(PASSWORD)*g_nNum);
size_t rlen = fread(*ppPwd, sizeof(PASSWORD), g_nNum, fp);
if (rlen != g_nNum)
{
printf("密码文件读取错误\n");
fclose(fp);
return;
}
size_t byteSize = sizeof(PASSWORD)*g_nNum;
char *buf = (char*)*ppPwd;
for (size_t i = 0; i < byteSize; i++)
{
buf[i] = (buf[i]-i)^8;
}
fclose(fp);
}
void saveInfo(PASSWORD pwd[], int nCount)
{
FILE *fp = NULL;
errno_t err;
if ((err = fopen_s(&fp,"password.txt", "wb+")) != 0)
{
printf("打开文件失败\n");
exit(1);
}
fwrite(&nCount, sizeof(int), 1, fp);
fwrite(&g_lock_flag, sizeof(int), 1, fp);
size_t byteSize = sizeof(PASSWORD)*nCount;
char *buf = (char*)malloc(byteSize);
memcpy_s(buf, byteSize, pwd, byteSize);
for (size_t i = 0; i < byteSize; i++)
{
buf[i] = (buf[i] ^ 8) + i;
}
size_t rlen = fwrite(buf, sizeof(PASSWORD), nCount, fp);
if (rlen != nCount)
{
printf("文件写入错误\n");
}
else
{
printf("保存成功\n");
}
fclose(fp);
system("pause");
system("cls");
}
登陆
void login(PASSWORD pwd[],int *pnCount)
{
char admName[40] = { 0 };
char admPwd1[20] = { 0 };
char admPwd2[20] = { 0 };
printf("*******************************************\n");
printf("-------------欢迎使用1024密码本------------\n");
printf("账户: ");
scanf_s("%s", admName,40);
printf("密码: ");
scanf_s("%s", admPwd1,20);
if (strlen(pwd[0].userName) == 0||g_nNum == 0)
{
printf("重新输入密码: ");
scanf_s("%s", admPwd2,20);
if(strcmp(admPwd1, admPwd2)!=0)
{
printf("两次密码不一致,请重新输入\n");
exit(1);
}
strcpy_s(pwd[0].website, 100, "密码本");
strcpy_s(pwd[0].userName, 40, admName);
strcpy_s(pwd[0].passWord, 20, admPwd1);
strcpy_s(pwd[0].note, 100, "管理员账户");
printf("注册成功\n");
(*pnCount)++;
saveInfo(pwd, *pnCount);
}
else if (strcmp(admName, pwd[0].userName) == 0)
{
int i = 0;
while (strcmp(admPwd1, pwd[0].passWord) != 0)
{
i++;
if (i >= 3)
{
g_lock_flag = 0;
printf("您的账户已被锁定!请解锁后再操作!\n");
exit(1);
}
printf("密码错误,请重新输入: ");
scanf_s("%s", admPwd1,20);
}
}
else
{
printf("账号密码错误\n");
system("pause");
exit(1);
}
}
功能就不写了,源代码上传了,下面是运行结果