Problems with XOR Encryption

I've been messing around with XOR encryption, one-time pads etc. and it's working fine with reading simple one line text files. The problem is with multi line texts. After encrypting and then decrypting it again, it would only output the first n characters. It seems to be unreliable every time I encrypt and decrypt.

I've posted the (shortened) code on pastebin because it's really long.
http://pastebin.com/RZNy3qBc

Plaintext:
Don't cry because it's over, smile because it happened.
I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.

Encrypted:
http://pastebin.com/NEBDu3mg
Decrypted:
Don't cry because it's ove
Last edited on
> #include "Includes.h"
I don't have that file

1
2
3
encryptStr();
decryptStr();
help();
don't have the definition of those functions

> Encrypted:
don't have the key

> OpenClipboard(0);
don't have windows.


After fixing all that, don't have your problem.
Wonder if there may be an issue in how windows handle the non-graphic characters in an std::string. Set a breakpoint on `decryptFile()' and see if you are reading all the data correctly.
I've re-coded the encryption program from scratch and it works perfectly now. :)

Just some notes for those who want to get into encrypting files.
- Open the file in binary mode
ifstream in("file.txt", ifstream::binary)
- Because it's in binary mode use the http://www.cplusplus.com/reference/istream/istream/read/ and http://www.cplusplus.com/reference/ostream/ostream/write/ functions.

Sample code to read from a file opened in binary mode:
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream in("file", ifstream::binary)

// should add a check to see if file is open

in.seekg(0, in.end);
size_t len = (size_t)in.tellg();
in.seekg(0);

char* data = new char[len];
in.read(data, len);

// add xor encryption function here
// save the encrypted file 


- Start with a simple algorithm first, then slowly add some "salt" to make it more advanced.
Last edited on
Topic archived. No new replies allowed.