Encryption and Decryption in ASP.NET with C#.Net(1024 -bit Encryption and Decryption)

Encryption and Decryption in ASP.NET with C#.Net

In most of the application we need to give security to the password, URL(security query strings) and more. For this purpose microsoft has provided System.Security.Cryptography.  In the below example i have encrypted "Ranga Rajesh Kumar" with 1024-bit encryption algorithm. when ever we required we will use 1024 bit decryption key for getting original string.

This is one of the good C#.Net Programming Logic
Program Explanation:



Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace EncriptDecript
{
    class Program
    {
        static void Main(string[] args)
        {
            string strText = EncryptIt("Ranga Rajesh Kumar");
            Console.WriteLine("Encrypted text:" + strText);
            strText = DecryptIt(strText);
            Console.WriteLine("Decrypted text:" + strText);
            Console.ReadLine();
        }
        public static string EncryptIt(string encryptData)
        {
            try
            {
                byte[] data = ASCIIEncoding.ASCII.GetBytes(encryptData);
                byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes("12345678");
                byte[] rgbIV = ASCIIEncoding.ASCII.GetBytes("87654321");
                //1024-bit encryption
                MemoryStream memoryStream = new MemoryStream(1024);
                DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, desCryptoServiceProvider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.FlushFinalBlock();
                byte[] result = new byte[(int)memoryStream.Position];
                memoryStream.Position = 0;
                memoryStream.Read(result, 0, result.Length);
                cryptoStream.Close();
                memoryStream.Dispose();
                return Convert.ToBase64String(result);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static string DecryptIt(string toDecrypt)
        {
            string decrypted = string.Empty;
            try
            {
                    byte[] data = System.Convert.FromBase64String(toDecrypt);
                    byte[] rgbKey = System.Text.ASCIIEncoding.ASCII.GetBytes("12345678");
                    byte[] rgbIV = System.Text.ASCIIEncoding.ASCII.GetBytes("87654321");
                    //1024-bit decryption
                    MemoryStream memoryStream = new MemoryStream(data.Length);
                    DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider();
                    ICryptoTransform x = desCryptoServiceProvider.CreateDecryptor(rgbKey, rgbIV);
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, x, CryptoStreamMode.Read);
                    memoryStream.Write(data, 0, data.Length);
                    memoryStream.Position = 0;
                    decrypted = new StreamReader(cryptoStream).ReadToEnd();
                    cryptoStream.Close();
                    memoryStream.Dispose();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return decrypted;
        }
    }

}


Oupput:
Encryption and Decryption in ASP.NET with C#.Net(1024 -bit Encryption and Decryption)
Encryption and Decryption in ASP.NET with C#.Net(1024 -bit Encryption and Decryption)

See more ASP.NET, C#, SQL Server, JQuery Interview Questions

If you have any queries or suggestions, please feel free to ask in comments section.
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar