您的位置:首页 > 其它

使用非对称RSA加密

2007-12-22 13:59 363 查看
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

/// <summary>
/// 下面介绍如何使用非对称RSA加密的方法:
/// </summary>
public partial class test : System.Web.UI.Page
{

//加密:
public string EncryptData(string data, string readpublickey)
{
RSACryptoServiceProvider crypt=new RSACryptoServiceProvider();
UTF8Encoding enc=new UTF8Encoding();
byte[] bytes=enc.GetBytes(data);
crypt.FromXmlString(readpublickey);
bytes=crypt.Encrypt(bytes, false);
string encryttext=Convert.ToBase64String(bytes);
return encryttext;
}

//解密:
public string DecryptData(string data, string readprivatekey)
{
RSACryptoServiceProvider crypt=new RSACryptoServiceProvider();
UTF8Encoding enc=new UTF8Encoding();
byte[] bytes=Convert.FromBase64String(data);
crypt.FromXmlString(readprivatekey);
byte[] decryptbyte=crypt.Decrypt(bytes, false);
string decrypttext=enc.GetString(decryptbyte);

return decrypttext;
}

//制造公匙和私匙的方法如下
public void CreateKey()
{
RSACryptoServiceProvider crypt=new RSACryptoServiceProvider();
TxtPublicKey.Text=crypt.ToXmlString(false); //公匙
TxtPrivateKey.Text=crypt.ToXmlString(true); //私匙

msg.Text="成功保存公匙和密匙!";
}

protected void BtnCreate_Click(object sender, EventArgs e)
{
CreateKey();
}
protected void BtnEncrypt_Click(object sender, EventArgs e)
{
MyResult.Text=EncryptData(MyData.Text, TxtPublicKey.Text);
}
protected void BtnDecrypt_Click(object sender, EventArgs e)
{
MyData.Text=DecryptData(MyResult.Text, TxtPrivateKey.Text);
}
}

<%@ Page AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" Language="C#"
ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>下面介绍如何使用非对称RSA加密的方法:</title></style>
</head>
<body>
<form id="myform1" runat="server">
<div id="content">
<div class="rtblue">
<div style="clear: both;">
<div>
<div>
<div class="grewbox">
下面介绍如何使用非对称RSA加密的方法:
<br />
<asp:Label ID="msg" runat="server" Text="" Width="70px"></asp:Label><br />
<table>
<tr>
<td style="width: 69px">
共钥:</td>
<td style="width: 100px">
<asp:TextBox ID="TxtPublicKey" runat="server" Height="43px" TextMode="MultiLine"
Width="440px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 69px">
私钥:</td>
<td style="width: 100px">
<asp:TextBox ID="TxtPrivateKey" runat="server" Height="45px" TextMode="MultiLine"
Width="439px"></asp:TextBox></td>
</tr>
</table>
</div>
<div class="newl">
</div>
</div>
</div>
</div>
</div>
</div>
<asp:Button ID="BtnCreate" runat="server" Text="创建密码" OnClick="BtnCreate_Click" />
<asp:Button ID="BtnEncrypt" runat="server" Text="加密" Width="44px" OnClick="BtnEncrypt_Click" />
<asp:Button ID="BtnDecrypt" runat="server" Text="解密" Width="49px" OnClick="BtnDecrypt_Click" />
<br />
<table border="0" cellpadding="3" cellspacing="6">
<tr>
<td align="right">
明文数据</td>
<td>
<span style="height: 58px;"> <asp:TextBox ID="MyData" runat="server"></asp:TextBox></span></td>
</tr>
<tr>
<td align="right" valign="top">
密文数据</td>
<td valign="top">
<asp:TextBox ID="MyResult" runat="server" Height="105px" TextMode="MultiLine" Width="414px"></asp:TextBox>
</td>
</tr>
</table>
<br />
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: