使用非对称RSA加密

本文提供了一个使用非对称RSA加密方法的示例,包括生成公钥和私钥、加密及解密过程。通过C#代码演示了如何实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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;">&nbsp;<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值