当前位置: 首页 > 编程日记 > 正文

C#非对称加密程序

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace 非对称加密
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.SaveFileDialog save;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.RichTextBox richtext2;
private System.Windows.Forms.OpenFileDialog open;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.RichTextBox richtext3;
private System.Windows.Forms.RichTextBox richtext;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
/// <summary>
/// 必需的设计器变量。
private static RSACryptoServiceProvider crypt;
private static string privatekey;
private static string publickey;
private static byte [] bytes;
private static string publicinfo;
private static string privateinfo;
private static string readpublickey;
private static string readprivatekey;
// ///private static byte [] onebytes ;


/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.button6 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.richtext = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.button8 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.richtext3 = new System.Windows.Forms.RichTextBox();
this.label2 = new System.Windows.Forms.Label();
this.richtext2 = new System.Windows.Forms.RichTextBox();
this.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.save = new System.Windows.Forms.SaveFileDialog();
this.open = new System.Windows.Forms.OpenFileDialog();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(32, 64);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(416, 280);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.button6);
this.tabPage1.Controls.Add(this.button3);
this.tabPage1.Controls.Add(this.button2);
this.tabPage1.Controls.Add(this.richtext);
this.tabPage1.Controls.Add(this.button1);
this.tabPage1.Location = new System.Drawing.Point(4, 21);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(408, 255);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "得到钥匙";
//
// button6
//
this.button6.Location = new System.Drawing.Point(72, 168);
this.button6.Name = "button6";
this.button6.TabIndex = 4;
this.button6.Text = "写入文件";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(200, 16);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "保存私匙";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(128, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "保存公钥";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richtext
//
this.richtext.Location = new System.Drawing.Point(16, 56);
this.richtext.Name = "richtext";
this.richtext.Size = new System.Drawing.Size(256, 96);
this.richtext.TabIndex = 1;
this.richtext.Text = "richTextBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 23);
this.button1.TabIndex = 0;
this.button1.Text = "得到钥匙信息";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tabPage2
//
this.tabPage2.Controls.Add(this.button8);
this.tabPage2.Controls.Add(this.button7);
this.tabPage2.Controls.Add(this.label3);
this.tabPage2.Controls.Add(this.richtext3);
this.tabPage2.Controls.Add(this.label2);
this.tabPage2.Controls.Add(this.richtext2);
this.tabPage2.Controls.Add(this.button5);
this.tabPage2.Controls.Add(this.button4);
this.tabPage2.Controls.Add(this.textBox1);
this.tabPage2.Controls.Add(this.label1);
this.tabPage2.Location = new System.Drawing.Point(4, 21);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Size = new System.Drawing.Size(408, 255);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "加密解密文字";
//
// button8
//
this.button8.Location = new System.Drawing.Point(216, 72);
this.button8.Name = "button8";
this.button8.TabIndex = 9;
this.button8.Text = "读取私匙";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(56, 72);
this.button7.Name = "button7";
this.button7.TabIndex = 8;
this.button7.Text = "读取公匙";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(200, 120);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 16);
this.label3.TabIndex = 7;
this.label3.Text = "解密结果";
//
// richtext3
//
this.richtext3.Location = new System.Drawing.Point(200, 144);
this.richtext3.Name = "richtext3";
this.richtext3.Size = new System.Drawing.Size(120, 72);
this.richtext3.TabIndex = 6;
this.richtext3.Text = "richTextBox2";
//
// label2
//
this.label2.Location = new System.Drawing.Point(48, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(128, 16);
this.label2.TabIndex = 5;
this.label2.Text = "加密窗口:";
//
// richtext2
//
this.richtext2.Location = new System.Drawing.Point(40, 144);
this.richtext2.Name = "richtext2";
this.richtext2.Size = new System.Drawing.Size(120, 72);
this.richtext2.TabIndex = 4;
this.richtext2.Text = "richTextBox2";
//
// button5
//
this.button5.Location = new System.Drawing.Point(200, 96);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(112, 23);
this.button5.TabIndex = 3;
this.button5.Text = "使用私匙解密";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(48, 96);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(96, 23);
this.button4.TabIndex = 2;
this.button4.Text = "使用公匙加密";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 32);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 32);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// label1
//
this.label1.Location = new System.Drawing.Point(128, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 16);
this.label1.TabIndex = 0;
this.label1.Text = "请输入加密的文字";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 357);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//得到钥匙信息
private void button1_Click(object sender, System.EventArgs e)
{
crypt=new RSACryptoServiceProvider();
publickey=crypt.ToXmlString(false);
richtext.Text="导出秘匙的情况下:/n"+publickey+"/n";
privatekey=crypt.ToXmlString(true);
string info="仅仅导出公匙的情况下:/n"+privatekey+"/n";
richtext.AppendText(info);
crypt.Clear();

}
//保存公匙信息
private void button2_Click(object sender, System.EventArgs e)
{

save=new SaveFileDialog();
save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
save.ShowDialog();
publicinfo=save.FileName;

}
//保存密匙信息
private void button3_Click(object sender, System.EventArgs e)
{
save=new SaveFileDialog();
save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
save.ShowDialog();
privateinfo=save.FileName;

}
//把钥匙信息写入文件
private void button6_Click(object sender, System.EventArgs e)
{

StreamWriter one=new StreamWriter(publicinfo,true,UTF8Encoding.UTF8);
one.Write(publickey);
StreamWriter two=new StreamWriter(privateinfo,true,UTF8Encoding.UTF8);
two.Write(privatekey);
one.Flush();
two.Flush();
one.Close();
two.Close();
MessageBox.Show("成功保存公匙和密匙!");

}
//用公匙加密
private void button4_Click(object sender, System.EventArgs e)
{

crypt=new RSACryptoServiceProvider();
UTF8Encoding enc=new UTF8Encoding();
bytes=enc.GetBytes(textBox1.Text);
crypt.FromXmlString( readpublickey );
bytes = crypt.Encrypt( bytes,false );
string encryttext=enc.GetString(bytes);//encryptbyte);
richtext2.Text="加密结果:/n"+encryttext+"/n"+"加密结束!";
}

