XOR encryption with c++
<p>I've implemented a fairly simple method to encrypt and decrypt string to a file and then back.
The methods im using look as following:</p>

<pre><code>string encrypt(string msg, string key) {
    string tmp(key);
    while (key.size() &lt; msg.size()) key += tmp;
    for (std::string::size_type i = 0; i &lt; msg.size(); ++i) msg[i] ^= key[i];
    return msg;
}

string decrypt(string msg, string key) {
    return encrypt(msg, key);
}
</code></pre>

<p>However some of the keys I am using are not very useful. The string gets encrypted correctly and written to a file. But when i try to decrypt everything (loading the file into a string, decrypt it, write is back to another file) the new file is significant smaller and doesnt contain the entire information stored in it.</p>

<p>The keys I've tried so far are:</p>

<pre><code>string crypt = "abc";                           //works kinda well
string crypt = "_H84M!-juJHFXGT01X1*G6a$gEv";   //doesnt work
string crypt = "H84MjuJHFXGT01X1G6agEv";        //doesnt work either
</code></pre>

<p>I am hoping you can help me and give me any advice on how to choose a usable key.</p>

<p>The code for the file handling:</p>

<pre><code>ofstream temp;
temp.open("temp", ios::in | ios::trunc);
temp &lt;&lt; encrypt(buffer, crypt);
temp.close();


ifstream in(file);
string content((std::istreambuf_iterator&lt;char&gt;(in))                (std::istreambuf_iterator&lt;char&gt;()));
ofstream plain;
plain.open(newfile, ios::in | ios::trunc);
plain &lt;&lt; decrypt(content, crypt);
plain.close();
</code></pre>