Published by
Jan 21, 2012 (last update: Jan 21, 2012)

XOR Encryption

Score: 3.1/5 (48 votes)
*****
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool XOR(const std::string& filePath, const std::string& outputPath, const std::string& key)
{
    std::ifstream in(filePath.c_str(), std::ios::binary);
    std::ifstream out(outputPath.c_str(), std::ios::binary);

    //files succesfully opened?
    if(!in || !out)
        return false;
    
    std::string::const_iterator keyChr = key.begin();
    
    //start encryption
    char chr;
    while(in.get(chr) && out)
    {
        out << char(chr ^ (*keyChr));.

        if(++keyChr == key.end())
            keyChr = key.begin();
    }
    
    in.close();
    out.close();
    
    return true;
}