Hi SajidHussa,
Refer below sample.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
string text = "Welcome to ASPForums.";
string encryptedString = EncryptString(text);
string decryptedString = DecryptString(encryptedString);
Response.Write(string.Format("Encrypted Value : {0}{1}Decrypted Value : {2}", encryptedString, Environment.NewLine, decryptedString));
}
public string EncryptString(string text)
{
string encrypted = "";
byte[] bytes;
try
{
bytes = new byte[text.Length];
bytes = System.Text.Encoding.UTF8.GetBytes(text);
encrypted = Convert.ToBase64String(bytes);
}
catch (Exception ex)
{
encrypted = "";
}
return encrypted;
}
public string DecryptString(string encrString)
{
byte[] bytes;
string decrypted;
try
{
bytes = Convert.FromBase64String(encrString);
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
}
catch (FormatException fe)
{
decrypted = "";
}
return decrypted;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim text As String = "Welcome to ASPForums."
Dim encryptedString As String = EncryptString(text)
Dim decryptedString As String = DecryptString(encryptedString)
Response.Write(String.Format("Encrypted Value : {0}{1}Decrypted Value : {2}", encryptedString, Environment.NewLine, decryptedString))
End Sub
Public Function EncryptString(ByVal text As String) As String
Dim encrypted As String = ""
Dim bytes As Byte()
Try
bytes = New Byte(text.Length - 1) {}
bytes = System.Text.Encoding.UTF8.GetBytes(text)
encrypted = Convert.ToBase64String(bytes)
Catch ex As Exception
encrypted = ""
End Try
Return encrypted
End Function
Public Function DecryptString(ByVal encrString As String) As String
Dim bytes As Byte()
Dim decrypted As String
Try
bytes = Convert.FromBase64String(encrString)
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Catch fe As FormatException
decrypted = ""
End Try
Return decrypted
End Function
Screenshot
![](https://i.imgur.com/N9iC1FK.jpg)