Here In code base64 Encryption and Decryption in done.
protected void btnencrypt_Click(object sender,System.EventArgs e)
{
string data = TextBox1.Text;
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
string encodedData = Convert.ToBase64String(encData_byte);
Lblencrypt.Text = encodedData.ToString();
}
.vb
Simple example I am showing you to make understand.
Example.
In .aspx page
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnencrypt" runat="server" Text="Encrypt" /> <asp:Button ID="btndecrypt" runat="server" Text="Decrypt" />
<br />
<asp:Label ID="Lblencrypt" runat="server" ></asp:Label>
<br />
<asp:Label ID="Lbldecrypt" runat="server" ></asp:Label>
For Encryption.
.vb
Protected Sub btnencrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnencrypt.Click
Dim data As String = TextBox1.Text
Dim encData_byte As Byte() = New Byte(data.Length - 1) {}
encData_byte = System.Text.Encoding.UTF8.GetBytes(data)
Dim encodedData As String = Convert.ToBase64String(encData_byte)
Lblencrypt.Text = encodedData.ToString()
End Sub
.C#
protected void btnencrypt_Click(object sender,System.EventArgs e)
{
string data = TextBox1.Text;
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
string encodedData = Convert.ToBase64String(encData_byte);
Lblencrypt.Text = encodedData.ToString();
}
For Decryption.
Protected Sub btndecrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndecrypt.Click
Dim encoder As New System.Text.UTF8Encoding()
Dim utf8Decode As System.Text.Decoder = encoder.GetDecoder()
Dim todecode_byte As Byte() = Convert.FromBase64String(Lblencrypt.Text)
Dim charCount As Integer = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length)
Dim decoded_char As Char() = New Char(charCount - 1) {}
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0)
Dim result As String = New [String](decoded_char)
Lbldecrypt.Text = result.ToString()
End Sub
.C#
protected void btndecrypt_Click(object sender,System.EventArgs e)
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(Lblencrypt.Text);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
Lbldecrypt.Text = result.ToString();
}
.C#
protected void btndecrypt_Click(object sender,System.EventArgs e)
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(Lblencrypt.Text);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
Lbldecrypt.Text = result.ToString();
}
Result
Thanks anish malik bhai ,, you are too good Please tell me ur id i wanna some more efficent help from you
ReplyDelete