当前位置:网站首页>C language to implement a password manager (under update)
C language to implement a password manager (under update)
2022-07-03 14:36:00 【roseisbule】
As a computer enthusiast , Will often register various website accounts , But considering the possible leakage of privacy , Don't like hosting passwords to browsers , So often forget all kinds of passwords . After suffering , I decided to learn a little c Language knowledge to write a password manager .
Before writing, I hope it can realize the following functions :
1. Be able to write a new password , And save the time and date when writing the password .
2. Can find the password , When you can't find an account that fully matches the characteristics , Can remind me of the most similar account .
3. Be able to change the password of a certain account type , When modifying , Synchronize the time of modification .、
4. The stored data is divided into four parts , The first is the account type , Such as :wechat, The second is account , The third is the account and password , The fourth is the last storage , Or the standard time of modification .
5. Be able to list all account types , For me to read .
6. All stored data should be randomly encrypted , When reading, it can decrypt according to the random key .
After figuring out the function , We need to imagine using the process , first , You must log in , If you haven't logged in before , Then you need to register .
Part of the code is as follows :
int jianchadengru()
{
char arr1[100] = { 0 };
char arr2[100] = { 0 };
char count[100] = { 0 };
char pass[100] = { 0 };
char date[100] = { 0 };
char type[100] = { 0 };
char mystring[400] = { 0 };
FILE* pf = fopen("test.txt", "r");
fgets(mystring, 400, pf);
fclose(pf);
jiemi(type, arr1, arr2, date, mystring);
//printf("%s", type);
printf(" Enter your account number :");
scanf("%s", &count);
printf(" Enter your password :");
scanf("%s", &pass);
if (strcmp(count, arr1) == 0 && strcmp(pass, arr2) == 0 && strcmp(type, "admin") == 0)
return 1;
return 0;
}
After go in , Need to write a simple menu .
void onlymenu()
{
printf("---------------------------------------\n");
printf("----- Password trustee ------\n");
printf("----- 1. Write new password ------\n");
printf("----- 2. Find a password ------\n");
printf("----- 3. Modify an account ------\n");
printf("----- 4. See all accounts ------\n");
printf("----- 0. Exit procedure ------\n");
printf("---------------------------------------\n");
printf("---------------------------------------\n");
printf("---------------------------------------\n");
}
void menu()
{
onlymenu();
int i = 0;
do
{
printf("\n Please select :");
scanf("%d", &i);
int k = 0;
switch (i)
{
case 1:
add_count();
break;
case 2:
chazhao();
break;
case 3:
xiugai();
break;
case 4:
check_all();
break;
case 0:
return;
default:
printf(" Wrong choice \n");
break;
}
} while (i);
}
Writing a new password is relatively simple , Just add write ok.
void add_count()
{
while (1)
{
char flag[20] = { 0 };
char count[100] = { 0 };
char pass[100] = { 0 };
char type[100] = { 0 };
printf(" Enter the account type you want to add ( Do not include spaces , Not more than 100 Characters ):");
scanf("%s", &type);
printf(" Enter the account you want to add ( Do not include spaces , Not more than 100 Characters ):");
scanf("%s", &count);
printf(" Enter the password of the account you want to add ( Do not include spaces , Not more than 100 Characters ):");
scanf("%s", &pass);
printf(" The account type you added is :%s, Account No :%s, The password for :%s\n Are you sure (YES):",type, count, pass);
scanf("%s", &flag);
if (strcmp(flag, "YES") == 0)
{
xieru(type, count, pass);
printf(" Write successfully !");
break;
}
}
}
void put_time(char ltime[100], int* i, int time_flag)
{
ltime[*i] = time_flag / 10 + '0';
(*i)++;
ltime[*i] = time_flag % 10 + '0';
(*i) += 3;
}
void get_time(char ltime[100])
{
time_t timep;
struct tm* p;
time(&timep);
p = gmtime(&timep);
int i = 0;
int year = 1900 + p->tm_year;
while (year > 0)
{
ltime[i] = (year % (int)pow(10, 4 - i)) / (int)pow(10, 3 - i) + '0';
i++;
year = year % (int)pow(10, 4 - i);
}
strcat(ltime, " year ");
i += 2;
char* week[7] = { " Monday "," Tuesday "," Wednesday "," Thursday "," Friday "," Saturday "," Sunday " };
put_time(ltime, &i, 1 + p->tm_mon);
strcat(ltime, " month ");
put_time(ltime, &i, p->tm_mday);
strcat(ltime, " Japan ");
i += 6;
strcat(ltime, week[p->tm_wday]);
put_time(ltime, &i, p->tm_hour + 8);
strcat(ltime, " when ");
put_time(ltime, &i, p->tm_min);
strcat(ltime, " branch ");
put_time(ltime, &i, p->tm_sec);
strcat(ltime, " second ");
}
void xieru(char type[100],char count[100],char pass[100])
{
FILE* pf = fopen("test.txt", "a");
int code1 = readom();
char ltime[100] = { 0 };
get_time(ltime);
jiami(ltime, (int)strlen(ltime), code1);
jiami(count, (int)strlen(count), code1);
jiami(pass, (int)strlen(pass), code1);
jiami(type, (int)strlen(type), code1);
char code = code1 + '0';
fputc(code, pf);
fputc(';', pf);
fputs(type, pf);
fputc(';', pf);
fputs(count, pf);
fputc(';', pf);
fputs(pass, pf);
fputc(';', pf);
fputs(ltime, pf);
fputc('\n', pf);
fclose(pf);
}
In this way, the writing automatically adds the system time .
Then there is the lookup function .
Lookup function , My idea is this , If you can find an account that fully matches the characteristics , Then there is no need to look down , If you don't find a perfect match , Then create an array , Record the overlapping size of each account and search account , Take the maximum to prompt .
int findmax(int arr[100])
{
int i = 0;
int max = 0;
int max_i = 0;
for (i = 0; i < 100; i++)
{
if (arr[i] > max)
{
max_i = i;
max = arr[i];
}
}
return max_i;
}
void chazhao()
{
char count[30][100] = { 0 };
char pass[30][100] = { 0 };
char type[30][100] = { 0 };
char date[30][100] = { 0 };
FILE* pf = fopen("test.txt", "r");
char mystring[400] = {0};
int i = 0;
int j = 0;
int tmp = 0;
if (pf == NULL)
{
printf(" Missing key files , Program error , Please contact the developer .\n");
system("pause");
return;
}
else
{
while (fgets(mystring, 400, pf) != NULL)
{
jiemi(type[i],count[i], pass[i],date[i], mystring);
i++;
}
fclose(pf);
}
int flag[100] = { 0 };
while (1)
{
printf(" Please enter the account type you want to find :");
char count1[100] = {0};
scanf("%s", &count1);
tmp = i;
int coin = 0;
for (i = 0; i < tmp; i++)
{
if (strcmp(count1, type[i]) == 0)
{
coin = 1;
printf(" Account No :%s\n The password for :%s\n",count[i], pass[i]);
return;
}
else
{
for (j = 0; j < 100; j++)
{
if (type[i][j] == count1[j] && j < strlen(count1) && j < strlen(type[i]))
{
flag[i]++;
}
}
}
}
if (coin != 1)
{
printf(" The account that fully matches the characteristics is not found \n The program guesses that you are looking for :%s Type account \n", type[findmax(flag)]);
}
}
}
Then modify the function , We can modify it like this , First write the modified value to the original file , Then find the location of the account number to be modified in the original file , Then create a new file , Copy the original file , But when it comes to modifying the location of the account , Just skip. .
void xiugai()
{
int i = 0;
while (1)
{
char type[100] = { 0 };
char count[100] = { 0 };
char pass[100] = { 0 };
char date[100] = { 0 };
int notice = 0;
char arr[100] = { 0 };
printf(" Please enter the account type to be modified :");
scanf("%s", &arr);
if (strcmp("admin",arr) == 0)
{
printf(" Modification of login account is not supported for the time being \n");
return;
}
i = 0;
int tmp = 0;
int j = 0;
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf(" Missing key files , Please contact the developer .");
return;
}
char mystring[400] = { 0 };
while (fgets(mystring, 400, pf) != NULL)
{
char count_tmp[100] = { 0 };
int key = mystring[0] - '0';
for (j = 2, tmp = 0; mystring[j] != ';'; j++, tmp++)
count_tmp[tmp] = mystring[j] + key;
if (strcmp(count_tmp, arr) == 0)
{
notice = 1;
fclose(pf);
jiemi(type, count, pass, date, mystring);
break;
}
else
i++;
}
fclose(pf);
if (notice == 0)
{
printf(" No fully matching account was found \n Please check this account first , Then enter the exact name !\n");
return;
}
else
{
char typetmp[100] = { 0 };
char counttmp[100] = { 0 };
char passtmp[100] = { 0 };
printf(" Please enter why you want to change the account type ( If it is not modified, enter quit):");
scanf("%s", &typetmp);
if (strcmp(typetmp, "quit") == 0)
strcpy(typetmp, type);
printf(" Please enter the reason for modifying the account ( If it is not modified, enter quit):");
scanf("%s", &counttmp);
if (strcmp(counttmp, "quit") == 0)
strcpy(counttmp, count);
printf(" Please enter the reason for changing the password ( If it is not modified, enter quit):");
scanf("%s", &passtmp);
if (strcmp(passtmp, "quit") == 0)
strcpy(passtmp, pass);
xieru(typetmp,counttmp,passtmp);
break;
}
}
move(i);
printf(" Modification successful !\n2s After the return \n");
Sleep(2000);
system("cls");
onlymenu();
}
void move(int i)
{
FILE* pf = fopen("test.txt", "r");
FILE* pf1 = fopen("tmp.txt", "w");
char mystring[400] = { 0 };
int j = 0;
int flag = i;
for (i = 0; fgets(mystring, 400, pf) != NULL; i++)
if (i != flag)
fputs(mystring, pf1);
fclose(pf);
fclose(pf1);
//fclose("test.txt");
remove("test.txt");
rename("tmp.txt","test.txt");
}
Then there is the function that displays all accounts , This is the simplest .
void check_all()
{
char type[30][100] = { 0 };
char date[30][100] = { 0 };
char pass[30][100] = { 0 };
char count[30][100] = { 0 };
FILE* pf = fopen("test.txt", "r");
char mystring[400] = { 0 };
int i = 0;
int tmp = 0;
if (pf == NULL)
{
printf(" Missing key files , Program error , Please contact the developer .(qq:2898239515)\n");
system("pause");
return;
}
else
{
while (fgets(mystring, 400, pf) != NULL)
{
jiemi(type[i], count[i], pass[i], date[i], mystring);
i++;
}
fclose(pf);
}
printf(" The account you are hosting here has the following :\n");
printf(" Account type ---- The last time the account was saved or modified \n");
for (tmp = 0; tmp < i; tmp++)
{
printf("%s", type[tmp]);
printf(" ");
printf("%s\n", date[tmp]);
}
}
Next is the most critical random encryption and decryption function , My current idea is to generate 10 A random number within is used as the random key , Then subtract the random number from all the characters , Get a new character , The whole data is encrypted . When decrypting , Read this key , Then add it back , It's solved .( Of course, I think this encryption method is very simple , I may use a more complicated encryption method later )
If the audience wants to experience , You can ask me for exe Oh .
边栏推荐
- Dllexport and dllimport
- 7-10 stack of hats (25 points) (C language solution)
- Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
- 7-16 find the set of integers that meet the given conditions
- 别再问自己适不适合做软件测试了
- dllexport和dllimport
- 7-1 positive integer a+b (15 points)
- Use of constraintlayout
- Use of form text box (I) select text
- Analysis of gene family characteristics - chromosome location analysis
猜你喜欢
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
分布式事务(Seata) 四大模式详解
dllexport和dllimport
puzzle(016.4)多米诺效应
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
Tonybot humanoid robot infrared remote control play 0630
Niuke: crossing the river
随机推荐
7-18 finding the single root of polynomial by dichotomy
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
Output student grades
关于敏捷的一些概念
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Puzzle (016.3) is inextricably linked
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
Statistical capital consonants
NPM install is stuck with various strange errors of node NPY
Dllexport and dllimport
Sword finger offer 28 Symmetric binary tree
【7.3】146. LRU缓存机制
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Zzuli:1040 sum of sequence 1
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
Zzuli:1054 monkeys eat peaches
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
ConstraintLayout 的使用
7-14 sum integer segments (C language)
7-17 crawling worms (break exercise)