user3627590
user3627590

Reputation: 281

Read from a simple encrypted file C++

I am trying to write a program which will output a XOR encrypted string to a file and will read this string and decrypt it back . To encrypt my string I have used a simple XOR Encryption : (thanks to Kyle W.Banks site)

string encryptDecrypt(string toEncrypt)
{
    char key = 'K'; //Any char will work
    string output = toEncrypt;

    for (int i = 0; i < toEncrypt.size(); i++)
        output[i] = toEncrypt[i] ^ key;

    return output;
}

Then In my program I use the following code to write and then read the string :

string encrypted = encryptDecrypt("Some text");
cout << "Encrypted:" << encrypted << "\n";

ofstream myFile("test.txt");
myFile << encrypted;

// Read all the txt file in binary mode to obtain the txt file in one string
streampos size;
char * memblock;
ifstream file ("test.txt", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
   size = file.tellg();
   memblock = new char [size];
   file.seekg (0, ios::beg);
   file.read (memblock, size);
   file.close();
}

//Convert the memblock into string and show the result of decrypted string
string result(memblock);
string decrypted = encryptDecrypt(result);
cout << "Decrypted:" << decrypted << "\n";

In result I have :

Encrypted : ,<.c%.;%
Decrypted : Õ52E65AD0

Maybe to save the file cause some problems into the byte saved so It can't retrieve the same byte when the program tried to read the string, but I'm not sure at all.

Best regards

Upvotes: 2

Views: 4384

Answers (3)

Namit Sinha
Namit Sinha

Reputation: 1445

As said by Zuppa it is dangerous to use it that way the string may terminate unexpectedly due to '\0'
you should post - calculate the length of the text you are dealing with it can be easily done by using stream.seekg(0,ios_base::end)

and you can use read and write functions to write or get the text from the file

ifstream file ("test.txt", ios::in|ios::binary|ios::ate);

file.seekg(0,ios::end);
int length=file.tellg();//length of the text in the file 
file.seekg(0);

char *memblock=new char[length];
file.read(memblock,length);

you may consult this Simple xor encryption

Upvotes: 0

Zuppa
Zuppa

Reputation: 477

Encryption with XOR is kind of dangerous. Assume your plain text contains the letter 'K', the encrypted string will contain a '\0' at this position. Your string will be cut off there.

Same thing for the other direction, you are reading the encrypted string. Converting the memory block to a string will result in a shorter string because std::string::string(const char*) will stop reading at '\0'.

Apart from that, memblock isn't initialized when the file could not be opened, so put the encryption part into the if (file.IsOpen()) clause.

Upvotes: 1

molbdnilo
molbdnilo

Reputation: 66371

Since you're not closing the output, there's a fair chance that your OS won't let you open the file for reading.
You're decrypting regardless of whether the file was successfully read.
If it wasn't successfully read, you'll have undefined behaviour due to memblock not being initialised - most likely getting a result constructed from random garbage data.

Once you get that fixed, you need to zero-terminate memblock to make it a "proper" C-style string.

Upvotes: 1

Related Questions