# XOR Encryption Decryption

XOR Encryption Decryption

Here we will see how can we perform a XOR Encryption Decryption on your data.

Before we start let me remind you XOR Encryption is not a secure way to store your data, its a very traditional way,
as it only transforms/convert your data from plain sight with the help of a password.
However I have customised my "XOR Encryption Decryption class" so that it cannot be easily decrypted.

However you can also integrate the XOR Encryption Decryption with other Encryption Decryption techniques for better security.

Before we start, let me briefly say what is XOR:
Lets get along with an example:

``sbyte a = 5;       sbyte b = 3; int xor = a^b;output : 5 ^ 3 = 6        ``

So how did the value of 5 ^ 3 became 6
As we know that
`a ^ b = XOR0 ^ 0 = 00 ^ 1 = 11 ^ 0 = 11 ^ 1 = 0`

So what happens in case of 5 ^ 3:
^(XOR operator)xor's the binary values of the operands(in this case 5,3)
`number binary 5 101 3 011so 5 ^ 3 : 1 0 1 0 1 1 ------- 1 1 0`
Here is your result : 110 is 6

So why to implement XOR Encryption Decryption? What's the use?
Because using the result & one of the operands here we can derive the other
For eg:
`5 ^ 3 = 6(result)6(result) ^ 3 = 56(result) ^ 5 = 3`

Now lets move onto the XOR Encryption Decryption in real time scenario.
First let me show how to encode/decode your texts to/from xor encrypted formats.
`using System;using System.Collections.Generic;using System.Text;`

using System.Text : For text manupulation

I have explained the process in the code itself, go through the code
`namespace XOREncDec{ static class XOREnc_Dec { //For base64 private static string _b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk.mnopqrstuvwxyz-123456789+/="; // Gets a base64 character and returns it's position in the base64 alphabet private static int GetNFromB64(char n) { return _b64.IndexOf(n); } // Gets a position in the base64 alphabet and returns it's base64 character. private static string GetB64FromN(int n) { if (n > _b64.Length) return "="; // well, we shouldn't reach this line. If we do, the encoding will be garbage anyway... return new string(_b64[n], 1); } // Gets a integer value and length of the binary string (e.g. 4, 8, 16). // returns the padded binary string private static string DecToBinary(int value, int length) { // Declare a few variables we're going to need string binString = ""; while (value > 0) { binString += value % 2; value /= 2; } // we need to reverse the binary string // that's why we are using this array here. string reverseString = ""; foreach (char c in binString) reverseString = new string((char)c, 1) + reverseString; binString = reverseString; // padding here binString = new string((char)'0', length - binString.Length) + binString; return binString; } // Gets the binary string and returns it's integer equivalent private static int BinToDec(string Binary) { return Convert.ToInt32(Binary, 2); }`

Here comes the Encryption
` // takes the data and password/key as input // Returns XOR text public static string Encode(string data, string key) { int keypos = 0; string binarydata = ""; //convert the sting to a 8bit binary data foreach (char c in data) { int xor = ((int)c ^ (int)key[keypos]) + (key.Length); Console.WriteLine("{0} = {1}\t{2} = {3}: {4} = {5}", c, (int)c, key[keypos], (int)key[keypos], xor, DecToBinary(xor, 8)); if (++keypos >= key.Length) keypos = 0; binarydata += DecToBinary(xor, 8); } int m = 0; string cipher = ""; // splitt the binary string to 4 byte chunks and assign each chunk the proper b64 value for (int i = 0; i < binarydata.Length; i += 4) { int v = BinToDec(binarydata.Substring(i, 4)); cipher += GetB64FromN(v * 4 + m); Console.WriteLine("{0}\t{1}\t{2} {3}", v, v * 4 + m, m, GetB64FromN(v * 4 + m)[0]); if (++m > 3) m = 0; } return cipher; }`

Here comes the Decryption
` // takes the XOR text and password/key as input // Returns the data in decrypted format public static string Decode(string data, string key) { int m = 0; string binarydata = ""; // convert the b64 string to binary string foreach (char c in data) { int v = (GetNFromB64(c) - m) / 4; binarydata += DecToBinary(v, 4); Console.WriteLine("{0}", DecToBinary(v, 4)); if (++m > 3) m = 0; } // chop the 8bit binaries and mix back the key into it int keypos = 0; string decoded = ""; for (int i = 0; i < binarydata.Length; i += 8) { if (i + 8 > binarydata.Length) break; int c = BinToDec(binarydata.Substring(i, 8)); int dc = (c - key.Length) ^ (int)key[keypos]; Console.WriteLine("{0} = {1}", binarydata.Substring(i, 8), c); Console.WriteLine(" {0} - {1} ^ {2} = {3}", c, key.Length - 1, (int)key[keypos], (c - key.Length) ^ (int)key[keypos]); if (++keypos >= key.Length) keypos = 0; decoded += new string((char)dc, 1); } return decoded; } }}`

How to implement?
`string My_XOR_text = XOREnc_Dec.Encode("input_text", "p@ss");//Here called the member function Encode() of XOREnc_Dec class.//My_XOR_text is the encrypted text, and can be written to a file or databasestring My_text = XOREnc_Dec.Decode(My_XOR_text, "p@ss");//Here called the member function Decode() of XOREnc_Dec class.//My_text is the decrypted text`

I have also attached a sample application, in which data with the password is XORed into a file and again retrived from the file for decryption and displays your data.

Best wishes
Paul

## Attachments

• XOR APP (40687-21829-XORApp.rar)