Encryption / Decryption in C#

Introduction:

As as web Developer some times you need encryption and Decryption in c#. for this purpose a quick code snap is given below.


Solution:

C# Encryption Function:


public static string Encrypt(string textToEncrypt, int key)
    {
        StringBuilder inSb = new StringBuilder(textToEncrypt);
        StringBuilder outSb = new StringBuilder(textToEncrypt.Length);
        char c;
        for (int i = 0; i < textToEncrypt.Length; i++)
        {
            c = inSb[i];
      
            if (c <= 32)
            {
           
            }
            else
            {
                c = (char)((c - key));     
            }
            outSb.Append(c);
        }
        return outSb.ToString();
    }




C# Decryption Function:

public static string Decrypt(string textToEncrypt, int key)
    {
        StringBuilder inSb = new StringBuilder(textToEncrypt);
        StringBuilder outSb = new StringBuilder(textToEncrypt.Length);
        char c;
        for (int i = 0; i < textToEncrypt.Length; i++)
        {
            c = inSb[i];     

            if (c <= 32)
            {
         
            }
            else
            {
                c = (char)((c + key));   
            }
            outSb.Append(c);
        }
        return outSb.ToString();


    }

Key points for use:
  • Provide same key in both function for Encryption and Decryption.

Follow us on below channels to stay updated for new post and tutorials:
Blog: http://easytutorials4all.blogspot.com
Google+ : https://plus.google.com/u/0/112994256924713874536
Facebook Page : https://facebook.com/EasyTutorials4All
Youtube: https://www.youtube.com/channel/UCod8G-TeTUTz9DjaqK1C5-g

Comments

Popular posts from this blog

Encrypt data in browser with Javascript and Decrypt on server with asp.net c#