<div>
<asp:Button ID="btnBuildKey" runat="server" Text="生成密钥"
onclick="btnBuildKey_Click" />
<br />
<br />
<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnEnCrypt" runat="server" Text="加密"
onclick="btnEnCrypt_Click" />
<br />
<asp:Label ID="lblEnCryptData" runat="server" Text="加密后的数据" BackColor="AliceBlue"></asp:Label>
<br />
<asp:Button ID="btnDeCrypt" runat="server" Text="解密"
onclick="btnDeCrypt_Click" />
<br />
<asp:Label ID="lblData" runat="server" Text="原始数据"></asp:Label>
</div>
<asp:Button ID="btnBuildKey" runat="server" Text="生成密钥"
onclick="btnBuildKey_Click" />
<br />
<br />
<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnEnCrypt" runat="server" Text="加密"
onclick="btnEnCrypt_Click" />
<br />
<asp:Label ID="lblEnCryptData" runat="server" Text="加密后的数据" BackColor="AliceBlue"></asp:Label>
<br />
<asp:Button ID="btnDeCrypt" runat="server" Text="解密"
onclick="btnDeCrypt_Click" />
<br />
<asp:Label ID="lblData" runat="server" Text="原始数据"></asp:Label>
</div>
protected void btnEnCrypt_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Server.MapPath("rsaPublicKey.xml"));
rsa.FromXmlString(doc.OuterXml);
byte[] PlainTextBArray = (new UnicodeEncoding()).GetBytes(txtData.Text.Trim());
byte[] CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
string strResult = Convert.ToBase64String(CypherTextBArray);
lblEnCryptData.Text = strResult;
}
protected void btnDeCrypt_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Server.MapPath("rsaPrivateKey.xml"));
rsa.FromXmlString(doc.OuterXml);
byte[] PlainTextBArray = Convert.FromBase64String(lblEnCryptData.Text);
byte[] DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
string strResult = (new UnicodeEncoding()).GetString(DypherTextBArray);
lblData.Text = strResult;
}
protected void btnBuildKey_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string strPrivateKey = rsa.ToXmlString(false);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(strPrivateKey);
doc.Save(Server.MapPath("rsaPublicKey.xml"));
string strPublicKey = rsa.ToXmlString(true);
doc = new System.Xml.XmlDocument();
doc.LoadXml(strPublicKey);
doc.Save(Server.MapPath("rsaPrivateKey.xml"));
}
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Server.MapPath("rsaPublicKey.xml"));
rsa.FromXmlString(doc.OuterXml);
byte[] PlainTextBArray = (new UnicodeEncoding()).GetBytes(txtData.Text.Trim());
byte[] CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
string strResult = Convert.ToBase64String(CypherTextBArray);
lblEnCryptData.Text = strResult;
}
protected void btnDeCrypt_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Server.MapPath("rsaPrivateKey.xml"));
rsa.FromXmlString(doc.OuterXml);
byte[] PlainTextBArray = Convert.FromBase64String(lblEnCryptData.Text);
byte[] DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
string strResult = (new UnicodeEncoding()).GetString(DypherTextBArray);
lblData.Text = strResult;
}
protected void btnBuildKey_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string strPrivateKey = rsa.ToXmlString(false);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(strPrivateKey);
doc.Save(Server.MapPath("rsaPublicKey.xml"));
string strPublicKey = rsa.ToXmlString(true);
doc = new System.Xml.XmlDocument();
doc.LoadXml(strPublicKey);
doc.Save(Server.MapPath("rsaPrivateKey.xml"));
}