C語(yǔ)言實(shí)現(xiàn)密碼本小項(xiàng)目
一、引言
學(xué)C語(yǔ)言有一段時(shí)間了,趁著正好做了密碼本的小項(xiàng)目,把它分享出來(lái)。
二、思路與原理
密碼本,見(jiàn)名知意,就是存放賬號(hào)密碼,起到備忘錄作用的本子,將需要備忘的數(shù)據(jù)通過(guò)加密存放在文本文件中,打開(kāi)的文本文件為加密文本,需要通過(guò)軟件查看已經(jīng)存放的數(shù)據(jù),提高安全性。(軟件設(shè)計(jì)了啟動(dòng)密碼,三次過(guò)后自動(dòng)退出)
項(xiàng)目目標(biāo):
- 保存賬號(hào)密碼,退出后打開(kāi)軟件數(shù)據(jù)不丟失
- 進(jìn)行簡(jiǎn)單加密
- 能按條件查找賬號(hào)密碼
- 能修改賬號(hào)密碼數(shù)據(jù)
- 能按條件進(jìn)行刪除數(shù)據(jù)
知識(shí)涉及到數(shù)組、文件操作的使用以及簡(jiǎn)單異或加密。
軟件的部分截圖如下:

主菜單

增加數(shù)據(jù)

刪除數(shù)據(jù)

查詢數(shù)據(jù)

