Implementation of codebook in C language

Time:2021-10-17

This example shares the specific code of C language codebook for your reference. The specific contents are as follows

Function Description:

1. Account login (password verification, locking the account three times)

2. Function selection: 1. View all passwords 2. Add password 3. Delete password 4. Modify password 5. Query password 6. Unlock 7. Exit login

3. Save the password and encrypt the file

4. Flow chart:

Data definition section

#pragma once
//Variable declaration
#Define num 100 // initial password Book capacity
extern int g_ nNum;    // Current password Book capacity
extern int g_ lock_ flag;  // Lock flag
/*Defines the structure for storing account passwords****************************/
typedef struct _PASSWORD
{
 char website[100]; // Site name
 char userName[40];
 char passWord[20];
 char note[100];// remarks
}PASSWORD,*PPASSWORD;
/*Define user action enumeration*************************************/
#Define view all passwords 1
#Define new password 2
#Define delete password 3
#Define change password 4
#Define query password 5
#Define unlock 6
#Define exit 7

Interface and user interaction

int main()
{
 /*Global variable initialization**********************************************************************/
 g_ nNum = 0;      // Current password Book capacity
 g_ lock_ flag = 1;     // Lock flag

 /*Local variable definition************************************************************************/
 PPASSWORD pPwd = nullptr;   // Password array pointer
 int nCount = 0;     // Number of current passwords
 int op = 0;      // User operation

 /*Read locally, initialize password book, lock flag*****************************************************/
 readInfo(&pPwd, &nCount);

 /*Login verification = = > registration and login****************************************************************/
 if (!g_lock_flag)
 {
 Printf ("account locked, please unlock! \ n");
 unLock(pPwd, nCount);
 }
 else
 {
 login(pPwd, &nCount);
 }
 
 /*Cyclic scanning, function selection*********************************************************************/
 /*1. View all 2. Add 3. Delete 4. Modify 5. Query 6. Unlock 7. Exit*/
 /*Password verification mechanism, three times locking, login and permission locking*/
 while (1)
 {
 Dlg_print();
 Printf ("please enter operation:");
 scanf_s("%d", &op);
 switch (op)
 {
 Case view all passwords: searchallinfo (PWD, ncount); break;
 Case new password: insertinfo (ppwd, & ncount); break;
 Case delete password: deleinfo (ppwd, & ncount); break;
 Case password modification: reviseinfo (PWD, ncount); break;
 Case query password: queryinfo (ppwd, ncount); break;
 Case unlock: Unlock (ppwd, ncount); break;
 Case exit: admexit (ppwd, ncount); break;
 }
 }
 return 0;
}

Read and save

/*Read locally and initialize the password book*/
void readInfo(PPASSWORD *ppPwd, int *pnCount)
{
 FILE *fp = NULL;
 errno_t err;
 if ((err = fopen_s(&fp,"password.txt", "rb")) != 0)
 {
 //Open up the maximum capacity space and wait for the password
 Printf ("the file does not exist, please register the account... \ n");
 //The password array pointer is null. Assign a value to the address of the password array pointer
 *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);
 /*Printf ("number of passwords in the password book:% d \ n", * pncount);
 Printf ("password Book lock status:% d \ n", g_lock_flag);
 Printf ("current capacity of password book:% d \ n", g_nnum)*/
 /*Decryption*/
 if (rlen != g_nNum)
 {
 Printf ("password file read error \ 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);
}

/*Save password, save lock flag*/
void saveInfo(PASSWORD pwd[], int nCount)
{
 FILE *fp = NULL;
 errno_t err;
 if ((err = fopen_s(&fp,"password.txt", "wb+")) != 0)
 {
 Printf ("failed to open file \ n");
 exit(1);
 }
 fwrite(&nCount, sizeof(int), 1, fp);
 fwrite(&g_lock_flag, sizeof(int), 1, fp);
 /*Encrypt before storing*/
 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 ("file write error \ n");
 }
 else
 {
 Printf ("saved successfully \ n");
 }
 fclose(fp);
 system("pause");
 system("cls");
}

land

void login(PASSWORD pwd[],int *pnCount)
{
 char admName[40] = { 0 };
 char admPwd1[20] = { 0 };
 char admPwd2[20] = { 0 };
 printf("*******************************************\n");
 Printf ("---------------- welcome to 1024 password Book ----------- \ n");
 Printf ("account:");
 scanf_s("%s", admName,40);
 Printf ("password:");
 scanf_s("%s", admPwd1,20);

 if (strlen(pwd[0].userName) == 0||g_nNum == 0)    
 {
 //Account does not exist --- register --- save account password
 Printf ("re-enter password:");
 scanf_s("%s", admPwd2,20);
 if(strcmp(admPwd1, admPwd2)!=0)
 {
 Printf ("the two passwords are inconsistent, please re-enter \ n");
 exit(1);
 }
 strcpy_ S (PWD [0]. Website, 100, "codebook");
 strcpy_s(pwd[0].userName, 40, admName);
 strcpy_s(pwd[0].passWord, 20, admPwd1);
 strcpy_ S (PWD [0]. Note, 100, "administrator account");
 Printf ("registration succeeded \ n");
 (*pnCount)++;
 saveInfo(pwd, *pnCount);
 }
 else if (strcmp(admName, pwd[0].userName) == 0)
 {
 //Account exists, verify password
 int i = 0;
 while (strcmp(admPwd1, pwd[0].passWord) != 0)
 { 
 i++;
 if (i >= 3)
 {
 g_lock_flag = 0;
 Printf ("your account has been locked! Please unlock it before operation! \ n");
 exit(1);
 }
 Printf ("wrong password, please re-enter:");
 scanf_s("%s", admPwd1,20);
 }
 }
 else
 {
 Printf ("account password error \ n");
 system("pause");
 exit(1);
 }
}

The function is not written, the source code is uploaded, and the following is the running result

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.