Security : Encryption and Decryption from and to a string

This is article is about two simple Encryption and Decryption methods which I wrote using .NET Class Libraries.
When you provide a string as a parameter the EncryptFromString method returns an encrypted string. Similarly, when you give the encrypted string to the DecryptFromString method it returns the decrypted string.


Before you start you must have,
- Visual Studio 2005/2008 Installed

Step 1 : Create a Console Application
Then open the Program.cs and just after the Main method paste the following code,

-------------------------------------------------------------------------------------------------------------

//Encryption Method

      private static string EncryptToString(string str)
        {
            List<uint> objList = new List<uint>();
            StringBuilder strBuilder = new StringBuilder();
            uint btVal;
            foreach (char ch in str)
            {
                btVal = (uint)ch;
                btVal = btVal << 1;
                objList.Add(btVal);
              
            }

            objList.Reverse();

            for (int i=0;i<objList.Count;i++)
            {
                if (i + 1 <= objList.Count - 1)
                {
                    strBuilder.AppendFormat("{0}|{1}|", objList[i], objList[i + 1]);
                    i++;
                }            
            }

            if (objList.Count % 2 == 1)
            {
                strBuilder.AppendFormat("{0}|",objList[objList.Count - 1]);
            }

            

            string retString = strBuilder.ToString();

            return retString.Remove(retString.LastIndexOf('|'));
        }

-------------------------------------------------------------------------------------------------------------

//Decryption Method

   private static string DecryptFromString(string str)
        {
            string[] strSplitArr = str.Split(new char[] { '|' });
            uint btVal;
            List<uint> objList = new List<uint>();
            StringBuilder strBuilder = new StringBuilder();
            foreach (string strElem in strSplitArr)
            {
                btVal = uint.Parse(strElem);
                btVal = btVal >> 1;
                objList.Add(btVal);
            }

            objList.Reverse();

            for (int i = 0; i < objList.Count; i++)
            {
                if (i + 1 <= objList.Count - 1)
                {
                    strBuilder.AppendFormat("{0}{1}", (char)objList[i], (char)objList[i + 1]);
                    i++;
                }
            }

             if (objList.Count % 2 == 1)
            {
                  strBuilder.AppendFormat("{0}", (char)objList[objList.Count - 1]);
             }


            return strBuilder.ToString();
        }



In future we will look at how to add these functions to SQL Server as a User Defined Function.

Comments

Popular posts from this blog

Secure .NET Core Api Server and Client

Security tips for hosting .NET Web Application on IIS

Simple API using Python, Flask and pyodbc