請隨身攜帶,因爲我現在只用了幾天的C++。閱讀加密的用戶名/密碼
我正在開發一個充當日記的控制檯應用程序。在這個階段,我正在開發登錄身份驗證,並且打了一下牆!因爲我將處理我的登錄和日記存儲的文本文件,所以我想從這些窺探中加密這些文本文件。
現在,問題是我不知道怎麼去解密>>檢查用戶& & pass >>再次加密。
難道是沿着這些路線?:
程序加載
解密password.txt的
如果在任何時候程序關閉,encryptFile()是跑了。
驗證用戶輸入
加密password.txt的
如果我沿着正確的線我怎麼去實施呢?我搜索了使用C++的文本文件的加密教程,並且他們不是很有幫助。
這是我剛剛開始使用的初學password.txt代碼,我應該從哪裏出發?如果有加密教程/文章,建議我錯過了,請發佈!
void checkPasswordFile() {
string username;
string password;
string passwordAgain;
string userIn;
string passIn;
string line;
ifstream passwordFile("passwords.txt");
istringstream instream;
if (passwordFile.good()) {
cout << "\t==================================" << endl;
cout << "\t----------------------------------" << endl;
cout << "\tYou are a returning user, please fill in your details" << endl;
while(!passwordFile.eof()) {
getline(passwordFile, line);
instream.clear();
instream.str(line);
username = line;
getline(passwordFile, line);
instream.clear();
instream.str(line);
password = line;
}
do {
cout << "Username: " << endl;
cin >> userIn;
cout << "Password: " << endl;
cin >> passIn;
if (userIn == username && passIn == password) {
displayMenu();
} else {
cout << "Username and Password Do Not Match, Try Again" << endl;
}
} while(userIn != username && passIn != password);
} else {
cout << "file no exist";
ofstream passwordFile;
passwordFile.open ("passwords.txt", ios:: in | ios::app);
cout << "\t==================================" << endl;
cout << "\t----------------------------------" << endl;
cout << "\tThis is your first run, please enter a username and password" << endl;
cout << "\tUsername: " << endl;
cin >> username;
cout << "\tPassword: " << endl;
cin >> password;
/*
Do Loop:
Prompts Re-Entry if PasswordAgain is not equal to Password
*/
do {
cout << "Re-Type Password: ";
cin >> passwordAgain;
if(password != passwordAgain) {
cout << "Passwords Do Not Match! Try Again" << endl;
}
} while(password != passwordAgain);
passwordFile << username << "\n";
passwordFile << password;
}
}
非常感謝您的寶貴時間。
PS爲我的生活,我不能找出如何做的:
用戶名:CIN >>用戶名]在同一控制檯線路,對不起增加了一倍,但認爲沒有一個足夠大問題爲自己的職位!謝謝。
編輯:
創建並存儲在文本文件時,我已成功能夠解密的用戶名和傳遞。然後,當用戶回來時,他們輸入的內容被加密並與文件進行比較。
問題是這隻適用於簡短的話,用戶通過工程,但用戶名和密碼不......任何想法爲什麼?這裏是我的加密代碼:
char encryptKey = 'h';
cout << "\tUsername: ";
cin >> userIn;
cout << "\tPassword: ";
cin >> passIn;
for (int i = 0; i < userIn.size(); i++) {
userIn[i] ^= encryptKey;
}
for (int x = 0; x < passIn.size(); x++) {
passIn[x] ^= encryptKey;
}
if (userIn == username && passIn == password) {
displayMenu();
} else {
cout << "\tUsername and Password Do Not Match, Try Again" << endl;
}
作爲一項建議,如果您從未將解密文件寫入磁盤將會更好。 – Wug 2012-07-12 23:22:39
假設你的二進制文件需要包含加密密鑰,所以你的應用程序對於任何知道如何使用調試器並閱讀asm的人來說都不會真的是安全的。 – ildjarn 2012-07-12 23:25:56
我只是在我的第一個星期的C++,這是一個個人項目,以提高知識,所以我很好,我只需要加密.txt文件,以便用戶不能只浸入程序以外的文件。 – speak 2012-07-12 23:29:31