c++ need help with caesar cipher decryption
Jan 5, 2014 at 7:23am UTC
I have text file that contains username and password
userandPassword.txt
1 2 3 4
chris ABC
jane DEF
john DEF
I am tryng to Decrypt the password using caesar cipher, with a key of 3 from the text file. which means that
if I key in username: chris and password:XYZ, it will login successfully.
however this is my password output after the decryption at this line of my codes
1 2 3 4 5 6 7 8 9
for (int x = 0; x < passwordInFile.length(); x++) {
output += caesarDecrypt(passwordInFile[x]);
}
cout << output << endl;
XYZ
XYZABC
XYZABCABC
expected output of password output after the decryption
this is what I so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
char caesarDecrypt( char c );
int main()
{
string line;
string userName;
string password;
string output = "" ;
string userNameInFile;
string passwordInFile;
ifstream readFile("userandPassword.txt" );
cout << "Enter UserName: " ;
cin >> userName;
cout << "Enter Password: " ;
cin >> password;
while (getline(readFile,line)) {
stringstream iss(line);
iss >> userNameInFile >> passwordInFile;
for (int x = 0; x < passwordInFile.length(); x++) {
output += caesarDecrypt(passwordInFile[x]);
}
cout << output << endl;
if (userName == userNameInFile && password == output) {
cout << "Login Successfully!" << endl;
}
}
}
char caesarDecrypt( char c )
{
if ( isalpha(c) )
{
c = toupper(c); //use upper to keep from having to use two seperate for A..Z a..z
c = (((c+65)-3) % 26) + 65;
}
//if c isn't alpha, just send it back.
return c;
}
Please help me.Thanks
Jan 5, 2014 at 10:36am UTC
You need to make the output string empty before handling the next password.
Topic archived. No new replies allowed.