webform1.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work079.WebForm1" ValidateRequest="false" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>RSA加密与解密的示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0">
<tr>
<td colspan="2" align="center">
RSA加密与解密的示例
</td>
</tr>
<tr>
<td>
明文:<br />
<asp:TextBox ID="txtSource" runat="server" Columns="50" Rows="20" TextMode="MultiLine"></asp:TextBox>
</td>
<td>
密文:<br />
<asp:TextBox ID="txtResult" runat="server" Columns="50" Rows="20" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
公钥XML<br />
<asp:TextBox ID="txtPublicKey" runat="server" Columns="50" Rows="20" TextMode="MultiLine"></asp:TextBox>
</td>
<td>
私钥XML<br />
<asp:TextBox ID="txtPrivateKey" runat="server" Columns="50" Rows="20" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="加密" />
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="解密" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html> webform1.aspx.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace work079
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 加密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string[] key = RSAProvider.GenerateKeys();
txtPublicKey.Text = key[0];
txtPrivateKey.Text = key[1];
string text = txtSource.Text;
txtResult.Text = RSAProvider.EncryptString(text, txtPublicKey.Text);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
string text = txtResult.Text;
txtSource.Text = RSAProvider.DecryptString(text, txtPrivateKey.Text);
}
}
} rsaprovider.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace work079
{
/// <summary>
/// RSA加密解密的类
/// </summary>
public class RSAProvider
{
private RSAProvider()
{
}
//指定密钥的初始化大小(单位时bit),增量是8
//private static int keySize = 5 * 1024;
/// <summary>
/// 使用指定的含有公钥的XML字符串加密
/// </summary>
/// <param name="enString">要加密的字符串</param>
/// <param name="publicKey">含有公钥的XML字符串</param>
/// <returns></returns>
public static string EncryptString(string enString, string publicKey)
{
if (string.IsNullOrEmpty(enString) || string.IsNullOrEmpty(publicKey))
{
throw new ArgumentNullException("enString和publicKey", "不能为空,或者长度为0");
}
else
{
byte[] enBytes = System.Text.Encoding.UTF8.GetBytes(enString);
enBytes = EncryptBytes(enBytes,publicKey);
return Convert.ToBase64String(enBytes);
}
}
/// <summary>
/// 使用指定的含有公钥的XML字符串加密字节数组
/// </summary>
/// <param name="sourceBytes">要加密的字节数组</param>
/// <param name="publicKey">含有公钥的XML字符串</param>
/// <returns></returns>
private static byte[] EncryptBytes(byte[] sourceBytes, string publicKey)
{
System.Security.Cryptography.RSACryptoServiceProvider provider = new System.Security.Cryptography.RSACryptoServiceProvider();
//System.Security.Cryptography.RSACryptoServiceProvider provider = new System.Security.Cryptography.RSACryptoServiceProvider(keySize);
provider.FromXmlString(publicKey);
//解密,如果在windows xp及更高版本上,第二参数可为true,否则false
byte[] resultBytes = provider.Encrypt(sourceBytes, true);
provider.Clear();
return resultBytes;
}
/// <summary>
/// 使用指定的私钥解密字符串
/// </summary>
/// <param name="enString">要解密的字符串</param>
/// <param name="privateKey">包含公钥和私钥的XML字符串</param>
/// <returns></returns>
public static string DecryptString(string enString, string privateKey)
{
if (string.IsNullOrEmpty(enString) || string.IsNullOrEmpty(privateKey))
{
throw new ArgumentNullException("enString和privateKey", "不能为空,或者长度为0");
}
else
{
byte[] enBytes = Convert.FromBase64String(enString);
byte[] resultBytes = DecryptBytes(enBytes, privateKey);
return System.Text.Encoding.UTF8.GetString(resultBytes);
}
}
/// <summary>
/// 使用私钥解密字节数组
/// </summary>
/// <param name="enBytes"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
private static byte[] DecryptBytes(byte[] enBytes, string privateKey)
{
System.Security.Cryptography.RSACryptoServiceProvider provider = new System.Security.Cryptography.RSACryptoServiceProvider();
//System.Security.Cryptography.RSACryptoServiceProvider provider = new System.Security.Cryptography.RSACryptoServiceProvider(keySize);
provider.FromXmlString(privateKey);
byte[] resultBytes = provider.Decrypt(enBytes, true);
provider.Clear();
return resultBytes;
}
/// <summary>
/// 生成仅包含公钥与私钥的XML字符串
/// </summary>
/// <returns></returns>
public static string[] GenerateKeys()
{
string[] keys = new string[2];
System.Security.Cryptography.RSACryptoServiceProvider provider = new System.Security.Cryptography.RSACryptoServiceProvider();
//System.Security.Cryptography.RSACryptoServiceProvider provider = new System.Security.Cryptography.RSACryptoServiceProvider(keySize);
//公钥
keys[0] = provider.ToXmlString(false);
//公钥和私钥
keys[1] = provider.ToXmlString(true);
return keys;
}
}
}
|