Sunday, February 10, 2013

XOR Cipher


Hi guys I would like to publish a simple way to create your own cipher using c#, here is complete code for it....


class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //XORCipher objXORCipher = new XORCipher("dinesh", "0123456789ABCDEF");
                XORCipher objXORCipher = new XORCipher();
                String textToEncrypt = "This is Test String...";
                String encText = objXORCipher.Encrypt(textToEncrypt);
                String finalText = objXORCipher.Decrypt(encText);
                Console.Write("\n\nOriginal  Text : " + textToEncrypt);
                Console.Write("\n\nEncrypted Text : " + encText);
                Console.Write("\n\nDecrypted Text : " + finalText);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.Write("\n\nDone...");
            Console.ReadKey();
        }
    }

    public class XORCipher
    {
        char[] key = new char[0];
        String cipherString = String.Empty;

        public XORCipher()
        {
            key = "DINESH".ToCharArray();
            cipherString = "0123456789ABCDEF";
        }

        public XORCipher(String key, String cipherString)
        {
            this.key = key.ToCharArray();
         
            if (cipherString.Length != 16)
            {
                throw new Exception("Invalid cipher string");
            }

            this.cipherString = cipherString;
        }

        public String Encrypt(String textToEncrypt)
        {
            String val = String.Empty;
            char[] myString = textToEncrypt.ToCharArray();

            byte[] demoString = new byte[myString.Length];

            for (int i = 0; i < demoString.Length; i++)
            {
                demoString[i] = (byte)(myString[i] ^ key[i % key.Length]);
            }

            foreach (byte ok in demoString)
            {
                String demo = DecimalToBinary((byte)ok);
                for (int i = 0; i < (demo.Length / 4); i++)
                {
                    //Console.Write(" " + demo.Substring(i * 4, 4));
                    val += cipherString.Substring(Convert.ToInt16(demo.Substring(i * 4, 4), 2), 1);
                }
            }

            return val;
        }

        public String Decrypt(String textToDecrypt)
        {
            if (textToDecrypt.Length % 2 != 0)
            {
                throw new Exception("Invalid text to decrypt");
            }

            String val = textToDecrypt;
            String decString = String.Empty;
            for (int i = 0; i < val.Length; i = i + 2)
            {
                String highNibble = DecimalToBinary((byte)(cipherString.IndexOf(val.Substring(i, 1)))).Substring(4);
                String lowNibble = DecimalToBinary((byte)(cipherString.IndexOf(val.Substring(i + 1, 1)))).Substring(4);
                char ok = (char)(Convert.ToInt16(highNibble + lowNibble, 2));
                decString += (char)(ok ^ key[(i / 2) % key.Length]);
            }

            return decString;
        }

        public String DecimalToBinary(byte decNumber)
        {
            int maxSize = (sizeof(byte)) * 8;
            String val = String.Empty;
            byte myVal = (byte)decNumber;

            for (int i = 0; i < maxSize; i++)
            {
                val = "" + (myVal % 2 == 0 ? 0 : 1) + val;
                myVal = (byte)(myVal / 2);
            }
         
            return val;
        }
    }

No comments:

Post a Comment

Age Calculator - Calculating difference between dates

Dear friends in most of the cases you need to work on dates. I had been stuck while calculating exact age of a person or total experience i...