In my previous article we discussed about Symmetric Cryptography in general, today we discussed about how to implement Symmetric Cryptography in C#. .Net framework provides System.Security.Cryptography namespace to implement cryptography in C#.
As we discussed earlier the main components of Symmetric Cryptography are Plain Text, Encryption Algorithm, Secret Key, Cipher Text, and Decryption Algorithm. We are going to consider all these elements to implement Symmetric Cryptography in C#.
Open Microsoft Visual Studio 2013 => Create Asp.Net Web Application and name it as SymmetricCryptography.
Add three TextBoxes, one is for original plain text; second is for Cipher text and third is for plain text which generates after decrypting the Cipher text. And also include two buttons one is for Encryption and another is for Decryption as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SymmetricCryptography.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Symmetric Encryption in C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Plain Text</td>
<td></td>
<td>Cipher Text</td>
<td></td>
<td>Plain Text</td>
</tr>
<tr>
<td><asp:TextBox ID="txtPText1" Width="200px" Height="200px" TextMode="MultiLine" runat="server"></asp:TextBox></td>
<td><asp:Button ID="btnEncrypt" Text="Encryption" runat="server" OnClick="btnEncrypt_Click" /></td>
<td><asp:TextBox ID="txtCText" Width="200px" Height="200px" TextMode="MultiLine" runat="server"></asp:TextBox></td>
<td><asp:Button ID="btnDecrypt" Text="Decryption" runat="server" OnClick="btnDecrypt_Click" /></td>
<td><asp:TextBox ID="txtPText2" Width="200px" Height="200px" TextMode="MultiLine" runat="server"></asp:TextBox></td>
</tr>
</table>
</div>
</form>
</body>
</html>
System.Security.Cryptography is the main namespace to implement Symmetric Cryptography. Include System.IO and System.Text also as shown below.
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
Take any random text for secret key, remember this should be same encryption and decryption. Here we consider the “$#@4#23*” as secret key as shown below.
private string sSecretKey = "$#@4#23*";
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
byte[] sKeybytes;
protected void Page_Load(object sender, EventArgs e)
{
sKeybytes = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
}
Let’s implement Symmetric Encryption algorithm as shown below.
private string EncryptionAlgorithm(string sPlainText)
{
MemoryStream memoryStream = new MemoryStream();
CryptoStream crypstream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(sKeybytes, sKeybytes), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(crypstream);
sw.Write(sPlainText);
sw.Flush();
crypstream.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
As shown above we are using CryptoStream class to encrypt the plain text with the help of secret key.
Now implement Symmetric Decryption algorithm as shown below.
private string DecryptionAlgorithm(string sCipherText)
{
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(sCipherText));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(sKeybytes, sKeybytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
Here also we are using same secret key bytes to decrypt the Cipher text. The complete code to implement the Symmetric Cryptography is given below.
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SymmetricCryptography
{
public partial class WebForm1 : System.Web.UI.Page
{
private string sSecretKey = "$#@4#23*";
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
byte[] sKeybytes;
protected void Page_Load(object sender, EventArgs e)
{
sKeybytes = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
}
protected void btnEncrypt_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtPText1.Text))
{
txtCText.Text = EncryptionAlgorithm(txtPText1.Text);
}
}
protected void btnDecrypt_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtCText.Text))
{
txtPText2.Text = DecryptionAlgorithm(txtCText.Text);
}
}
private string EncryptionAlgorithm(string sPlainText)
{
MemoryStream memoryStream = new MemoryStream();
CryptoStream crypstream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(sKeybytes, sKeybytes), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(crypstream);
sw.Write(sPlainText);
sw.Flush();
crypstream.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
private string DecryptionAlgorithm(string sCipherText)
{
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(sCipherText));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(sKeybytes, sKeybytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
}
}
Run the application, enter some text on first text box and perform Encryption and Decryption. The output displays as shown below.
As shown above second text box displays Cipher text and third text box displays plain text which generated from Cipher text.
SymmetricCryptography.zip (25.7KB)