Today we'll see how to Encrypt and Decrypt with and without key in asp.net
Step 1 :Add class file in app code folder
Step 2: We are going to use Cryptography so add in class file,
using System.Security.Cryptography;
using System.Text;
public static string Key
{
set
{
_key = value;
}
}
/// <summary>
/// Encrypt the given string using the default key.
/// </summary>
/// <param name="strToEncrypt">The string to be encrypted.</param>
/// <returns>The encrypted string.</returns>
public static string Encrypt(string strToEncrypt)
{
try
{
return Encrypt(strToEncrypt, _key);
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
/// <summary>
/// Decrypt the given string using the default key.
/// </summary>
/// <param name="strEncrypted">The string to be decrypted.</param>
/// <returns>The decrypted string.</returns>
public static string Decrypt(string strEncrypted)
{
try
{
return Decrypt(strEncrypted, _key);
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
/// <summary>
/// Encrypt the given string using the specified key.
/// </summary>
/// <param name="strToEncrypt">The string to be encrypted.</param>
/// <param name="strKey">The encryption key.</param>
/// <returns>The encrypted string.</returns>
public static string Encrypt(string strToEncrypt, string strKey)
{
try
{
TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteBuff;
string strTempKey = strKey;
byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
objHashMD5 = null;
objDESCrypto.Key = byteHash;
objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
return Convert.ToBase64String(objDESCrypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
/// Decrypt the given string using the specified key.
/// <param name="strEncrypted">The string to be decrypted.</param>
/// <param name="strKey">The decryption key.</param>
/// <returns>The decrypted string.</returns>
public static string Decrypt(string strEncrypted, string strKey)
{
try
{
TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash ;
byte[] byteBuff ;
string strTempKey = strKey;
byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
objHashMD5 = null;
objDESCrypto.Key = byteHash;
objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = Convert.FromBase64String(strEncrypted);
string strDecrypted = ASCIIEncoding.ASCII.GetString(objDESCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
objDESCrypto = null;
return strDecrypted;
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
}
3 step: Add default.aspx
<div>
<asp:CheckBox ID="chkIsCustomKey" runat="server"
oncheckedchanged="chkIsCustomKey_CheckedChanged" Text="Use my custom key"
AutoPostBack="True" />
<br />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Custom Key"></asp:Label>
<asp:TextBox ID="txtCustomKey" runat="server" Height="18px" Width="279px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Input text"></asp:Label>
<asp:TextBox ID="txtInputText" runat="server" Height="61px" Width="279px"></asp:TextBox>
<br />
<br />
<br />
<br />
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="OutPut Text"></asp:Label>
<asp:TextBox ID="txtOutputText" runat="server" Height="76px" Width="276px" ReadOnly="True"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Encrypt" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Decrypt" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Clear" />
<br />
<br />
</div>
step 4: In default.aspx.cs
ON Page_Load()
{
txtCustomKey.Enabled = false;
}
btnEncrypt_Click()
{
txtOutputText.Text = Class1.Encrypt(txtInputText.Text, SetKey());
}
btnDecrypt_Click()
{
txtOutputText.Text = Class1.Decrypt(txtInputText.Text, SetKey());
}
btnClear_Click()
{
chkIsCustomKey.Checked = false;
txtCustomKey.Text = String.Empty;
txtCustomKey.Enabled = false;
txtInputText.Text = String.Empty;
txtOutputText.Text = String.Empty;
}
chkIsCustomKey_CheckedChanged()
{
if (chkIsCustomKey.Checked)
{
txtCustomKey.Enabled = true;
}
else
{
txtCustomKey.Enabled = false;
txtCustomKey.Text = string.Empty;
}
}
OUTPUT:
Step 1 :Add class file in app code folder
Step 2: We are going to use Cryptography so add in class file,
using System.Security.Cryptography;
using System.Text;
public static string Key
{
set
{
_key = value;
}
}
/// <summary>
/// Encrypt the given string using the default key.
/// </summary>
/// <param name="strToEncrypt">The string to be encrypted.</param>
/// <returns>The encrypted string.</returns>
public static string Encrypt(string strToEncrypt)
{
try
{
return Encrypt(strToEncrypt, _key);
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
/// <summary>
/// Decrypt the given string using the default key.
/// </summary>
/// <param name="strEncrypted">The string to be decrypted.</param>
/// <returns>The decrypted string.</returns>
public static string Decrypt(string strEncrypted)
{
try
{
return Decrypt(strEncrypted, _key);
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
/// <summary>
/// Encrypt the given string using the specified key.
/// </summary>
/// <param name="strToEncrypt">The string to be encrypted.</param>
/// <param name="strKey">The encryption key.</param>
/// <returns>The encrypted string.</returns>
public static string Encrypt(string strToEncrypt, string strKey)
{
try
{
TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteBuff;
string strTempKey = strKey;
byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
objHashMD5 = null;
objDESCrypto.Key = byteHash;
objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
return Convert.ToBase64String(objDESCrypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
/// Decrypt the given string using the specified key.
/// <param name="strEncrypted">The string to be decrypted.</param>
/// <param name="strKey">The decryption key.</param>
/// <returns>The decrypted string.</returns>
public static string Decrypt(string strEncrypted, string strKey)
{
try
{
TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash ;
byte[] byteBuff ;
string strTempKey = strKey;
byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
objHashMD5 = null;
objDESCrypto.Key = byteHash;
objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = Convert.FromBase64String(strEncrypted);
string strDecrypted = ASCIIEncoding.ASCII.GetString(objDESCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
objDESCrypto = null;
return strDecrypted;
}
catch (Exception ex)
{
return "Wrong Input. " + ex.Message;
}
}
}
3 step: Add default.aspx
<div>
<asp:CheckBox ID="chkIsCustomKey" runat="server"
oncheckedchanged="chkIsCustomKey_CheckedChanged" Text="Use my custom key"
AutoPostBack="True" />
<br />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Custom Key"></asp:Label>
<asp:TextBox ID="txtCustomKey" runat="server" Height="18px" Width="279px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Input text"></asp:Label>
<asp:TextBox ID="txtInputText" runat="server" Height="61px" Width="279px"></asp:TextBox>
<br />
<br />
<br />
<br />
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="OutPut Text"></asp:Label>
<asp:TextBox ID="txtOutputText" runat="server" Height="76px" Width="276px" ReadOnly="True"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Encrypt" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Decrypt" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Clear" />
<br />
<br />
</div>
step 4: In default.aspx.cs
ON Page_Load()
{
txtCustomKey.Enabled = false;
}
private string SetKey()
{
if (chkIsCustomKey.Checked)
{
if (txtCustomKey.Text != String.Empty)
{
return txtCustomKey.Text;
}
else
{
return "SampleKey";
}
}
else
{
return "SampleKey";
}
}
{
txtOutputText.Text = Class1.Encrypt(txtInputText.Text, SetKey());
}
btnDecrypt_Click()
{
txtOutputText.Text = Class1.Decrypt(txtInputText.Text, SetKey());
}
btnClear_Click()
{
chkIsCustomKey.Checked = false;
txtCustomKey.Text = String.Empty;
txtCustomKey.Enabled = false;
txtInputText.Text = String.Empty;
txtOutputText.Text = String.Empty;
}
chkIsCustomKey_CheckedChanged()
{
if (chkIsCustomKey.Checked)
{
txtCustomKey.Enabled = true;
}
else
{
txtCustomKey.Enabled = false;
txtCustomKey.Text = string.Empty;
}
}
OUTPUT: