String encryption is a popular subject in all programming languages. A simple algorithm in C# for string encryption is based on the xor operator.
The xor operator is a form to flip between three separate number values. For example, say you have values A and B. The xor operator would work as follows:
A xor B = C
C xor B = A
The only way to get value C to convert back to A is with the previously used B value. In terms of encryption, that means value B will be the encryption and decryption key. Information that has the xor operator applied can only be recovered by knowing what value was originally applied.
There is a small problem, the xor operator only works for integer values and boolean values. In other to use the xor operator for string encryption we will need to convert text into numbers.
Luckily there is the ASCII table, which assigns a unique number value to each character used by computers.
Thus the C# algorithm comes down to converting each letter (character) in a string to a number, applying the xor operator, and converting back to a number.
The .Net Framework handles the conversion easily for programmers. Convert a char to an int with:
Convert.ToInt32() and backwards with char.ConvertFromUtf32().
The downfall of xor encryption is that it is too simple. Since it is done on a character by character basis the overall pattern of text is preserved, making long text relatively easy to work back into its original state. However, for a quick simple way to hide data from plain sight, it works well.
Source by Armando Pensado