954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Data encryption in C#

0
By Marivoet Daniel on Nov 25th, 2008 6:08 pm

Encryption has many issues. It can be very simple or very complex. I personally don’t have that many secrets, but when the company I worked for wanted to encrypt something I used the following : XOR !
What is nice about xor is that you can use the same method to encrypt and to decipher your message.
message xor key = secret
secret xor key = message
If you xor a string with one key-value, you always xoring the same char with the same key, so it is relatively easy to decipher the encrypted message. I made use of a key which is say 7 chars long (the longer the better), which I repeatedly xored over the messagestring. That way, the same chars where xored by different other chars. To decipher that is much harder. Look at the code snippet for more details.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StringEncryption
{
    class Program
    {
        
        static void Main(string[] args)
        {
            // the message to encrypt
            string messageStr = "Oh what a beautifull morning, oh what a beautifull day!";
            // the secret key used, can be anything make it at least 2 chars long
            string keyStr = "ABCDEFG";

            Console.WriteLine("Encrypted string:");
            StringBuilder OutStr = StringEncrypt(messageStr, keyStr);  
            Console.WriteLine(OutStr.ToString());

            Console.WriteLine("Deciphered string:");
            StringBuilder OutStr2 = StringEncrypt(OutStr.ToString(), keyStr);
            Console.WriteLine(OutStr2.ToString());
            Console.ReadKey();
        }

        private static StringBuilder StringEncrypt(string messageStr, string keyStr)
        {
            const int EOS = -1; //end of string condition

            StringReader Msr = new StringReader(messageStr);
            StringBuilder OutStr = new StringBuilder();
            // alternatively BinaryReader and BinaryWriter could be used here
            int keyLength = keyStr.Length - 1;
            int index = 0;
            char ch;
            int x;

            do
            {
                x = Msr.Read() ^ keyStr[index];     //XOR
                ch = Convert.ToChar(x);
                OutStr.Append(ch);
                index++;
                if (index > keyLength) index = 0;   //start all over with the key               
            } while (Msr.Peek() != EOS);

            return OutStr;
        }
    }
}

The char string method is cool, I just used the current position of the string as the XOR value, after some other ciphering, then inflating the string with gibberish to avoid them from knowing the size to reverse it, et al.; but I might try this technique out, since it's more compact, and not as analyzable by finding value averages.
Only bad part is that the XOR trick can be deciphered with loops(and other tricks), in seconds. :(

Good work, five stars!

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

Five stars (Y)

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You