XOR Encryption
In cryptography, XOR Encryption, also known as XOR Cipher, is a encryption algorithm. With this algorithm, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher.
public static string XORCipher(string data, string key)
{
int dataLen = data.Length;
int keyLen = key.Length;
char[] output = new char[dataLen];
for (int i = 0; i < dataLen; ++i)
{
output[i] = (char)(data[i] ^ key[i % keyLen]);
}
return new string(output);
}
Example
string text = "The quick brown fox jumps over the lazy dog.";
string key = "secret";
string cipherText = XORCipher(text, key);
string plainText = XORCipher(cipherText, key);
Output
cipherText: "'\r\u0006R\u0014\u0001\u001a\u0006\bR\a\u0006\u001c\u0012\rR\u0003\u001b\vE\t\a\b\u0004\0E\f\u0004\0\u0006S\u0011\v\u0017E\u0018\u0012\u001f\u001aR\u0001\u001b\u0014K"
plainText: "The quick brown fox jumps over the lazy dog."