#include <iostream>
#include <string>
using namespace std;
string xorEncrypt(string str, string key, int sPos, int kPos)
{ // recursively loop through each char in the string
if (sPos < str.length())
{ // repeat through the key if needed
if (kPos >= key.length())
{ kPos = 0;
}
// xor sucks, but it works for simple things
str[sPos] = str[sPos] ^ key[kPos];
return xorEncrypt(str, key, ++sPos, ++kPos);
}
else
{ return str;
}
}
// example usage
int main(int argc, char* argv[])
{ string str = "skaggles is more awesome than gabehabe";
string key = "boobies!";
cout << "original string: " << str << endl;
// the zeros signify the starting position for the string and key
string eStr = xorEncrypt(str, key, 0, 0);
cout << "encrypted string: " << eStr << endl;
// to decrypt, pass the encrypted string with the same key
string dStr = xorEncrypt(eStr, key, 0, 0);
cout << "decrypted string: " << dStr << endl;
system("PAUSE"); return 0;
}