Problem With Encryption and Decryption Code

Well I had written a program which encrypts a file database.txt in same folder as program and can also decrypt it, But when trying to decrypt the encrypted file the file that returns to me is partially decrypted. My code is as follows -

Encryption-

char oldname[] ="Database.txt";
char newname[] ="DatabaseOriginal.txt";
rename( oldname , newname );
char pass[4];
int i=0;
strcpy(pass,"qwert");
ifstream infile;
infile.open("DatabaseOriginal.txt");//open the database file
ofstream outfile;
outfile.open("Database.txt");
while(infile.get(enc))
{
enc+=pass[i];
outfile<<enc;
i++;
i%=4;
}

infile.close(); //closes the original file
outfile.close();



Decryption -


char pass[4];
strcpy(pass,"qwert");
int i=0;
if (ifstream("Database.txt"))
{
rename("Database.txt","Database1.txt");
}
ifstream infile;
infile.open("Database1.txt");
ofstream outfile;
outfile.open("Database.txt");
while(infile.get(enc1))
{
enc1-=pass[i];
outfile<<enc1;
i++;
i%=4;
}
infile.close(); //closes the original file
outfile.close();


I cannot Understand why is the original file not returning on running the Decryption program.
Last edited on
Two obvious issues:
1
2
3
char pass[4];
int i=0;
strcpy(pass,"qwerty");
Your pass variable is too small so you end up with undefined behaviour.

Same here:
1
2
char pass[4];
strcpy(pass,"qwert");
Also you need to use the same password to decrypt.
Thanks for replying ,
I am sorry but I had written y in (qwerty) by mistake there and for the second mistake u told me , I tried to change the variable size to pass[5] but I am getting the same result.

Here is the output files I am Getting -

http://www27.zippyshare.com/v/xnYBRXrd/file.html
The problem is that plus and minus is not always bijective due to the numeric limit of char.

What are the characters that are not decrypted?

Another problem might be that the stream translates the line endings wrongfully.
Last edited on
Well when I try to encrypt a file every 4th character starting from the 1st is left unencrypted but while Decryption all the characters except those which were left the same get decrypted. So in short every 4th character does not gets decrypted, for more info you can see the output files I had attached in my previous post.
Topic archived. No new replies allowed.