全部數(shù)據(jù)
三、實(shí)現(xiàn)
1.頭文件及數(shù)據(jù)定義
在密碼本的小項(xiàng)目中我設(shè)立四個(gè)了存放數(shù)據(jù)的數(shù)組,所存放的賬號(hào)密碼的站點(diǎn),存放的賬號(hào),存放的密碼以及備注。定義了一個(gè)全局變量,用來(lái)計(jì)數(shù)當(dāng)前密碼本使用的條數(shù)。
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> //定義四個(gè)數(shù)組,用來(lái)存放站點(diǎn)、賬號(hào)、密碼、備注的值 char webSite[100][20]; char userName[100][20]; char passWord[100][20]; char reMark[100][20]; //定義一個(gè)全局變量 int g_count = 0;
2.程序的整體框架
int main(void)
{
InitInfo();
int nChoice = 1;
StartPassWord();
while (nChoice)
{
printf("*********************************歡迎使用密碼本*********************************\n");
printf(" 請(qǐng)輸入以下選項(xiàng)進(jìn)行操作: \n");
printf(" 1、增加數(shù)據(jù) \n");
printf(" 2、刪除數(shù)據(jù) \n");
printf(" 3、修改數(shù)據(jù) \n");
printf(" 4、查詢數(shù)據(jù) \n");
printf(" 5、全部數(shù)據(jù) \n");
printf(" 6、結(jié)束程序 \n");
printf("********************************************************************************\n");
scanf_s("%d", &nChoice);
switch (nChoice)
{
case 1:
{
AddInfo();//增加數(shù)據(jù)
}break;
case 2:
{
DelInfo(); //刪除數(shù)據(jù)
}break;
case 3:
{
EditInfo();//修改數(shù)據(jù)
}break;
case 4:
{
FindInfo();//查詢數(shù)據(jù)
}break;
case 5:
{
ReadAllInfo();//查看全部
}break;
case 6:
{
nChoice = 0;//退出程序
}break;
}
}
printf("歡迎下次使用!\n");
system("pause");
return 0;
}
3.函數(shù)的聲明
//啟動(dòng)時(shí)輸入密碼 void StartPassWord(); //增加數(shù)據(jù) void AddInfo(); //刪除數(shù)據(jù) void DelInfo(); //修改數(shù)據(jù) void EditInfo(); //查詢數(shù)據(jù) void FindInfo(); //讀取全部數(shù)據(jù) void ReadAllInfo(); //保存數(shù)據(jù) void SaveInfo(); //初始化數(shù)據(jù) void InitInfo();
4.各個(gè)函數(shù)的實(shí)現(xiàn)
//增加數(shù)據(jù)
void AddInfo()
{
printf("輸入需要存儲(chǔ)的 站點(diǎn):");
scanf_s("%s", webSite[g_count], 20);
printf("輸入需要存儲(chǔ)的 賬號(hào):");
scanf_s("%s", userName[g_count], 20);
printf("輸入需要存儲(chǔ)的 密碼:");
scanf_s("%s", passWord[g_count], 20);
printf("輸入需要存儲(chǔ)的 備注:");
scanf_s("%s", reMark[g_count], 20);
g_count++;
SaveInfo();
InitInfo();
system("pause");
system("cls");
}
//查看全部
void ReadAllInfo()
{
FILE *pfile;
if (fopen_s(&pfile, "data.i", "r") != 0)
{
printf("file open fail");
exit(1);
}
fscanf_s(pfile, "當(dāng)前個(gè)數(shù)%d\n", &g_count);
printf("當(dāng)前有%d條數(shù)據(jù)\n", g_count);
for (int i = 0; i < g_count; i++)
{
printf("第%d條數(shù)據(jù):\n", i + 1);
printf("站點(diǎn):%s,賬號(hào):%s,密碼:%s,備注:%s",
webSite[i], userName[i], passWord[i], reMark[i]);
printf("\n");
}
fclose(pfile);
system("pause");
system("cls");
}
//保存數(shù)據(jù)
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, "當(dāng)前個(gè)數(shù)%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);
}
//刪除數(shù)據(jù)
void DelInfo()
{
char aTemp[20];
int nFind = 0;
int nIndex = 0;
printf("請(qǐng)輸入要?jiǎng)h除的站點(diǎn):\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("刪除成功\n");
}
else
{
printf("查找失??!\n");
}
system("pause");
system("cls");
}
//初始化數(shù)據(jù)
void InitInfo()
{
FILE *pfile;
if (fopen_s(&pfile, "data.i", "r") != 0)
{
printf("請(qǐng)先建立data.i文件!");
system("pause");
exit(1);
}
fscanf_s(pfile, "當(dāng)前個(gè)數(shù)%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);
}
//查詢數(shù)據(jù)
void FindInfo()
{
char aTemp[20];
int nFind = 0;
printf("請(qǐng)輸入要查詢的站點(diǎn):\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("站點(diǎn):%s,賬號(hào):%s,密碼:%s,備注:%s\n",
webSite[nIndex], userName[nIndex], passWord[nIndex], reMark[nIndex]);
break;
}
}
if (nFind == 0)
{
printf("查找失?。n");
}
system("pause");
system("cls");
}
//修改數(shù)據(jù)
void EditInfo()
{
char aTemp[20];
int nFind = 0;
printf("請(qǐng)輸入要修改的站點(diǎn):\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("輸入需要修改的 賬號(hào):");
scanf_s("%s", userName[nIndex], 20);
printf("輸入需要修改的 密碼:");
scanf_s("%s", passWord[nIndex], 20);
printf("輸入需要修改的 備注:");
scanf_s("%s", reMark[nIndex], 20);
printf("站點(diǎn):%s,賬號(hào):%s,密碼:%s,備注:%s\n",
webSite[nIndex], userName[nIndex], passWord[nIndex], reMark[nIndex]);
SaveInfo();
break;
}
}
if (nFind == 0)
{
printf("查找失??!\n");
}
system("pause");
system("cls");
}
//啟動(dòng)時(shí)輸入密碼
void StartPassWord()
{
char spw[10] = "abcd"; //程序的啟動(dòng)密碼
char write[10] = "";
int nSuccess = 0;
for (int i = 1; i <= 3; i++)
{
printf("請(qǐng)輸入啟動(dòng)密碼:\n");
scanf_s("%s", write, 10);
if (strcmp(write, spw) == 0)
{
nSuccess = 1;
break;
}
else
{
printf("你的輸入有誤!還有%d次機(jī)會(huì)\n", 3 - i);
system("pause");
system("cls");
}
}
if (nSuccess == 1)
{
printf("輸入正確!按任意鍵開(kāi)始使用程序\n");
}
else
{
printf("輸入全部錯(cuò)誤!按任意鍵結(jié)束程序\n");
system("pause");
exit(1);
}
system("pause");
system("cls");
}
5.關(guān)于加密
異或加密:異或是一種運(yùn)算方式,在C語(yǔ)言中是^符號(hào),通常可以用于加密。
例如項(xiàng)目中的實(shí)現(xià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;
}
}
四、總結(jié)
程序中還有很有缺陷,第一次寫(xiě)博客,希望大家多多包涵!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++ 公有繼承、保護(hù)繼承和私有繼承的對(duì)比詳解
這篇文章主要介紹了C/C++ 公有繼承、保護(hù)繼承和私有繼承的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-02-02
詳解C語(yǔ)言結(jié)構(gòu)體中的char數(shù)組如何賦值
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言結(jié)構(gòu)體中的char數(shù)組如何賦值的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
C++實(shí)現(xiàn)LeetCode(144.二叉樹(shù)的先序遍歷)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(144.二叉樹(shù)的先序遍歷),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++容器適配與棧的實(shí)現(xiàn)及dequeque和優(yōu)先級(jí)詳解
這篇文章主要介紹了C++容器適配與棧的實(shí)現(xiàn)及dequeque和優(yōu)先級(jí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10
C++ ffmpeg硬件解碼的實(shí)現(xiàn)方法
這篇文章主要介紹了C++ ffmpeg硬件解碼的實(shí)現(xiàn),對(duì)FFmpeg多媒體解決方案中的視頻編解碼流程進(jìn)行研究。為嵌入式多媒體開(kāi)發(fā)提供參考,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
VC實(shí)現(xiàn)圖片拖拽及動(dòng)畫(huà)的實(shí)例
這篇文章介紹了VC實(shí)現(xiàn)圖片拖拽及動(dòng)畫(huà)的實(shí)例,有需要的朋友可以參考一下2013-08-08
Qt之使用GraphicsView框架實(shí)現(xiàn)思維導(dǎo)圖的示例
思維導(dǎo)圖可以更方便的整理知識(shí),本文主要介紹了Qt之使用GraphicsView框架實(shí)現(xiàn)思維導(dǎo)圖的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C語(yǔ)言全方位講解指針與地址和數(shù)組函數(shù)堆空間的關(guān)系
指針是C語(yǔ)言中一個(gè)非常重要的概念,也是C語(yǔ)言的特色之一。使用指針可以對(duì)復(fù)雜數(shù)據(jù)進(jìn)行處理,能對(duì)計(jì)算機(jī)的內(nèi)存分配進(jìn)行控制,在函數(shù)調(diào)用中使用指針還可以返回多個(gè)值2022-04-04
詳解C++中的ANSI與Unicode和UTF8三種字符編碼基本原理與相互轉(zhuǎn)換
在C++編程中,我們有時(shí)需要去處理字符串編碼的相關(guān)問(wèn)題,常見(jiàn)的字符編碼有ANSI窄字節(jié)編碼、Unicode寬字節(jié)編碼及UTF8可變長(zhǎng)編碼。很多人在處理字符串編碼問(wèn)題時(shí)都會(huì)有疑惑,即便是有多年工作經(jīng)驗(yàn)的朋友也可能搞不清楚。所以有必要講一下這三種字符編碼以及如何去使用它們2021-11-11

