加密和解密 [英] encryption & decryption
本文介绍了加密和解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Input : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="encrypt"
onclick="Button1_Click" />
<br /><br />
Output : <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="decrypt"
onclick="Button2_Click" />
<br /><br />
Final Result :
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
c#
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = Encrypt(TextBox1.Text.Trim());
}
public static string Encrypt(string strPassword)
{
FormsAuthenticationTicket Ftk = new FormsAuthenticationTicket(strPassword, false, 1);
return FormsAuthentication.Encrypt(Ftk);
}
public static string Decrypt(string encryptedString)
{
FormsAuthenticationTicket Ftk = FormsAuthentication.Decrypt(encryptedString);
return Ftk.Name;
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = Decrypt(TextBox2.Text.Trim());
}
}
推荐答案
请下次如果我们看到一半的问题,那么它将被视为垃圾邮件,并且不会给出任何答复,是的,您在写什么里面有完整的螃蟹,请使用下面的代码
为此创建一个单独的类
Please next time if we see half question then it will be consider spam and no response to it will be given , and yes what u writing in there is complete crab , user the below code
create a seprate class for it
using System.Collections.Generic;
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
private static Byte[] sharedkey = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
private static Byte[] sharedvector = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
public static string Encrypt(string val)
{
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
Byte[] toEncrypt = Encoding.UTF8.GetBytes(val);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, tdes.CreateEncryptor(sharedkey, sharedvector), CryptoStreamMode.Write);
cs.Write(toEncrypt, 0, toEncrypt.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
public static string Decrypt(string val)
{
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
Byte[] toDecrypt = Convert.FromBase64String(val);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, tdes.CreateDecryptor(sharedkey, sharedvector), CryptoStreamMode.Write);
cs.Write(toDecrypt, 0, toDecrypt.Length);
cs.FlushFinalBlock();
return Encoding.UTF8.GetString(ms.ToArray());
}
这篇关于加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文