Data encryption in C#

ddanbe 0 Tallied Votes 591 Views Share

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;
        }
    }
}
MosaicFuneral 812 Nearly a Posting Virtuoso

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!

Ramy Mahrous 401 Postaholic Featured Poster

Five stars (Y)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.