Click here to Skip to main content
15,913,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
how to easy way of Encrypt and decrypt in asp.net?

Advance Wishes!!
Posted

1 solution

HI,
Its so easy.

Please refer below code for this.

Pass value in string which you want to encrypt or decrypt.

C#
using System.Text;
using System.Security.Cryptography;
using System.IO;

public static string EncryptQueryString(string str)
    {
        byte[] bytes = ASCIIEncoding.ASCII.GetBytes("proposal");
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
        StreamWriter writer = new StreamWriter(cryptoStream);
        writer.Write(str);
        writer.Flush();
        cryptoStream.FlushFinalBlock();
        writer.Flush();
        return HttpUtility.UrlEncode(Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length));
    }


 public static string DecryptQueryString(string cryptedstr)
    {
        cryptedstr = cryptedstr.Replace(" ", "+");
        byte[] bytes = ASCIIEncoding.ASCII.GetBytes("proposal");
        if (String.IsNullOrEmpty(cryptedstr))
        {
            throw new ArgumentNullException("String can not be null.");
        }
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedstr));
        CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
        StreamReader reader = new StreamReader(cryptoStream);
        return HttpUtility.UrlDecode(reader.ReadToEnd());
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900