asp.net代码练习 work079 RSA加密与解密的示例

258 篇文章 2 订阅

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="加密" OnClick="Button1_Click" />
                </td>
                <td>
                    <asp:Button ID="Button2" runat="server" Text="解密" OnClick="Button2_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using 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.cs

using 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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值