private void button5_Click(object sender, System.EventArgs e)
{

UTF8Encoding enc=new UTF8Encoding();
byte [] decryptbyte;
crypt.FromXmlString ( readprivatekey ) ;
decryptbyte = crypt.Decrypt( bytes,false );
string decrypttext=enc.GetString( decryptbyte );
richtext3.Text = "解密结果:/n" + decrypttext + "/n" + "解密结束!" ;

}
//从文件中读取公匙信息
private void button7_Click(object sender, System.EventArgs e)
{
StreamReader sr ;
open = new OpenFileDialog( );
open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
// open.ShowDialog();
if(open.ShowDialog()==DialogResult.OK)
{
sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);
}
else
{
MessageBox.Show("发生错误!");
return;
}
readpublickey = sr.ReadToEnd();
sr.Close();
}
//从文件中读取私匙信息
private void button8_Click(object sender, System.EventArgs e)
{
open = new OpenFileDialog( );
open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
open.ShowDialog();
StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);
readprivatekey = sr.ReadToEnd();
sr.Close();
}



}
}

相关文章:

Exchange Server2013 系列十:证书的配置

Exchange Server2013 系列十&#xff1a;证书的配置杜飞经过前面的配置&#xff0c;基本上可以进行简单的邮件通讯了&#xff0c;但是当用户通过OWA连接邮箱时会报下面的提示&#xff1a;其他一些服务&#xff0c;如 Outlook Anywhere 和 Exchange ActiveSync&#xff0c;也要求…

高级程序员到底高级在哪里?

身为一名技术人&#xff0c;你是否遇到过这些情况&#xff1f;工作效率低&#xff1a;别人1小时就能修复的bug&#xff0c;你需要3小时没有存在感&#xff1a;技术趋势看不透&#xff0c;和同事聊天完全插不上话技术提升慢&#xff1a;苦熬996&#xff0c;但升职加薪仍然遥遥无…

AlexNet 网络详解及Tensorflow实现源码

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 1. 图片数据处理2. 卷积神经网络 2.1. 卷积层2.2. 池化层2.3. 全链层3. AlexNet4. 用Tensorflow搭建完整的AlexNet5. 用AlexNet识别猫狗图片 5.1. 定义分类5.2. 训练网络5.3. 验证1. 图片数据处理 一…

.net反射详解(转)

摘自&#xff1a;http://www.cnblogs.com/knowledgesea/archive/2013/03/02/2935920.html 概述反射 通过反射可以提供类型信息&#xff0c;从而使得我们开发人员在运行时能够利用这些信息构造和使用对象。 反射机制允许程序在执行过程中动态地添加各种功能。 运行时类型标识 …

C# 多网卡 Server Listen

VC和BCB中做一个Server的监听程序,只需要指定端口,然后监听(Listen)就行了.在C#找不到这个函数了,慢慢看MSDN,怎么需要指定IP和Port才能监听,那么多网卡的机器应该怎么写程序呢?下面的程序可以解释怎么去做. TcpListener 类别会提供简易的方法&#xff0c;用以在封锁的同步模式…

赠书 | 一文了解预训练语言模型

来源 | 博文视点头图 | 下载于视觉中国近年来&#xff0c;在深度学习和大数据的支撑下&#xff0c;自然语言处理技术迅猛发展。而预训练语言模型把自然语言处理带入了一个新的阶段&#xff0c;也得到了工业界的广泛关注。通过大数据预训练加小数据微调&#xff0c;自然语言处理…

