C++控制檯實現密碼管理系統
阿新 • • 發佈:2020-11-30
本文例項為大家分享了C++控制檯實現密碼管理系統的具體程式碼,供大家參考,具體內容如下
功能介紹:
1.怎麼建立密碼,輸入兩次
2.怎麼修改密碼
3.怎麼刪除密碼
目錄
1.主介面
2. 功能程式碼
是不是有點意思,那還不ctrl-c ctrl-v 弄入你的IDE環境下,試下
// mima.cpp: 主專案檔案。 #include "stdafx.h" /// #include <iostream> #include <conio.h> #include <string.h> #include <fstream> //#include <windows.h> using namespace std; void display(); //主介面函式 void xuanze(); //選擇函式 int read_file(); //讀取密碼檔案 void write_file();//寫入密碼檔案 void Create_mima(); //建立密碼 void YanZheng_mima(); //驗證密碼 void Chang_mima(); //修改密碼 void delete_mima(); //刪除密碼 // void jiami_suanfa(char* str); //加密解密演算法 char mimaStr[100]; //全域性變數 //以上是函式的宣告部分 //下面是函式的實現部分 void display() //主介面函式 { system("cls"); read_file(); cout<<"\t\t************************************************"<<endl; cout<<"\t\t\t\t歡迎使console密碼系統"<<endl; cout<<"\t\t************************************************"<<endl; if(strlen(mimaStr)==0)cout<<"\t\t\t\t [1] 建立密碼"<<endl; else cout<<"\t\t\t\t 建立密碼"<<endl; //密碼已經存在就不能建立了 cout<<"\t\t\t\t [2] 驗證密碼"<<endl; cout<<"\t\t\t\t [3] 修改密碼"<<endl; cout<<"\t\t\t\t [4] 刪除密碼"<<endl; cout<<"\t\t\t\t [5] 退出系統"<<endl; cout<<"\t\t************************************************"<<endl; xuanze(); } void xuanze() { cout<<"\t\t請輸入你要進行的運算元: "; char ch; L: ch=getch(); if ((ch=='1' && strlen(mimaStr)==0) || ch=='2' || ch=='3' || ch=='4' || ch=='5') { switch(ch) { case '1':Create_mima(); break; case '2':YanZheng_mima(); break; case '3':Chang_mima(); break; case '4':delete_mima(); break; case '5':exit(0); break; } } else goto L; } int read_file() //讀取密碼檔案 { L: ifstream infile("MiMa_record.txt"); if (!infile) { write_file(); goto L; } else infile>>mimaStr; return 1; } void write_file()//寫入密碼檔案 { ofstream outfile("MiMa_record.txt"); if (!outfile) { cout<<"can not open the file!"<<endl; return; } else outfile<<mimaStr; } void jiami_suanfa(char* str) //加密解密演算法 { int len=strlen(str); for (int i=0;i<len;i++) str[i]=str[i]^'g'; } void Create_mima() //建立密碼 { system("cls"); char ch; int i=0; char str[100]; //確認密碼存放處 cout<<"請輸入新建密碼,按Enter結束(大於等於6位數): "; ch=getch(); while (i<100) { if (ch==13 && i>5)break; else if(ch==13)ch=getch(); else { cout<<"*"; mimaStr[i++]=ch; ch=getch(); } } mimaStr[i]='\0'; //結束標誌 i=0; cout<<endl<<"請輸入確認密碼,按Enter結束(大於等於6位數): "; //第二次輸入密碼 ch=getch(); while (i<100) { if (ch=='\r' && i>5)break; else if(ch=='\r')ch=getch(); else { cout<<"*"; str[i++]=ch; ch=getch(); } } str[i]='\0'; //結束標誌 if (strcmp(mimaStr,str)==0) { jiami_suanfa(mimaStr); write_file(); cout<<endl<<"建立密碼成功!,任意鍵返回..."<<endl; ch=getch(); display(); } else { cout<<"兩次輸入密碼不一樣,建立失敗! 繼續建立密碼按Enter,任意鍵返回..."<<endl; ch=getch(); if (ch=='\r')Create_mima(); else display(); } } void YanZheng_mima() //驗證密碼 { read_file(); system("cls"); char ch; char str[100]; int i=0; cout<<"請輸入你要驗證的密碼,Enter結束: "; ch=getch(); while (i<100) { if (ch=='\r' && i>5)break; else if(ch=='\r')ch=getch(); else { cout<<"*"; str[i++]=ch; ch=getch(); } } str[i]=0; cout<<endl; jiami_suanfa(mimaStr); //解密 if (strcmp(str,mimaStr)==0) { cout<<"恭喜!驗證成功!任意鍵返回..."<<endl; ch=getch(); display(); } else { cout<<"驗證不成功!按Enter繼續驗證,任意鍵返回..."<<endl; ch=getch(); if (ch=='\r')YanZheng_mima(); else display(); } } void Chang_mima() //修改密碼 { read_file(); system("cls"); char ch; char str[100]; int i=0; cout<<"請輸入原來的密碼,Enter結束: "; ch=getch(); while (i<100) { if (ch=='\r' && i>5)break; else if(ch=='\r')ch=getch(); else { cout<<"*"; str[i++]=ch; ch=getch(); } } str[i]='\0'; cout<<endl; i=0; jiami_suanfa(mimaStr); //解密 if (strcmp(str,mimaStr)==0) { cout<<endl<<"請輸入修改密碼,按Enter結束(大於等於6位數): "; ch=getch(); while (i<100) { if (ch=='\r' && i>5)break; else if(ch=='\r')ch=getch(); else { cout<<"*"; mimaStr[i++]=ch; ch=getch(); } } mimaStr[i]='\0'; //結束標誌 i=0; cout<<endl<<"請輸入確認密碼,str)==0) { jiami_suanfa(mimaStr); write_file(); cout<<endl<<"修改密碼成功!,任意鍵返回..."<<endl; ch=getch(); display(); } else { cout<<endl<<"兩次輸入密碼不一樣,修改失敗! 繼續修改密碼按Enter,任意鍵返回..."<<endl; ch=getch(); if (ch=='\r')Chang_mima(); else display(); } } else { cout<<endl<<"輸入密碼不匹配!你不能修改該密碼!任意鍵返回..."<<endl; ch=getch(); display(); } } void delete_mima() //刪除密碼 { read_file(); system("cls"); char ch; char str[100]; int i=0; cout<<"請輸入原來的密碼,mimaStr)==0) { cout<<"確定刪除請按'y'or'Y',任意鍵取消返回..."<<endl; ch=getch(); if (ch=='y' || ch=='Y') { mimaStr[0]='\0'; write_file(); cout<<"刪除成功,任意鍵返回..."<<endl; ch=getch(); display(); } else display(); } else { cout<<endl<<"輸入密碼不匹配!你不能刪除該密碼!任意鍵返回..."<<endl; ch=getch(); display(); } } //mian函式 void main() { display(); }
是不是和給出的效果一致呢, 以上的密碼只是簡單的異或操作加密,你可以在這基礎上加入你的專業級的加密演算法試試哈, 什麼 des aes都可以哈!
關於管理系統的更多內容請點選《管理系統專題》進行學習
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。