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 RSA_Encrypt
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </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.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(176, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(160, 23);
this.button1.TabIndex = 0;
this.button1.Text = "用RSA算法加密文件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(184, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(152, 23);
this.button2.TabIndex = 1;
this.button2.Text = "用RSA算法解密文件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 117);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "文件加密、解密的RSA算法程式";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
/// <summary>
/// 功能:.打开文件,加密,加密数据到文件,密钥到文件:
/// 作者:黄海
/// 时间:2007-4-6
/// </summary>
private void EncryptionFile(string InputFileName,string ResultFileName,string KeyFileName)
{
//初始化RSA加密,如果觉得速度慢了,可以自己定义密钥的大小,
//new RSACryptoServiceProvider(512);
RSACryptoServiceProvider rsac = new RSACryptoServiceProvider();
//字节缓冲区定为64(密钥为1024),最大长度为(rsac.KeySize/8 - 11)
byte[] src = new byte[64];
//Encrypt.cs是要加密的文件
FileStream fs = new FileStream(InputFileName, FileMode.Open, FileAccess.Read);
//Result.txt存储的是加密之后的数据
FileStream res = new FileStream(ResultFileName, FileMode.Create, FileAccess.Write);
int len;
//加密
while ((len = fs.Read(src, 0, 64)) > 0)
{
byte[] temp = new byte[len];
Array.Copy(src, 0, temp, 0, len);
temp = rsac.Encrypt(temp, false);
res.Write(temp, 0, temp.Length);
}
fs.Close();
res.Close();
//存储密钥到文件,以便解密用。
StreamWriter sw = new StreamWriter(KeyFileName);
sw.Write(rsac.ToXmlString(true));
sw.Close();
}
/// <summary>
/// 功能:.打开文件,读密钥,读加密数据,解密,原始纯文本(解密数据)到文件:
/// 作者:黄海
/// 时间:2007-4-6
/// </summary>
private void DecryptionFile (string DecryptFileName,string ResultFileName,string KeyFileName)
{
RSACryptoServiceProvider rsac = new RSACryptoServiceProvider();
//读密钥
StreamReader sr = new StreamReader(KeyFileName);
String str = sr.ReadToEnd();
sr.Close();
rsac.FromXmlString(str);
byte[] src = new byte[128];
FileStream fs = new FileStream(ResultFileName, FileMode.Open, FileAccess.Read);
FileStream res = new FileStream(DecryptFileName, FileMode.Create, FileAccess.Write);
int len;
//解密
while ((len = fs.Read(src, 0, 128)) > 0)
{
byte[] temp = new byte[len];
Array.Copy(src, 0, temp, 0, len);
temp = rsac.Decrypt(temp, false);
res.Write(temp, 0, temp.Length);
}
fs.Close();
res.Close();
}
private void button1_Click(object sender, System.EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
//开始加密文件!
this.EncryptionFile(openFileDialog1.FileName,"c://111.txt","c://RSA.KEY");
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.DecryptionFile("d://HuangHai.txt","c://111.txt","c://RSA.KEY");
}
}
}