写了六个相同功能的函数之后,我学到了什么

本文讲的是写了六个相同功能的函数之后&#xff0c;我学到了什么&#xff0c;几周之前&#xff0c;一个社区在 Free Code Camp’s Forum 上发起了非官方的算法大赛。 这个题目看似很简单&#xff1a;返回小于数字 N 的所有 3 或者 5 的倍数的和&#xff0c;N 是函数的参数。 但…

libevent介绍

libevent是一款事件驱动的网络开发包 由于采用 c 语言开发 体积小巧&#xff0c;跨平台&#xff0c;速度极快。 通常我们在建立服务器的处理模型的时候,主要是下面集中模型;(1) a new Connection 进来&#xff0c;用 fork() 产生一个 Process 处理。 (2) a new Connecti…

蓝色起源载人火箭7月首飞,贝索斯即将实现儿时愿望

整理 | 寇雪芹出品 | AI 科技大本营&#xff08;ID:rgznai100&#xff09;头图 | 下载于ICphoto美国当地时间6月7日早&#xff0c;亚马逊创始人、世界首富贝索斯&#xff08;Jeff Bezos&#xff09;在社交媒体上发帖表示&#xff0c;自己将在7月20日乘坐蓝色起源&#xff08;Bl…

使用jquery.more.js来实现点击底部更多后, 底部加载出新的数据

<div class"bus-nav-bar ft12"><div class"navt bor-r-c pos-rel {if $int 0}fwbold{/if}"><a href"portal.php?modmerchant&actionvoucherlist&int0">全部订单</a><em class"pos-abs"></…

ios开发学习-手势交互(Gesture)效果源码分享

qianqianlianmengios开发学习-手势交互&#xff08;Gesture&#xff09;效果源码分享 All Around Pull View 介绍&#xff1a;实现视图四个方向&#xff08;上下左右&#xff09;都能够拖动更新&#xff08;pull to refresh&#xff09;。 编译测试&#xff0c;测试环境…

通过C#实现集合类纵览.NET Collections及相关技术

概述&#xff1a;在真正的对象化开发项目中&#xff0c;我们通常会将常用的业务实体抽象为特定的类&#xff0c;如Employee、Customer、Contact等&#xff0c;而多数的类之间会存在着相应的关联或依存关系&#xff0c;如Employee和Customer通过Contact而产生关联、Contact是依赖…

TIOBE 6 月榜单: Python 有望超越 C 语言成为第一名

整理 | 苏宓出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;头图 | 下载于ICphotoTIOBE 官方最新发布了 6 月的编程语言榜单&#xff0c;这个月榜单中又有怎样的发展趋势&#xff1f;Python 有望成为第一名在本月榜单中&#xff0c;位居第二名的 Python 与第一名 C…

使用dom4j解析XML例子

包括三个文件&#xff1a;studentInfo.xml(待解析的xml文件), Dom4jReadExmple.java(解析的主要类), TestDom4jReadExmple.java(测试解析的结果) 代码运行前需先导入dom4j架包。 studentInfo.xml文件&#xff08;该文件放在本项目目录下&#xff09;内容如下&#xff1a; <?…

mkdir、rmdir命令、head、tail命令

mkdir-p 递归创建目录11里面都是空目录rmdir删除空目录 -p 当子目录被删除后使它也成为空目录的话&#xff0c;则一并删除步骤&#xff1a;先删除11/22/33 发现22目录空了&#xff0c;因为33删了&#xff0c;于是再删22&#xff0c;空了再删11head默认是前10行 –n指定几行tai…

Linux Find 命令精通指南

作者&#xff1a;Sheryl Calish Linux find 命令是所有 Linux 命令中最有用的一个&#xff0c;同时也是最混乱的一个。它很难&#xff0c;因为它的语法与其他 Linux 命令的标准语法不同。但是&#xff0c;它很强大&#xff0c;因为它允许您按文件名、文件类型、用户甚至是时间戳…

【安全运维】 linux 系统账户,网络,简易安全加固方案(第一部分),经测试可行...

前言讲到linux系统账户的管理以及安全&#xff0c;就必须涉及 /etc/passwd /etc/shadow 这2个文件这里以截图中文字说明的方式&#xff0c;来分析这2个文件的内容&#xff0c;并且给出一些实用的安全加固方案注意&#xff0c;本文会持续更新&#xff0c;后续加入的内容都以直…

不用深度学习,怎么提取图像特征?

