ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Encrypt and Decrypt Data in ASP.Net using C# and VB.Net<p>Hi&nbsp;Saurav982,</p> <p>Check the below sample code. The provided key is working.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">Key: &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtKey&#34; /&gt; &lt;hr /&gt; &lt;u&gt;Encrypt&lt;/u&gt;&lt;br /&gt; &lt;br /&gt; Original Text: &lt;asp:TextBox ID=&#34;txtOriginalText&#34; runat=&#34;server&#34; Text=&#34;Test&#34; /&gt; &lt;br /&gt; &lt;br /&gt; Encrypted Text: &lt;asp:Label ID=&#34;lblEncryptedText&#34; runat=&#34;server&#34; Text=&#34;&#34; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnEncrypt&#34; OnClick=&#34;Encrypt&#34; Text=&#34;Encrypt&#34; runat=&#34;server&#34; /&gt; &lt;hr /&gt; &lt;u&gt;Decrypt&lt;/u&gt; &lt;br /&gt; &lt;br /&gt; Encrypted Text: &lt;asp:TextBox ID=&#34;txtEncryptedText&#34; runat=&#34;server&#34; Text=&#34;&#34; Width=&#34;200px&#34; /&gt; &lt;br /&gt; &lt;br /&gt; Decrypted Text: &lt;asp:Label ID=&#34;lblDecryptedText&#34; runat=&#34;server&#34; Text=&#34;&#34; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnDecrypt&#34; OnClick=&#34;Decrypt&#34; Text=&#34;Decrypt&#34; runat=&#34;server&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Text; using System.Security.Cryptography;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Text Imports System.Security.Cryptography</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Encrypt(object sender, EventArgs e) { lblEncryptedText.Text = this.Encrypt(txtOriginalText.Text.Trim()); txtEncryptedText.Text = this.Encrypt(txtOriginalText.Text.Trim()); } private string Encrypt(string clearText) { string EncryptionKey = txtKey.Text.Trim(); byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } clearText = Convert.ToBase64String(ms.ToArray()); } } return clearText; } protected void Decrypt(object sender, EventArgs e) { lblDecryptedText.Text = this.Decrypt(txtEncryptedText.Text.Trim()); } private string Decrypt(string cipherText) { string EncryptionKey = txtKey.Text.Trim(); byte[] cipherBytes = Convert.FromBase64String(cipherText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipherText = Encoding.Unicode.GetString(ms.ToArray()); } } return cipherText; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Encrypt(sender As Object, e As EventArgs) lblEncryptedText.Text = Me.Encrypt(txtOriginalText.Text.Trim()) txtEncryptedText.Text = Me.Encrypt(txtOriginalText.Text.Trim()) End Sub Private Function Encrypt(clearText As String) As String Dim EncryptionKey As String = txtKey.Text.Trim() Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText) Using encryptor As Aes = Aes.Create() Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&amp;H49, &amp;H76, &amp;H61, &amp;H6E, &amp;H20, &amp;H4D, _ &amp;H65, &amp;H64, &amp;H76, &amp;H65, &amp;H64, &amp;H65, _ &amp;H76}) encryptor.Key = pdb.GetBytes(32) encryptor.IV = pdb.GetBytes(16) Using ms As New MemoryStream() Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write) cs.Write(clearBytes, 0, clearBytes.Length) cs.Close() End Using clearText = Convert.ToBase64String(ms.ToArray()) End Using End Using Return clearText End Function Protected Sub Decrypt(sender As Object, e As EventArgs) lblDecryptedText.Text = Me.Decrypt(txtEncryptedText.Text.Trim()) End Sub Private Function Decrypt(cipherText As String) As String Dim EncryptionKey As String = txtKey.Text.Trim() Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText) Using encryptor As Aes = Aes.Create() Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&amp;H49, &amp;H76, &amp;H61, &amp;H6E, &amp;H20, &amp;H4D, _ &amp;H65, &amp;H64, &amp;H76, &amp;H65, &amp;H64, &amp;H65, _ &amp;H76}) encryptor.Key = pdb.GetBytes(32) encryptor.IV = pdb.GetBytes(16) Using ms As New MemoryStream() Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write) cs.Write(cipherBytes, 0, cipherBytes.Length) cs.Close() End Using cipherText = Encoding.Unicode.GetString(ms.ToArray()) End Using End Using Return cipherText End Function</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/Hwv15Gp.gif" alt="" width="345" height="373" /></span></strong></p>https://www.aspforums.net:443/Threads/189868/Encrypt-and-Decrypt-Data-in-ASPNet-using-C-and-VBNet/https://www.aspforums.net:443/Threads/189868/Encrypt-and-Decrypt-Data-in-ASPNet-using-C-and-VBNet/Wed, 23 Jan 2019 04:11:18 GMT