<div>
<asp:textbox id="txtData"
runat="server"
/>
<br />
<br />
<asp:button Id="btnClick"
text="Click"
runat="server"
onclick="dd_Click"
/>
</div>
using System.IO;
using System.Text;
using System.Security.Cryptography;
protected void btnClick_Click(object sender, EventArgs
e)
{
string Data = HttpUtility.UrlEncode(Encrypt_Data(txtData.Text));
Response.Redirect(string.Format("~/test2.aspx?Data={0}", Data));
}
private string
Encrypt_Data(string Encry_Data)
{
string Encryp_Key = "MAKV2SPBNI99212";
byte[] Clear_Data = Encoding.Unicode.GetBytes(Encry_Data);
using (Aes Enc_Key
= Aes.Create())
{
Rfc2898DeriveBytes pdb = new
Rfc2898DeriveBytes(Encryp_Key, new byte[] { 0x49,
0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
Enc_Key.Key = pdb.GetBytes(32);
Enc_Key.IV = pdb.GetBytes(16);
using (MemoryStream
MemoryStream1 = new MemoryStream())
{
using (CryptoStream
cs = new CryptoStream(MemoryStream1,
Enc_Key.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(Clear_Data, 0, Clear_Data.Length);
cs.Close();
}
Encry_Data = Convert.ToBase64String(MemoryStream1.ToArray());
}
}
return Encry_Data;
}
<div>
<asp:label text="Encryption
Querystring data => " runat="server" /><asp:label id="lblData"
runat="server"
/>
</div>
protected void Page_Load(object sender, EventArgs
e)
{
if (!this.IsPostBack)
{
lblData.Text = Request.QueryString["Data"];
string Data = Decrypt_Data(HttpUtility.UrlDecode(Request.QueryString["Data"]));
Response.Write(" Your Quyery String
Data => " + Data);
}
}
private string
Decrypt_Data(string Data_Encry)
{
string Encryp_Key = "MAKV2SPBNI99212";
Data_Encry = Data_Encry.Replace("
", "+");
byte[] Data_Encry_Bytes = Convert.FromBase64String(Data_Encry);
using (Aes Enc_Key
= Aes.Create())
{
Rfc2898DeriveBytes enc = new
Rfc2898DeriveBytes(Encryp_Key, new byte[] { 0x49,
0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
Enc_Key.Key = enc.GetBytes(32);
Enc_Key.IV = enc.GetBytes(16);
using (MemoryStream
MemoryStream1 = new MemoryStream())
{
using (CryptoStream
cs = new CryptoStream(MemoryStream1,
Enc_Key.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(Data_Encry_Bytes, 0, Data_Encry_Bytes.Length);
cs.Close();
}
Data_Encry = Encoding.Unicode.GetString(MemoryStream1.ToArray());
}
}
return Data_Encry;
}
No comments:
Post a Comment