C# XOR Encryption

String Encryption

C# xor encryption is a primitive way to encrypt string data. It should never be used for secure encryption. However it is a simple way to hide meaningful text from plain sight.

Xor encryption does offer the use of a number key, making decryption only possible with said key.

Xor Operator

The basis of the encryption is the xor operator. In a nutshell, the xor operator works this way with numbers:

value1 xor value2 = value3
value3 xor value2 = value1

As you can probably tell, xor encryption will work based on value2 which in this case will be your number key.

Xor Encryption

The algorithm is simple, go through each character in the string, convert it to its integer ASCII value, xor the value with the key, and convert the new value back into a string.

To convert a character to an int, use:

Convert.ToInt32([char here]);

To convert an int to a char, use:

char.ConvertFromUtf32([int here]);

Since the xor operator is basically flipping values, you do not need a separate encryption and decryption function. Only one funciton is needed to flip the character values. Xor encryption is simple, but is a good introduction to string encryption.