| | |
Encryption application
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hi there! I'm working on this app that will allow you to encrypt or decrypt simple lines or entire txt files. It's no "real" encryption since it's easy to crack the output lines, but it's a fun project. So far I'm just done with the first option, and the funtions required to encrypt a simple line.
It seems to run fine long as the entered line is not longer than the password. If it is, I get the following error:
Unhandled exception at 0x7c812aeb in Encryptor.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012fb4c..
Also, if numbers are entered in the line, it seems like it wont be possible to decrypt them to numbers again. If the line "12" is entered using the pass "111111" it will encrypt into "bc". Does this have anything to do with how I handle the data? Should I use char arrays instead of strings?
Any help would be appreciated. Also, constructive comments about my code layout or any way I could make the code more effective would be nice
It seems to run fine long as the entered line is not longer than the password. If it is, I get the following error:
Unhandled exception at 0x7c812aeb in Encryptor.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012fb4c..
Also, if numbers are entered in the line, it seems like it wont be possible to decrypt them to numbers again. If the line "12" is entered using the pass "111111" it will encrypt into "bc". Does this have anything to do with how I handle the data? Should I use char arrays instead of strings?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> #include <conio.h> using namespace std; string lineInput (); string createPassword (); string encryptLine (string line, string password); int main() { bool loopHold = false; string menuChoice, tempLine, tempPassword; do { cout << "Welcome, do you wish to:\n" "1 - Encrypt\n" "2 - Decrypt\n" "3 - Exit\n" << endl; getline(cin, menuChoice); if (menuChoice == "1") { system("cls"); do { cout << "Options:\n" "1 - Encrypt a text string\n" "2 - Encrypt a text string and save it to a file\n" "3 - Encrypt a target file and save it to another\n" "4 - Back to previous menu\n" << endl; getline(cin, menuChoice); if (menuChoice == "1") { system("cls"); cout << "Enter the string to be encrypted, finish with #" << endl; tempLine = lineInput(); tempPassword = createPassword(); cout << encryptLine(tempLine, tempPassword) << endl; _getch(); } else if (menuChoice == "4") { system("cls"); break; } system("cls"); } while (loopHold); } else if (menuChoice == "3") { return 0; } system("cls"); } while (loopHold); return 0; } string lineInput () { string tempLine; char in; while (cin.get(in) && in != '#') { tempLine += in; } return tempLine; } string createPassword () { cin.ignore(1000, '\n'); string tempPassword, confirmPassword; bool loopHold; do { do { cout << "Enter desired password (min. 6 characters): "; getline(cin, tempPassword); if (tempPassword.size() < 6) { cout << "You must enter a longer password!" << endl; loopHold = true; } else { loopHold = false; } } while (loopHold); cout << "Confirm the password: "; getline(cin, confirmPassword); if (confirmPassword != tempPassword) { cout << "The passwords do not match!" << endl; loopHold = true; } else { loopHold = false; } } while (loopHold); return confirmPassword; } string encryptLine (string line, string password) { string encryptedLine; for (unsigned pos = 0, pos2 = 0; pos < line.size(); pos ++, pos2 ++) { encryptedLine += (line.at(pos) + password.at(pos2)); if (pos2 > password.size()) { pos2 = 0; } } return encryptedLine; }
Any help would be appreciated. Also, constructive comments about my code layout or any way I could make the code more effective would be nice
C++ Syntax (Toggle Plain Text)
encryptedLine += (line.at(pos) + password.at(pos2)); if (pos2 > password.size()) { pos2 = 0; }
Chris
Knowledge is power -- But experience is everything
Oh that simple, thanks! Working now:
Anyone knows about the character/number problem?
C++ Syntax (Toggle Plain Text)
if (pos2 >= password.size()) { pos2 = 0; } encryptedLine += (line.at(pos) + password.at(pos2));
Anyone knows about the character/number problem?
![]() |
Similar Threads
- How to create SHA1 Encryption program (VB.NET)
- XTEA Encryption (C++)
- Database encryption (ASP.NET)
- encryption (Java)
- DriveCrypt and Encryption (Community Introductions)
- blnWaitOnReturn of wshShell.Run has no effect if required application is already runn (Visual Basic 4 / 5 / 6)
- help with/removing window's encryption on my data (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Truble with GetDC()
- Next Thread: Queue problem
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





