C language implementation of codebook small project

Time:2021-10-18

1、 Introduction

I’ve been learning C language for some time. I just did a small project of codebook and shared it.

2、 Ideas and principles

The password book, known by name, is a book that stores account passwords and plays the role of memos. The data to be remembered is encrypted and stored in the text file. The opened text file is encrypted text. The stored data needs to be viewed through software to improve security. (the software has designed a startup password and automatically exits after three times)

Project objectives:

  • Save the account and password, open the software after exiting, and the data will not be lost
  • Simple encryption
  • Can search account and password according to conditions
  • Can modify account and password data
  • Data can be deleted according to conditions

Knowledge involves the use of arrays, file operations, and simple XOR encryption.

Some screenshots of the software are as follows:

Main menu

Add data

Delete data

Query data

All data

3、 Realize

1. Header file and data definition

In the small project of the password book, I set up four arrays for storing data, the site for storing the account and password, the account, the password and remarks. Defines a global variable to count the number of entries used by the current codebook.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
//Define four arrays to store the values of site, account, password and remarks
char webSite[100][20];
char userName[100][20];
char passWord[100][20];
char reMark[100][20];
//Define a global variable
int g_count = 0;

2. Overall framework of the procedure

int main(void)
{
 InitInfo();
 int nChoice = 1;
 
 StartPassWord();
 
 while (nChoice)
 {
 Printf ("**************************************************************** \ n");
 Printf ("please enter the following options to operate: \ n");
 Printf ("1. Add data \ n");
 Printf ("2. Delete data \ n");
 Printf ("3. Modify data \ n");
 Printf ("4. Query data \ n");
 Printf ("5. All data \ n");
 Printf ("6. End program \ n");
 printf("********************************************************************************\n");
 scanf_s("%d", &nChoice);
 switch (nChoice)
 {
 case 1:
 {
 AddInfo();// Add data 
 }break;
 case 2:
 {
 DelInfo(); // Delete data
 }break;
 case 3:
 {
 EditInfo();// Modify data
 }break;
 case 4:
 {
 FindInfo();// Query data
 }break;
 case 5:
 {
 ReadAllInfo();// View all
 }break;
 case 6:
 {
 nChoice = 0;// Exit program
 }break;
 }
 }
 Printf ("welcome to use next time! \ n");
 
 system("pause");
 return 0;
}

3. Function declaration

//Enter password at startup
void StartPassWord();
//Add data
void AddInfo();
//Delete data
void DelInfo();
//Modify data
void EditInfo();
//Query data
void FindInfo();
//Read all data
void ReadAllInfo();
//Save data
void SaveInfo();
//Initialization data
void InitInfo();

4. Implementation of each function

//Add data
void AddInfo()
{
 Printf ("enter the site to store:");
 scanf_s("%s", webSite[g_count], 20);
 Printf ("enter the account to be stored:");
 scanf_s("%s", userName[g_count], 20);
 Printf ("enter the password to be stored:");
 scanf_s("%s", passWord[g_count], 20);
 Printf ("enter comments to be stored:");
 scanf_s("%s", reMark[g_count], 20);
 g_count++;
 SaveInfo();
 InitInfo();
 system("pause");
 system("cls");
}
 
//View all
void ReadAllInfo()
{
 FILE *pfile;
 if (fopen_s(&pfile, "data.i", "r") != 0)
 {
 printf("file open fail");
 exit(1);
 }
 
 fscanf_ S (pfile, "current number% d \ n", &g_count);
 Printf ("currently there are% d pieces of data \ n", g_count);
 for (int i = 0; i < g_count; i++)
 {
 Printf ("item% d data: \ n", I + 1);
 Printf ("site:% s, account:% s, password:% s, remarks:% s",
 webSite[i], userName[i], passWord[i], reMark[i]);
 printf("\n");
 
 }
 fclose(pfile);
 system("pause");
 system("cls");
}
 
//Save data
void SaveInfo()
{
 for (int j = 0; j < g_count; j++)
 {
 for (int m = 0; m < 20; m++)
 {
 webSite[j][m] ^= 5;
 userName[j][m] ^= 5;
 passWord[j][m] ^= 5;
 reMark[j][m] ^= 5;
 }
 }
 FILE *pfile;
 if (fopen_s(&pfile, "data.i", "w") != 0)
 {
 printf("file open fail");
 exit(1);
 }
 fseek(pfile, 0, SEEK_SET);
 Fprintf (pfile, "current number% d \ n", g_count);
 for (int i = 0; i < g_count; i++)
 {
 fwrite(webSite[i], 20, 1, pfile);
 fwrite(userName[i], 20, 1, pfile);
 fwrite(passWord[i], 20, 1, pfile);
 fwrite(reMark[i], 20, 1, pfile);
 fprintf(pfile, "\n");
 }
 fclose(pfile);
}
 
