Asp.net programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this Asp.net and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Asp.net Website by URL, How to Play YouTube Videos in Asp.net by using Jquery , Jquery Change div background on mouseover in asp.net , JQuery UI Datepicker (Calendar) etc.

Thursday, June 25, 2015

Simple Method in C# Encryption and Decryption a String

Simple Method in C# Encryption and Decryption a String:



Encrypt and Decrypt is very important when we want to hide our data from others. When we are talking about web application then encryption and Decryption play most important rolls.
Encrypt and Decrypt in asp.net with C#:
Microsoft has a simple method to convert string in Encrypt and Decrypt at any time.

 Encryption and Decryption Simple Method:

string encryptedString = SomeStaticClass.Encrypt(sourceString);
string decryptedString = SomeStaticClass.Decrypt(encryptedString);

Example of Encrypt and Decrypt:

To use the code sample, you can copy the CryptorEngine to your project and start playing or copy method bodies and paste to your application projects.

I wrote a class to access the Encrypt and Decrypt Method. Because I wanted to use this method at many places. And this made my programming simple.

Basic codes encrypt and decrypt in asp.net


Encrypt and Decrypt Using Class

  public string DecryptString(string encrString)
    {
        byte[] b;
        string decrypted;
        try
        {
            b = Convert.FromBase64String(encrString);
            decrypted = System.Text.ASCIIEncoding.ASCII.GetString(b);
        }
        catch (FormatException fe)
        {
            decrypted = "";
        }
        return decrypted;
    }

    public string EnryptString(string strEncrypted)
    {
        byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted);
        string encrypted = Convert.ToBase64String(b);
        return encrypted;
    }
Still The above code is not enough for me because I want to do something with my choice. Then I had implemented the code with specify charter to use in Encrypt method. I have created two method class.
Advance Encrypt and Decrypt

Encrypt and Decrypt with Character Choice

        public string encrypt(string encryptString)
        {
            string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    encryptString = Convert.ToBase64String(ms.ToArray());
                }
            }
            return encryptString;
        }

        public string Decrypt(string cipherText)
        {
            string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            cipherText = cipherText.Replace(" ", "+");
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }



And At last the above code is totally perfect to make such a good application.



Other Asp.net with C# Related Post:



Calling JavaScript function from code behind ASP.NET:

TextChanged Event of C# TextChanged Event:

jQuery UI DatePicker (Calendar) Example in ASP.Net:

Advantages and Disadvantages of using 3 tier architecture:

How to Make a HTML Table by C# code in asp.net programming :

JQuery modal dialog with postbacks in ASP.NET:

 Check If checkbox is checked in asp.net using C#:

Asp.net checkboxlist control example:
Radio List Control bind from Sql database:


High Court Recruitment  Jobs 2014 

No comments:

Post a Comment