Method (1) :
NameSpaces :
using System.Security.Cryptography;
using System.Text;
using System.IO;
HTML :
<p>
Current Guid :
<asp:Label ID="lblGuid" runat="server" ></asp:Label>
</p>
<p>
User Password :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="btnEncryption" runat="server"
Text="Encryption" onclick="btnEncryption_Click" />
<asp:Button ID="btnDecryption" runat="server"
Text="Decryption" onclick="btnDecryption_Click" />
</p>
<p>
Encrypting Password :
<asp:Label ID="lblEncrypting" runat="server" ></asp:Label>
</p>
<p>
Decrypting Password :
<asp:Label ID="lblDecrypting" runat="server" ></asp:Label>
</p>
Code-Behind :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Guid passwordSalt = System.Guid.NewGuid();
lblGuid.Text = passwordSalt.ToString();
}
}
protected void btnEncryption_Click(object sender, EventArgs e)
{
lblEncrypting.Text = Encryption_decryption.Encrypt(TextBox1.Text, Convert.ToString(lblGuid.Text));
}
protected void btnDecryption_Click(object sender, EventArgs e)
{
lblDecrypting.Text = Encryption_decryption.Decrypt(TextBox1.Text, Convert.ToString(lblGuid.Text));
}
Encryption-Decryption Class :
public class Encryption_decryption
{
public static string Decrypt(string TextToBeDecrypted, string SaltContaint)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
//string SaltContaint = "CSC";
string DecryptedData;
try
{
byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);
byte[] Salt = Encoding.ASCII.GetBytes(SaltContaint.Length.ToString());
//Making of the key for decryption
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(SaltContaint, Salt);
//Creates a symmetric Rijndael decryptor object.
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(EncryptedData);
//Defines the cryptographics stream for decryption.THe stream contains decrpted data
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
byte[] PlainText = new byte[EncryptedData.Length];
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
memoryStream.Close();
cryptoStream.Close();
//Converting to string
DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
}
catch
{
DecryptedData = TextToBeDecrypted;
}
return DecryptedData;
}
public static string Encrypt(string TextToBeEncrypted, string SaltContaint)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
//string SaltContaint = "CSC";
byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);
byte[] Salt = Encoding.ASCII.GetBytes(SaltContaint.Length.ToString());
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(SaltContaint, Salt);
//Creates a symmetric encryptor object.
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
//Defines a stream that links data streams to cryptographic transformations
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
cryptoStream.Write(PlainText, 0, PlainText.Length);
//Writes the final state and clears the buffer
cryptoStream.FlushFinalBlock();
byte[] CipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string EncryptedData = Convert.ToBase64String(CipherBytes);
return EncryptedData;
}
}
Method (2) :
HTML :
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox runat="server" ID="txtEncrypt"></asp:TextBox>
</td>
<td>
<asp:TextBox runat="server" ID="txtDecrypt"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnEncrypt" runat="server" OnClick="btnEncrypt_Click" Text="Encrypt" />
<asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" OnClick="btnDecrypt_Click" />
</td>
</tr>
</table>
Code-Behind :
EncryptionManager manager = new EncryptionManager();
protected void btnEncrypt_Click(object sender, EventArgs e)
{
txtEncrypt.Text = manager.Encrypt(txtData.Text);
}
protected void btnDecrypt_Click(object sender, EventArgs e)
{
txtDecrypt.Text = manager.Decrypt(txtEncrypt.Text);
}
Encryption-Decryption Class :
public class EncryptionManager
{
private byte[] Key = { 89, 83, 45, 236, 140, 228, 180, 79, 209, 164, 231, 131, 28, 7, 110, 73, 140, 235, 118, 52, 225, 46, 202, 118 };
private byte[] IV = { 161, 200, 187, 207, 22, 92, 119, 227 };
public string Encrypt(string inputString)
{
//Step #4
byte[] buffer = Encoding.ASCII.GetBytes(inputString);
TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider()
{
Key = Key,
IV = IV
};
//Step #4,5,6
ICryptoTransform ITransform = tripleDes.CreateEncryptor();
return Convert.ToBase64String(ITransform.TransformFinalBlock(buffer, 0, buffer.Length));
}
public string Decrypt(string inputString)
{
byte[] buffer = Convert.FromBase64String(inputString);
TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider()
{
Key = Key,
IV = IV
};
ICryptoTransform ITransform = tripleDes.CreateDecryptor();
return Encoding.ASCII.GetString(ITransform.TransformFinalBlock(buffer, 0, buffer.Length));
}
}