//Delete data
void DelInfo()
{
 char aTemp[20];
 int nFind = 0;
 int nIndex = 0;
 Printf ("please enter the site to delete: \ n");
 scanf_s("%s", aTemp, 20);
 for (; nIndex < g_count; nIndex++)
 {
 if (strcmp(aTemp, webSite[nIndex]) == 0)
 {
 nFind = 1;
 break;
 }
 }
 if (nFind == 1)
 {
 for (int i = nIndex; i < g_count; i++)
 {
 strcpy_s(webSite[i], 20, webSite[i + 1]);
 strcpy_s(userName[i], 20, userName[i + 1]);
 strcpy_s(passWord[i], 20, passWord[i + 1]);
 strcpy_s(reMark[i], 20, reMark[i + 1]);
 }
 g_count--;
 SaveInfo();
 InitInfo();
 Printf ("deleted successfully \ n");
 }
 else
 {
 Printf ("lookup failed! \ n");
 }
 system("pause");
 system("cls");
}
 
//Initialization data
void InitInfo()
{
 FILE *pfile;
 if (fopen_s(&pfile, "data.i", "r") != 0)
 {
 Printf ("please create the data. I file first!");
 system("pause");
 exit(1);
 }
 fscanf_ S (pfile, "current number% d \ n", &g_count);
 for (int i = 0; i < g_count; i++)
 {
 
 fread(webSite[i], 20, 1, pfile);
 fread(userName[i], 20, 1, pfile);
 fread(passWord[i], 20, 1, pfile);
 fread(reMark[i], 20, 1, pfile);
 fscanf_s(pfile, "\n");
 }
 for (int j = 0; j < g_count; j++)
 {
 for (int m = 0; m < 20; m++)
 {
 webSite[j][m] ^= 5;
 userName[j][m] ^= 5;
 passWord[j][m] ^= 5;
 reMark[j][m] ^= 5;
 }
 }
 fclose(pfile);
}
 
//Query data
void FindInfo()
{
 char aTemp[20];
 int nFind = 0;
 
 Printf ("please enter the site to query: \ n");
 scanf_s("%s", aTemp, 20);
 for (int nIndex = 0; nIndex < g_count; nIndex++)
 {
 if (strcmp(aTemp, webSite[nIndex]) == 0)
 {
 nFind = 1;
 }
 if (nFind == 1)
 {
 Printf ("site:% s, account:% s, password:% s, remarks:% s \ n",
 webSite[nIndex], userName[nIndex], passWord[nIndex], reMark[nIndex]);
 break;
 }
 }
 if (nFind == 0)
 {
 Printf ("lookup failed! \ n");
 }
 system("pause");
 system("cls");
}
 
//Modify data
void EditInfo()
{
 char aTemp[20];
 int nFind = 0;
 
 Printf ("please enter the site to modify: \ n");
 scanf_s("%s", aTemp, 20);
 for (int nIndex = 0; nIndex < g_count; nIndex++)
 {
 if (strcmp(aTemp, webSite[nIndex]) == 0)
 {
 nFind = 1;
 }
 if (nFind == 1)
 {
 Printf ("enter the account number to be modified:");
 scanf_s("%s", userName[nIndex], 20);
 Printf ("enter the password to be modified:");
 scanf_s("%s", passWord[nIndex], 20);
 Printf ("enter comments to be modified:");
 scanf_s("%s", reMark[nIndex], 20);
 Printf ("site:% s, account:% s, password:% s, remarks:% s \ n",
 webSite[nIndex], userName[nIndex], passWord[nIndex], reMark[nIndex]);
 SaveInfo();
 break;
 }
 }
 if (nFind == 0)
 {
 Printf ("lookup failed! \ n");
 }
 system("pause");
 system("cls");
}
 
//Enter password at startup
void StartPassWord()
{
 char spw[10] = "abcd"; // Program startup password
 char write[10] = "";
 int nSuccess = 0;
 
 for (int i = 1; i <= 3; i++)
 {
 Printf ("please enter the startup password: \ n");
 scanf_s("%s", write, 10);
 if (strcmp(write, spw) == 0)
 {
 nSuccess = 1;
 break;
 }
 else
 {
 Printf ("your input is wrong! There are% d chances \ n", 3 - I);
 system("pause");
 system("cls");
 }
 }
 if (nSuccess == 1)
 {
 Printf ("correct input! Press any key to start using the program \ n");
 }
 else
 {
 Printf ("input all errors! Press any key to end the program \ n");
 system("pause");
 exit(1);
 }
 system("pause");
 system("cls");
}

5. About encryption

XOR encryption: XOR is an operation method. It is a ^ symbol in C language and can usually be used for encryption.

For example, implementation in a project


for (int j = 0; j < g_count; j++)
 {
 for (int m = 0; m < 20; m++)
 {
 webSite[j][m] ^= 5;
 userName[j][m] ^= 5;
 passWord[j][m] ^= 5;
 reMark[j][m] ^= 5;
 }
 }

4、 Summary

There are still many defects in the program. I hope you will forgive me for writing a blog for the first time!

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.