来源 | 小白学视觉头图 | 下载于ICphoto图像分类是数据科学中最热门的领域之一&#xff0c;在本文中&#xff0c;我们将分享一些将图像转换为特征向量的技术&#xff0c;可以在每个分类模型中使用。VATboxVATbox&#xff0c;作为n一个我们所暗示的&#xff0c;涉及增值税问题&a…

课程第五天内容《基础交换 五》

2019独角兽企业重金招聘Python工程师标准>>> 以太网/LAN(local area network )的相关概念&#xff1a; 问题&#xff1a; 信号传输距离有限&#xff1b; 解决方案&#xff1a; 中继器 - 放大电信号&#xff0c;延长信息的传输距离&#xff1…

C#综合揭秘——Entity Framework 并发处理详解

引言 在软件开发过程中&#xff0c;并发控制是确保及时纠正由并发操作导致的错误的一种机制。从 ADO.NET 到 LINQ to SQL 再到如今的 ADO.NET Entity Framework&#xff0c;.NET 都为并发控制提供好良好的支持方案。 并发处理方式一般分为乐观必并发与悲观必并发两种&#xff0…

@2021高考生,用 Python 分析专业“钱景”

来源 | 关于数据分析于可视化头图 | 下载于ICphoto2021年的高考在昨日拉开帷幕&#xff0c;十年的寒窗苦读&#xff0c;终于到了最后见分晓的时候了。在这么一场关键的考试当中&#xff0c;除了考试前努力奋斗&#xff0c;考场上认真答题&#xff0c;考后的志愿填报也是极其的重…

Linux下C语言的fgets与fputs

使用的是 CentOS gcc编译下面程序 显示warning: the gets function is dangerous and should not be used.问题出在程序中使用了 gets Linux 下gcc编译器不支持这个函数&#xff0c;解决办法是使用 fgets fgets()函数的基本用法为&#xff1a; fgets(char * s,int size,FILE * …

linux发行版的用户交互

1 cli&#xff0c;即command line interface 纯命令行的交互方式&#xff0c;该命令行界面是由shell提供的。 linux内核本身也自带了一个console&#xff0c;即linux console&#xff0c;它是基于frame buffer的。 cli的界面都是基于ncurses库开发的。 2 GUI&#xff0c;graphi…

C#中在应用程序和DLL使用消息

在C#中采用的是事件驱动方式&#xff0c;但在我们使用的过程中&#xff0c;有时候通过调用系统原有的消息&#xff0c;处理起来会比较简单一些&#xff0c;特别是在处理与DLL文件的交互时&#xff0c;的确是非常的方便。 在C#中使用自定义消息 在C#中使用自定义消息非常简单&…

倪光南院士:openEuler与全球开发者共同推动计算产业发展

今日&#xff0c;以“创造最好的OS&#xff0c;成就更好的我们”为主题的 openEuler Developer Day 2021在北京成功举行。本次大会由openEuler社区发起&#xff0c;产业组织专家、学者、企业领袖和开发者们围绕多样性计算、云原生全栈、全场景协同等技术方向共同探讨和创新。大…

jhello框架-ajax

2019独角兽企业重金招聘Python工程师标准>>> 现在的web开发没有ajax都没法活&#xff0c;所以在jhello中实现了一个简单的ajax交互&#xff0c;使用json作为载体。 在上篇《交互》中讲到一种交互的方式是把数据放入Model中&#xff0c;通过ModelAndView类返回到视图…

发微信模版消息换行用\n

发微信模版消息换行用\n发微信模版消息换行用\n发微信模版消息换行用\n发微信模版消息换行用\n发微信模版消息换行用\n转载于:https://blog.51cto.com/xuqin/1974131

二次元会让人脸识别失效吗?

来源 | PyTorch 开发者社区责编 | 寇雪芹头图 | 下载于ICphoto人脸识别也遇到坑了&#xff0c;识别得了三次元&#xff0c;却对二次元无效。迪士尼的技术团队&#xff0c;正在开发这一算法&#xff0c;以帮助动画制作者进行后期搜索。团队利用 PyTorch&#xff0c;效率得到很大…

基于第四层交换技术的负载均衡

摘 要 本文介绍了第四层交换技术的概念&#xff0c;技术原理以及如何使用第四层交换技术实现远程教育系统中的应用服务器负载均衡。 作者&#xff1a; 凌仲权&#xff0c;现就读于西安电子科技大学&#xff0c;2001级硕士研究生。主要的科研方向为计算机网络技术研究以及远程教…

開始Unity3D的学习之旅

前言&#xff1a;这个系列的文章纯属对自己学习的整理&#xff0c;非高手之作。但确实的记载了我作为一个没接触过3D游戏编程的大学生的心路历程。争取每周整理一次吧。之所以会開始学Unity3D&#xff0c;最基本的原因是由于在快放暑假的时候&#xff0c;我找了一家做iPhone游戏…