C#实现简单的RSA非对称加密算法示例
发布时间 - 2026-01-11 02:59:42 点击率:次本文实例讲述了C#实现简单的RSA非对称加密算法。分享给大家供大家参考,具体如下:

界面控件
namespace RSA算法
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.StrBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.PubKeyBox = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.PrvKeyBox = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.EncrypeBox = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.Str2Box = new System.Windows.Forms.TextBox();
this.CrypeBtn = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// StrBox
//
this.StrBox.Location = new System.Drawing.Point(115, 12);
this.StrBox.Name = "StrBox";
this.StrBox.Size = new System.Drawing.Size(258, 21);
this.StrBox.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(11, 18);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 12);
this.label1.TabIndex = 1;
this.label1.Text = "加密前的明文";
//
// PubKeyBox
//
this.PubKeyBox.Location = new System.Drawing.Point(115, 78);
this.PubKeyBox.Multiline = true;
this.PubKeyBox.Name = "PubKeyBox";
this.PubKeyBox.Size = new System.Drawing.Size(258, 74);
this.PubKeyBox.TabIndex = 2;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(11, 87);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 3;
this.label2.Text = "公钥";
//
// PrvKeyBox
//
this.PrvKeyBox.Location = new System.Drawing.Point(115, 158);
this.PrvKeyBox.Multiline = true;
this.PrvKeyBox.Name = "PrvKeyBox";
this.PrvKeyBox.Size = new System.Drawing.Size(258, 128);
this.PrvKeyBox.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(13, 167);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(101, 12);
this.label3.TabIndex = 5;
this.label3.Text = "密钥(包含私钥)";
//
// EncrypeBox
//
this.EncrypeBox.Location = new System.Drawing.Point(115, 292);
this.EncrypeBox.Name = "EncrypeBox";
this.EncrypeBox.Size = new System.Drawing.Size(258, 21);
this.EncrypeBox.TabIndex = 6;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(14, 299);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(29, 12);
this.label4.TabIndex = 7;
this.label4.Text = "密文";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(14, 329);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(77, 12);
this.label5.TabIndex = 8;
this.label5.Text = "解密后的明文";
//
// Str2Box
//
this.Str2Box.Location = new System.Drawing.Point(115, 320);
this.Str2Box.Name = "Str2Box";
this.Str2Box.Size = new System.Drawing.Size(258, 21);
this.Str2Box.TabIndex = 9;
//
// CrypeBtn
//
this.CrypeBtn.Location = new System.Drawing.Point(117, 43);
this.CrypeBtn.Name = "CrypeBtn";
this.CrypeBtn.Size = new System.Drawing.Size(104, 23);
this.CrypeBtn.TabIndex = 10;
this.CrypeBtn.Text = "执行加密解密";
this.CrypeBtn.UseVisualStyleBackColor = true;
this.CrypeBtn.Click += new System.EventHandler(this.CrypeBtn_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(385, 353);
this.Controls.Add(this.CrypeBtn);
this.Controls.Add(this.Str2Box);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.EncrypeBox);
this.Controls.Add(this.label3);
this.Controls.Add(this.PrvKeyBox);
this.Controls.Add(this.label2);
this.Controls.Add(this.PubKeyBox);
this.Controls.Add(this.label1);
this.Controls.Add(this.StrBox);
this.Name = "Form1";
this.Text = "RSA非对称加密解密";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox StrBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox PubKeyBox;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox PrvKeyBox;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox EncrypeBox;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox Str2Box;
private System.Windows.Forms.Button CrypeBtn;
}
}
RSA代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace RSA算法
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CrypeBtn_Click(object sender, EventArgs e)
{
//创建RSA加密算法服务提供者
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//显示公钥和私钥
PubKeyBox.Text = RSA.ToXmlString(false);
PrvKeyBox.Text = RSA.ToXmlString(true);
//执行加密
byte[] EncrypeBytes = RSA.Encrypt(Encoding.UTF8.GetBytes(StrBox.Text), true);
EncrypeBox.Text = Encoding.UTF8.GetString(EncrypeBytes);
//执行解密
byte[] DecrypeBytes = RSA.Decrypt(EncrypeBytes, true);
Str2Box.Text = Encoding.UTF8.GetString(DecrypeBytes);
}
}
}
运行效果:
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含AES、DES、RC4等):
http://tools./password/txt_encode
MD5在线加密工具:
http://tools./password/CreateMD5Password
在线散列/哈希算法加密工具:
http://tools./password/hash_encrypt
在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools./password/hash_md5_sha
在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools./password/sha_encode
更多关于C#相关内容还可查看本站专题:《C#加密与解密算法与技巧总结》、《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。
# C#
# RSA
# 非对称
# 加密
# 算法
# 详解c#与js的rsa加密互通
# c# rsa加密解密详解
# C#中RSA加密与解密的实例详解
# C#使用RSA加密解密文件
# 同时兼容JS和C#的RSA加密解密算法详解(对web提交的数据加密传输)
# jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
# C#自定义RSA加密解密及RSA签名和验证类实例
# 基于私钥加密公钥解密的RSA算法C#实现方法
# c# rsa注册实现加密文字
# c# 实现RSA非对称加密算法
# 加密工具
# 加密解密
# 程序设计
# 操作技巧
# 公钥
# 相关内容
# 还可以
# 所需
# 感兴趣
# 数据结构
# 给大家
# 还可
# 更多关于
# 则为
# 所述
# 编辑器
# 面向对象
# 讲述了
# CrypeBtn
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel Blade组件怎么用_Laravel可复用视图组件的创建与使用
如何用花生壳三步快速搭建专属网站?
Python数据仓库与ETL构建实战_Airflow调度流程详解
java中使用zxing批量生成二维码立牌
如何在阿里云域名上完成建站全流程?
Laravel如何记录自定义日志?(Log频道配置)
如何在万网开始建站?分步指南解析
高性价比服务器租赁——企业级配置与24小时运维服务
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
Laravel中DTO是什么概念_在Laravel项目中使用数据传输对象(DTO)
如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南
原生JS实现图片轮播切换效果
如何在宝塔面板中创建新站点?
Laravel用户密码怎么加密_Laravel Hash门面使用教程
高端云建站费用究竟需要多少预算?
Laravel观察者模式如何使用_Laravel Model Observer配置
如何彻底删除建站之星生成的Banner?
郑州企业网站制作公司,郑州招聘网站有哪些?
如何在自有机房高效搭建专业网站?
公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?
如何选择可靠的免备案建站服务器?
Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】
Laravel怎么使用Session存储数据_Laravel会话管理与自定义驱动配置【详解】
Thinkphp 中 distinct 的用法解析
免费网站制作appp,免费制作app哪个平台好?
高端建站三要素:定制模板、企业官网与响应式设计优化
香港服务器网站推广:SEO优化与外贸独立站搭建策略
如何在阿里云服务器自主搭建网站?
大学网站设计制作软件有哪些,如何将网站制作成自己app?
phpredis提高消息队列的实时性方法(推荐)
Laravel如何从数据库删除数据_Laravel destroy和delete方法区别
西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?
实例解析Array和String方法
怎么用AI帮你为初创公司进行市场定位分析?
Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】
Laravel怎么判断请求类型_Laravel Request isMethod用法
Linux网络带宽限制_tc配置实践解析【教程】
Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势
原生JS获取元素集合的子元素宽度实例
jQuery中的100个技巧汇总
Laravel队列任务超时怎么办_Laravel Queue Timeout设置详解
Laravel如何使用API Resources格式化JSON响应_Laravel数据资源封装与格式化输出
Laravel怎么配置.env环境变量_Laravel生产环境敏感数据保护与读取【方法】
php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】
Laravel中间件如何使用_Laravel自定义中间件实现权限控制
如何在阿里云虚拟服务器快速搭建网站?
如何快速查询域名建站关键信息?
如何确保西部建站助手FTP传输的安全性?
php在windows下怎么调试_phpwindows环境调试操作说明【操作】
利用 Google AI 进行 YouTube 视频 SEO 描述优化

