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?
#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 :)