Hi, I created a program that will read the text file you specify, and encrypt it with the key you specify. The encrypting stage works just fine, and the encrypted text is successfully saved to another text file. I've even tried decrypting it right after I've encrypted it in the same program, and it outputs the correct information. But if I close the program, and open again and try to decrypt it using the same key it either gives me a few of the words or just a mix of them, especially on long text files. I know that the problem is when it's reading it, but I don't know what else to do. Can anyone help me? Thanks. The code is below:
Code:#include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> #include <string.h> using namespace std; using std::string; string XOR(string value,string key) { string retval(value); int klen = key.length(); int vlen = value.length(); int k = 0, v = 0; for(v; v < vlen; v++) { retval[v] = value[v]^key[k]; k = (++k < klen); } return retval; } int main() { string value; string key; char file[256]; cout << "Please enter the key:\n"; cin >> key; cout << "Please enter the name of the file to encrypt:\n"; cin >> file; ifstream read(file); getline(read, value, 'X'); value = XOR (value, key); cout << "Data being encrypted...\n"; cout << "Done!\n"; ofstream os("Encrypted_Text.txt"); os << value; }



LinkBack URL
About LinkBacks



