XOR (Exclusive OR, EOR, EXOR) is a logical operation which results to a true value if exactly one of the two operands has a value of true.
Sample XOR Table
| INPUT | OUTPUT | |
| A | B | A ^ B |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Using this knowledge we can encrypt characters using their binary values. Take for example the string “ENCRYPT”. First, we take the ASCII Code of each character in this string and convert each to their respective binary value.
E - 69 - 1000101
N - 78 - 1001110
C - 67 - 1000011
R - 82 - 1010010
Y - 89 - 1011001
P - 80 - 1010000
T - 84 - 1010100
Note: Small letters have different ASCII values.
Choose a random encryption key, say “X”. X has an ASCII Code of 88 and binary value of 1011000. If we perform XOR on “ENCRYPT” against “X”, we get:
1000101 ^ 1011000 = 0001101 [29]
1001110 ^ 1011000 = 0010110 [22]
1000011 ^ 1011000 = 0011011 [27]
1010010 ^ 1011000 = 0001010 [10]
1011001 ^ 1011000 = 0000001 [1]
1010000 ^ 1011000 = 0001000 [8]
1010100 ^ 1011000 = 0001100 [12]
Looking up these numbers in ASCII Code Table, we get:

Thus, the string “ENCRYPT” will be encrypted to:

Putting it in Codes [C#]
Encrypt:
public static string Encrypt(string orgStr, char keyChar)
{
string encrypted = “”;
for (int i = 0; i < orgStr.Length; i++)
{
encrypted += (char)(orgStr[i] ^ keyChar);
}return encrypted;
}
Decrypt:
public static string Decrypt(string orgStr, char keyChar)
{
string decrypted = “”;
foreach (char encChar in orgStr.ToCharArray())
{
decrypted += (char)(encChar ^ keyChar);
}return decrypted;
}
Usage:
string encStr = Encrypt(”TEST”, ‘X’);
Console.WriteLine(”Original String: TEST”);
Console.WriteLine(”Encrypted String: ” + encStr);
Console.WriteLine(”Decrypted String: ” + Decrypt(encStr, ‘X’));
Result:

===================
.Net asp .net tutorial asp .net tutorial c# sample code CSS sample script database guide java sample code javascript sample code mssql sample script string reverse in C# t-sql telerik sample code user-defined function vb .net sample code visual studio tutorial
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.
RSS feed for comments on this post · TrackBack URI
Leave a reply