C# 关于DSA&RSA两种不同加密方式的例子
常用的两种加密方式.
- DSA

以下是代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.IO;
namespace Journal_Entry_Sub_System

{
/**////
/// Form1 的摘要说明。
///
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/**////
/// 必需的设计器变量。
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.OpenFileDialog openfile;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.SaveFileDialog savefile;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.CheckBox checkBox1;
//创建实例
RijndaelManaged rij = new RijndaelManaged();//
//全局变量,标记文件的扩展名
private static string ext=null;
//标记加密成功与否
private static bool enresult = false;
//标记解密成功与否的标记
private static bool deresult = false;

/**////
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

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

Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**////
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.openfile = new System.Windows.Forms.OpenFileDialog();
this.button3 = new System.Windows.Forms.Button();
this.savefile = new System.Windows.Forms.SaveFileDialog();
this.button4 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(73, 89);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(140, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(73, 126);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(140, 20);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(73, 186);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(63, 21);
this.button1.TabIndex = 2;
this.button1.Text = "加密";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 186);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(62, 21);
this.button2.TabIndex = 3;
this.button2.Text = "解密";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox3
//
this.textBox3.HideSelection = false;
this.textBox3.Location = new System.Drawing.Point(73, 45);
this.textBox3.MaxLength = 17;
this.textBox3.Name = "textBox3";
this.textBox3.PasswordChar = '$';
this.textBox3.Size = new System.Drawing.Size(84, 20);
this.textBox3.TabIndex = 4;
this.textBox3.Text = "";
this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// button3
//
this.button3.Location = new System.Drawing.Point(233, 89);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(34, 21);
this.button3.TabIndex = 5;
this.button3.Text = "Open";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(233, 126);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(34, 22);
this.button4.TabIndex = 6;
this.button4.Text = "Save";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(73, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(114, 22);
this.label1.TabIndex = 7;
this.label1.Text = "请输入你的密码:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(180, 45);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(153, 21);
this.label2.TabIndex = 8;
this.label2.Text = "(密码大小8至16个字符)";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(73, 156);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(180, 22);
this.checkBox1.TabIndex = 9;
this.checkBox1.Text = "删除源文件";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(379, 258);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "DSA";
this.ResumeLayout(false);
}
#endregion

/**////
/// 应用程序的主入口点。
///
// [STAThread]
// static void Main()
// {
// Application.Run(new Form1());
// }
//加密文件的按钮
private void button1_Click(object sender, System.EventArgs e)
{
if(textBox1.Text != null || textBox2.Text != null)
encryption(textBox3.Text,textBox1.Text,textBox2.Text);
if(checkBox1.Checked==true && enresult==true)
DeleteFile(textBox1.Text);
}
//用于加密的函数
public void encryption(string textBox,string readfile,string writefile)
{
try
{
if(textBox.Length >=8 && textBox.Length<=16 )//判断密码的字符的大小
{
byte [] key = System.Text.Encoding.Default.GetBytes( textBox );
byte [] iv = rij.IV;
Rijndael crypt = Rijndael.Create();
ICryptoTransform transform = crypt.CreateEncryptor(key ,iv);
//写进文件
FileStream fswrite = new FileStream(writefile,FileMode.Create);
CryptoStream cs = new CryptoStream( fswrite ,transform ,CryptoStreamMode.Write );
//打开文件
FileStream fsread = new FileStream(readfile,FileMode.Open);
int length;
while((length = fsread.ReadByte() )!= -1)
cs.WriteByte((byte)length);
fsread.Close();
cs.Close();
fswrite.Close();
enresult = true ;//成功加密
MessageBox.Show("已经成功完成加密任务!");
}
else
{
MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!");
return ;
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
//用于解密的函数
public void decryption(string textBox,string readfile,string writefile)
{
try
{
if(textBox.Length >=8 && textBox.Length<=16 )
{
byte [] key = System.Text.Encoding.Default.GetBytes( textBox );
byte [] iv = rij.IV;
Rijndael crypt = Rijndael.Create();
ICryptoTransform transform = crypt.CreateDecryptor(key,iv);
//读取加密后的文件
FileStream fsopen = new FileStream(readfile,FileMode.Open);
CryptoStream cs = new CryptoStream( fsopen ,transform ,CryptoStreamMode.Read );
//把解密后的结果写进文件
FileStream fswrite = new FileStream(writefile,FileMode.OpenOrCreate);
int length;
while ((length = cs.ReadByte()) != -1)
fswrite.WriteByte((byte)length);
fswrite.Close();
cs.Close();
fsopen.Close();
deresult=true; //成功解密
MessageBox.Show("已经成功完成解密任务!");
}
else
{
MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!");
return ;
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
//解密文件
private void button2_Click(object sender, System.EventArgs e)
{
decryption( textBox3.Text,textBox1.Text,textBox2.Text ) ;
if(checkBox1.Checked==true && deresult==true)
DeleteFile(textBox1.Text);
}
//用于打开文件的按钮
private void button3_Click(object sender, System.EventArgs e)
{
openfile = new OpenFileDialog();openfile.Filter ="All files (*.*)|*.*" ;
openfile.ShowDialog();
textBox1.Text = openfile.FileName;
ext = getfileext(openfile.FileName);
}
private void button4_Click(object sender, System.EventArgs e)
{
savefile = new SaveFileDialog();
savefile.Filter =ext + " files" +"(*."+ext+")|*."+ ext +"|All files (*.*)|*.*" ;
savefile.ShowDialog();
textBox2.Text = savefile.FileName;
}
//得到文件的扩展名
private string getfileext(string filename)
{
try
{
char [] point = new char[]
{'.'};
string [] filename2 = filename.Split(point);
return filename2[1];
}
catch
{
return null;
}
}
//删除文件的函数
public void DeleteFile(string filename)
{
try
{
File.Delete(filename);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}

}
}
- RSA
以下是代码
![]()
Code
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;
using System.Threading;

namespace Journal_Entry_Sub_System


{

/**//// <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.TabPage tabPage3;
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;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.RichTextBox richtext4;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.TabPage tabPage4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Button button19;
private System.Windows.Forms.Button button20;
private System.Windows.Forms.Button button21;
private System.Windows.Forms.RichTextBox richtext5;

/**//// <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 bool openla = false;
private bool savela = false;
private bool readpubkey=false;
private bool readprikey=false;
private Thread th;

/**//// </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 );
}


Windows 窗体设计器生成的代码#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.tabPage3 = new System.Windows.Forms.TabPage();
this.button15 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.richtext4 = new System.Windows.Forms.RichTextBox();
this.label6 = new System.Windows.Forms.Label();
this.button10 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button9 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.tabPage4 = new System.Windows.Forms.TabPage();
this.richtext5 = new System.Windows.Forms.RichTextBox();
this.button21 = new System.Windows.Forms.Button();
this.button20 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button16 = new System.Windows.Forms.Button();
this.textBox4 = new System.Windows.Forms.TextBox();
this.save = new System.Windows.Forms.SaveFileDialog();
this.open = new System.Windows.Forms.OpenFileDialog();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.tabPage3.SuspendLayout();
this.tabPage4.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Controls.Add(this.tabPage4);
this.tabControl1.Location = new System.Drawing.Point(7, 59);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(473, 342);
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, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(465, 316);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "得到钥匙";
//
// button6
//
this.button6.Location = new System.Drawing.Point(180, 267);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(62, 22);
this.button6.TabIndex = 4;
this.button6.Text = "写入文件";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(380, 15);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(62, 21);
this.button3.TabIndex = 3;
this.button3.Text = "保存私匙";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(267, 15);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(62, 21);
this.button2.TabIndex = 2;
this.button2.Text = "保存公钥";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richtext
//
this.richtext.Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.richtext.Location = new System.Drawing.Point(13, 52);
this.richtext.Name = "richtext";
this.richtext.Size = new System.Drawing.Size(434, 171);
this.richtext.TabIndex = 1;
this.richtext.Text = "richTextBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(113, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(74, 21);
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, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Size = new System.Drawing.Size(465, 316);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "加密解密文字";
//
// button8
//
this.button8.Location = new System.Drawing.Point(307, 67);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(62, 21);
this.button8.TabIndex = 9;
this.button8.Text = "读取私匙";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(100, 67);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(62, 21);
this.button7.TabIndex = 8;
this.button7.Text = "读取公匙";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(253, 119);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(84, 15);
this.label3.TabIndex = 7;
this.label3.Text = "解密结果";
//
// richtext3
//
this.richtext3.Location = new System.Drawing.Point(233, 141);
this.richtext3.Name = "richtext3";
this.richtext3.Size = new System.Drawing.Size(227, 149);
this.richtext3.TabIndex = 6;
this.richtext3.Text = "richTextBox2";
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 119);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(107, 15);
this.label2.TabIndex = 5;
this.label2.Text = "加密窗口:";
// richtext2
//
this.richtext2.Location = new System.Drawing.Point(7, 141);
this.richtext2.Name = "richtext2";
this.richtext2.Size = new System.Drawing.Size(213, 149);
this.richtext2.TabIndex = 4;
this.richtext2.Text = "richTextBox2";
//
// button5
//
this.button5.Location = new System.Drawing.Point(293, 89);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(94, 21);
this.button5.TabIndex = 3;
this.button5.Text = "使用私匙解密";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(93, 89);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(80, 21);
this.button4.TabIndex = 2;
this.button4.Text = "使用公匙加密";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(87, 30);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(220, 29);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// label1
//
this.label1.Location = new System.Drawing.Point(107, 7);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 15);
this.label1.TabIndex = 0;
this.label1.Text = "请输入加密的文字";
//
// tabPage3
//
this.tabPage3.Controls.Add(this.button15);
this.tabPage3.Controls.Add(this.button14);
this.tabPage3.Controls.Add(this.button13);
this.tabPage3.Controls.Add(this.button12);
this.tabPage3.Controls.Add(this.button11);
this.tabPage3.Controls.Add(this.richtext4);
this.tabPage3.Controls.Add(this.label6);
this.tabPage3.Controls.Add(this.button10);
this.tabPage3.Controls.Add(this.label5);
this.tabPage3.Controls.Add(this.textBox3);
this.tabPage3.Controls.Add(this.button9);
this.tabPage3.Controls.Add(this.label4);
this.tabPage3.Controls.Add(this.textBox2);
this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Name = "tabPage3";
this.tabPage3.Size = new System.Drawing.Size(465, 316);
this.tabPage3.TabIndex = 2;
this.tabPage3.Text = "加密解密文件";
//
// button15
//
this.button15.Location = new System.Drawing.Point(173, 119);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(63, 21);
this.button15.TabIndex = 12;
this.button15.Text = "清空文本";
this.button15.Click += new System.EventHandler(this.button15_Click);
//
// button14
//
this.button14.Location = new System.Drawing.Point(360, 82);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(62, 21);
this.button14.TabIndex = 11;
this.button14.Text = "读取私匙";
this.button14.Click += new System.EventHandler(this.button14_Click);
//
// button13
//
this.button13.Location = new System.Drawing.Point(360, 30);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(62, 21);
this.button13.TabIndex = 10;
this.button13.Text = "读取公匙";
this.button13.Click += new System.EventHandler(this.button13_Click);
//
// button12
//
this.button12.Location = new System.Drawing.Point(267, 82);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(62, 21);
this.button12.TabIndex = 9;
this.button12.Text = "开始解密";
this.button12.Click += new System.EventHandler(this.button12_Click);
//
// button11
//
this.button11.Location = new System.Drawing.Point(267, 30);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(62, 21);
this.button11.TabIndex = 8;
this.button11.Text = "开始加密";
this.button11.Click += new System.EventHandler(this.button11_Click);
//
// richtext4
//
this.richtext4.Location = new System.Drawing.Point(7, 141);
this.richtext4.Name = "richtext4";
this.richtext4.Size = new System.Drawing.Size(413, 156);
this.richtext4.TabIndex = 7;
this.richtext4.Text = "";
//
// label6
//
this.label6.Location = new System.Drawing.Point(13, 111);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(140, 22);
this.label6.TabIndex = 6;
this.label6.Text = "加密与解密窗口:";
//
// button10
//
this.button10.Location = new System.Drawing.Point(187, 82);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(62, 21);
this.button10.TabIndex = 5;
this.button10.Text = "Save
";
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// label5
//
this.label5.Location = new System.Drawing.Point(13, 59);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(180, 15);
this.label5.TabIndex = 4;
this.label5.Text = "请保存加密好或者解密后的文件";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(13, 82);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(154, 20);
this.textBox3.TabIndex = 3;
this.textBox3.Text = "textBox3";
//
// button9
//
this.button9.Location = new System.Drawing.Point(187, 30);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(62, 21);
this.button9.TabIndex = 2;
this.button9.Text = "Open
";
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(13, 7);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(180, 15);
this.label4.TabIndex = 1;
this.label4.Text = "请选择要加密或者解密的文本文件";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(13, 30);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(154, 20);
this.textBox2.TabIndex = 0;
this.textBox2.Text = "textBox2";
//
// tabPage4
//
this.tabPage4.Controls.Add(this.richtext5);
this.tabPage4.Controls.Add(this.button21);
this.tabPage4.Controls.Add(this.button20);
this.tabPage4.Controls.Add(this.button19);
this.tabPage4.Controls.Add(this.button18);
this.tabPage4.Controls.Add(this.button17);
this.tabPage4.Controls.Add(this.textBox5);
this.tabPage4.Controls.Add(this.button16);
this.tabPage4.Controls.Add(this.textBox4);
this.tabPage4.Location = new System.Drawing.Point(4, 22);
this.tabPage4.Name = "tabPage4";
this.tabPage4.Size = new System.Drawing.Size(465, 316);
this.tabPage4.TabIndex = 3;
this.tabPage4.Text = "加密解密其它文件";
//
// richtext5
//
this.richtext5.Location = new System.Drawing.Point(73, 186);
this.richtext5.Name = "richtext5";
this.richtext5.Size = new System.Drawing.Size(254, 104);
this.richtext5.TabIndex = 8;
this.richtext5.Text = "";
//
// button21
//
this.button21.Location = new System.Drawing.Point(193, 134);
this.button21.Name = "button21";
this.button21.Size = new System.Drawing.Size(63, 21);
this.button21.TabIndex = 7;
this.button21.Text = "解密文件";
this.button21.Click += new System.EventHandler(this.button21_Click);
//
// button20
//
this.button20.Location = new System.Drawing.Point(93, 134);
this.button20.Name = "button20";
this.button20.Size = new System.Drawing.Size(63, 21);
this.button20.TabIndex = 6;
this.button20.Text = "加密文件";
this.button20.Click += new System.EventHandler(this.button20_Click);
//
// button19
//
this.button19.Location = new System.Drawing.Point(133, 82);
this.button19.Name = "button19";
this.button19.Size = new System.Drawing.Size(63, 21);
this.button19.TabIndex = 5;
this.button19.Text = "读取私匙";
this.button19.Click += new System.EventHandler(this.button19_Click);
//
// button18
//
this.button18.Location = new System.Drawing.Point(40, 82);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(62, 21);
this.button18.TabIndex = 4;
this.button18.Text = "读取公匙";
this.button18.Click += new System.EventHandler(this.button18_Click);
//
// button17
//
this.button17.Location = new System.Drawing.Point(253, 52);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(63, 21);
this.button17.TabIndex = 3;
this.button17.Text = "保存";
this.button17.Click += new System.EventHandler(this.button17_Click);
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(20, 52);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(200, 20);
this.textBox5.TabIndex = 2;
this.textBox5.Text = "textBox5";
//
// button16
//
this.button16.Location = new System.Drawing.Point(253, 22);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(63, 22);
this.button16.TabIndex = 1;
this.button16.Text = "打开";
this.button16.Click += new System.EventHandler(this.button16_Click);
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(20, 22);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(200, 20);
this.textBox4.TabIndex = 0;
this.textBox4.Text = "textBox4";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(486, 420);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "RSA";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.tabPage3.ResumeLayout(false);
this.tabPage4.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 button4_Click(object sender, System.EventArgs e)

{
// if(textBox1.Text =="")
// {
// MessageBox.Show("加密文字信息不能为空!");
// return;
// }
// if(readpubkey==false)
// {
// MessageBox.Show("请打开公钥所在的文件!");
// return;
// }
try

{
readpublickey = "";

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);
richtext2.Text="加密结果:\n"+encryttext+"\n"+"加密结束!";
}
catch

{
MessageBox.Show("请检查是否打开公匙或者公匙是否损坏!");
}
}
//使用私匙解密
private void button5_Click(object sender, System.EventArgs e)

{
// if(readprikey==false)
// {
// MessageBox.Show("请打开密匙所在的文件!");
// return;
// }
readprivatekey = "<RSAKeyValue><Modulus>xTzjEF8KYJVsSXztud+EPO8iNlvgDjlhS5Kbm3zmfFJdRTUBz1y2o3ydBBF8nUvYWeujXxTAdDk3rHbXueF0J29WIi1BZusq3FdpJ803BeSROl2f+V7NLrfwV1FoDvqdPXpTfmVzdxykTF26AIu3HTUYI0PaAxY1Hm0KCIar6Ks=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

try

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

{
MessageBox.Show("请检查是否打开私匙或者私匙是否损坏!");
}
}
//把钥匙信息写入文件
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 button7_Click(object sender, System.EventArgs e)

{
readpublickey = ReadPublicKey();
readpubkey=true;
}
//从文件中读取私匙信息
private void button8_Click(object sender, System.EventArgs e)

{
readprivatekey = ReadPrivateKey();
readprikey = true;
}
//打开加密或者解密的文件
private void button9_Click(object sender, System.EventArgs e)

{
open = new OpenFileDialog( );
open.Filter="Text File (*.txt)|*.txt|All Files (*.*)|*.*";
open.ShowDialog();
textBox2.Text=open.FileName;
openla = true;
}
//保存加密或者解密的文件
private void button10_Click(object sender, System.EventArgs e)

{
try

{
save=new SaveFileDialog();
save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
save.ShowDialog();
textBox3.Text=save.FileName;
savela = true;
}
catch

{
MessageBox.Show("请输入文件名字!");
return;
}

}
//加密文本文件
private void button11_Click(object sender, System.EventArgs e)

{
if(readpubkey != true)

{
MessageBox.Show("请打开公钥所在的文件!");
return;
}
if(openla==true&&savela==true)

{
crypt=new RSACryptoServiceProvider();
crypt.FromXmlString( readpublickey );
UTF8Encoding enc=new UTF8Encoding();
//读取原文件到一个string 去啊
StreamReader sr=new StreamReader(textBox2.Text,Encoding.Default);
string textinfo=sr.ReadToEnd();
sr.Close();
// richtext4.AppendText( "\n原文件内容:\n" + textinfo + "\n" );
//开始加密
string readinfo=EncryptFile(textinfo,textBox3.Text);
richtext4.AppendText( "加密文件已经保存到:" + textBox3.Text + "\n" );
richtext4.AppendText( "加密结果:\n" + readinfo + "\n加密结束!" );
}
else
MessageBox.Show("请选择你要加密的文件或者要保存的文件!");
}
//解密文件
private void button12_Click(object sender, System.EventArgs e)

{
if(readprikey==false)

{
MessageBox.Show("请打开密匙所在的文件!");
return;
}
if(openla==true&&savela==true)

{
try

{
crypt=new RSACryptoServiceProvider();
crypt.FromXmlString( readprivatekey );
string decryptinfo = DecryptFile(textBox2.Text,textBox3.Text);
richtext4.AppendText( "解密文件已经保存到:" + textBox3.Text+ "\n" );
richtext4.AppendText("解密结果:\n" + decryptinfo + "\n" + "解密结束!\n");
}
catch

{
MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
return;
}
}
else
MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
}
//加密文件时候,同样要打开公钥匙
private void button13_Click(object sender, System.EventArgs e)

{
readpublickey = ReadPublicKey();
readpubkey=true;
}
//解密文件时候,同样要打开私钥匙
private void button14_Click(object sender, System.EventArgs e)

{
readprivatekey =ReadPrivateKey();
readprikey=true;
}
private void button15_Click(object sender, System.EventArgs e)

{
richtext4.Clear();
}
//打开文件
private void button16_Click(object sender, System.EventArgs e)

{
open = new OpenFileDialog( );
open.Filter="All Files (*.*)|*.*";
open.ShowDialog();
textBox4.Text=open.FileName;
openla = true;
}
//保存文件
private void button17_Click(object sender, System.EventArgs e)

{
save=new SaveFileDialog();
save.Filter="All File (*.*)|*.*";
save.ShowDialog();
textBox5.Text=save.FileName;
savela = true;
}


//读取公钥匙
private void button18_Click(object sender, System.EventArgs e)

{
readpublickey = ReadPublicKey();
readpubkey=true;
}
//读取私钥匙
private void button19_Click(object sender, System.EventArgs e)

{
readprivatekey = ReadPrivateKey();
readprikey=true;
}
// 加密其它非文本文件啦
private void button20_Click(object sender, System.EventArgs e)

{
if(readpubkey==false)

{
MessageBox.Show("请打开公匙所在的文件!");
return;
}
if(openla==true&&savela==true)

{
try

{
crypt=new RSACryptoServiceProvider();
crypt.FromXmlString( readpublickey );
EncryptOther( textBox4.Text,textBox5.Text );
richtext5.AppendText("加密过程结束!:\n" + "加密文件已经保存到:\n" + textBox5.Text + "\n");
}
catch

{
MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
return;
}
}
else
MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
}
//解密其它文件啦
private void button21_Click(object sender, System.EventArgs e)

{
if(readprikey==false)

{
MessageBox.Show("请打开密匙所在的文件!");
return;
}
if(openla==true&&savela==true)

{
try

{
crypt=new RSACryptoServiceProvider();
crypt.FromXmlString( readprivatekey );
//使用线程后,反而达不到效果 //原因?暂时不清晰
//可以转换,但是转换要经过几分钟,才可以真正得到想要的转换的东西。
th = new Thread( new ThreadStart( DecryptUsingThread ) );
th.Start();
// DecryptOther(textBox4.Text,textBox5.Text);
richtext5.AppendText( "解密结束!\n" + "解密文件已经保存到:" + textBox5.Text+ "\n" );
}
catch

{
MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
return;
}
}
else
MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
}

//下面为自己定义的函数啦
//单独一个读取公匙的函数
private string ReadPublicKey()

{
string publickey;
open = new OpenFileDialog( );
open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
open.ShowDialog();
StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);
publickey = sr.ReadToEnd();
sr.Close();
return publickey;
}
//单独一个读取私匙的函数
private string ReadPrivateKey()

{
string privatekey;
open = new OpenFileDialog( );
open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
open.ShowDialog();
StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);
privatekey = sr.ReadToEnd();
sr.Close();
return privatekey;
}
//加密文本文件
private string EncryptFile( string textinfo, string filename )

{
UTF8Encoding enc=new UTF8Encoding();
byte [] bytes=enc.GetBytes( textinfo );
int blockSize=0;
if(crypt.KeySize==1024)
blockSize=16;
else
blockSize=8;
//同时写入到内存中去
MemoryStream ms=new MemoryStream();
//写入文件中去啊。
FileStream fs=new FileStream(@filename,FileMode.OpenOrCreate);
byte[]rawblock,encryblock;
for(int i=0;i<bytes.Length;i+=blockSize)

{
if((bytes.Length-i)>blockSize)
rawblock=new byte[blockSize];
else
rawblock =new byte[bytes.Length-i];
Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
encryblock=crypt.Encrypt(rawblock,false);
fs.Write(encryblock,0,encryblock.Length);
//写入到内存中去啊。
ms.Write(encryblock,0,encryblock.Length);
}
//写入到内存中
ms.Position = 0;
byte [] decode = new byte[ms.Length];
ms.Read(decode,0,(int)ms.Length);
string decodeinfo = enc.GetString(decode,0,decode.Length);
fs.Flush();
fs.Close();
ms.Close();
return decodeinfo;
}

//解密文本文件
private string DecryptFile(string encryptfile,string decryptfile)

{
//打开加密后的文件,读取文件内容
FileStream fs=new FileStream(@encryptfile,FileMode.Open);
byte [] bytes=new byte[fs.Length];
fs.Read(bytes,0,(int)fs.Length);
fs.Close();
//先写入到内存
MemoryStream ms=new MemoryStream();
//最好写入到解密后的一个文件
StreamWriter sw=new StreamWriter(decryptfile);
int keySize=crypt.KeySize/8;
byte[]rawblock,decryptblock;
for(int i=0;i<bytes.Length;i+=keySize)

{
if( ( bytes.Length-i ) > keySize )

{
rawblock=new byte[keySize];
}
else

{rawblock =new byte[bytes.Length-i];}

Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
decryptblock = crypt.Decrypt( rawblock,false );
ms.Write(decryptblock,0,decryptblock.Length);
}
ms.Position=0;
byte [] decode=new byte[ms.Length];
ms.Read(decode,0,(int)ms.Length);
UTF8Encoding enc=new UTF8Encoding();
string text=enc.GetString(decode);
//写入到内存中去啊
sw.Write(text);
sw.Flush();
ms.Close();
sw.Close();
return text;
}

//加密其它非文本文件,当然可以包括文本文件
private void EncryptOther(string file1,string file2)

{
FileStream picfs=new FileStream(@file1,FileMode.Open);
FileStream fs=new FileStream(@file2,FileMode.OpenOrCreate);
byte [] bytes=new byte[picfs.Length];
picfs.Read(bytes,0,(int)picfs.Length);
picfs.Close();
int blockSize=0;
if(crypt.KeySize==1024)
blockSize=16;
else blockSize=8;
byte[]rawblock,encryblock;
for(int i=0;i<bytes.Length;i+=blockSize)

{
if((bytes.Length-i)>blockSize)
rawblock=new byte[blockSize];
else rawblock =new byte[bytes.Length-i];
Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
encryblock=crypt.Encrypt(rawblock,false);
fs.Write(encryblock,0,encryblock.Length);
}
fs.Flush();
fs.Close();
}
//解密其它非文本文件,当然可以包括文本文件
private void DecryptOther(string fileone,string filetwo)

{
FileStream fs=new FileStream(@fileone,FileMode.Open);
FileStream writopic=new FileStream(@filetwo,FileMode.Create);
byte [] bytes=new byte[fs.Length];
fs.Read(bytes,0,(int)fs.Length);
fs.Close();
MemoryStream ms=new MemoryStream();
int keySize=crypt.KeySize/8;
byte[]rawblock,decryptblock;
for(int i=0;i<bytes.Length;i+=keySize)

{
if( ( bytes.Length-i ) > keySize )

{
rawblock=new byte[keySize];
}
else

{rawblock =new byte[bytes.Length-i];}

Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
decryptblock = crypt.Decrypt( rawblock,false );
ms.Write(decryptblock,0,decryptblock.Length);
}
ms.Position=0;
byte [] decode=new byte[ms.Length];
ms.Read(decode,0,(int)ms.Length);
ms.Close();
writopic.Write(decode,0,decode.Length);
writopic.Close();

}
//创建一个函数,可以在线程中使用,经实验,效果不明显
private void DecryptUsingThread()

{
DecryptOther(textBox4.Text,textBox5.Text);
}
}
}

这个例子会有一点小错误,但是不影响学习.可以自己修改一下.

浙公网安备 33010602011771号