您的位置:首页 > 编程语言 > C#

[C#]简单加密解密[利用反射获取加密解密类]

2005-11-30 03:57 666 查看
写了有段时间。一直都没有把它改进。最近想改进一下。改好了就放上来
////////以下程序实现2个加密算法[DES][RC2]加密///////////////////
///////使用的IV和KEY是我的姓名[普恺]缩写:PuKai////////////////
//////////////////////////////////////////////////////////////////////////////
接口类:
using System;

namespace OutInterface
{
/// <summary>
/// 接口类
/// </summary>
public interface OutInterface
{
void Encryptor(string oldFileName,string newFileName);
void Decryptor(string oldFileName,string newFileName);
}
}
/////////////////////////////////////////////////////////
实现类:
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using OutInterface;
using System.Security;
namespace EncryptorDecryptor
{
/// <summary>
/// 实现DES加密解密
/// </summary>
public class EncryptorDecryptor:OutInterface.OutInterface
{
public Type cryptographyType;
public EncryptorDecryptor()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public EncryptorDecryptor(string str)
{
cryptographyType = Type.GetType(str);
}

#region OutInterface 成员

public void Encryptor(string oldFileName, string newFileName)
{
System.IO.FileStream Fin = new FileStream(oldFileName,FileMode.Open,FileAccess.Read);

System.IO.FileStream Fout = new FileStream(newFileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);
Fout.SetLength(0);
SymmetricAlgorithm des = (SymmetricAlgorithm)Activator.CreateInstance(cryptographyType);
PasswordToByte(des);

ICryptoTransform transForm = des.CreateEncryptor(des.Key,des.IV);

CryptoStream transString = new CryptoStream(Fout,transForm,CryptoStreamMode.Write);

byte[] bin = new byte[100];
Fin.Read(bin,0,100);
transString.Write(bin,0,100);
transString.Close();
Fin.Close();
Fout.Close();
}

public void Decryptor(string oldFileName, string newFileName)
{
System.IO.FileStream Fin = new FileStream(oldFileName,FileMode.Open,FileAccess.Read);
System.IO.FileStream Fout = new FileStream(newFileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);
Fout.SetLength(0);

SymmetricAlgorithm des = (SymmetricAlgorithm)Activator.CreateInstance(cryptographyType);
PasswordToByte(des);

ICryptoTransform transForm = des.CreateDecryptor(des.Key,des.IV);

CryptoStream transString = new CryptoStream(Fout,transForm,CryptoStreamMode.Write);

byte[] bin = new byte[100];
Fin.Read(bin,0,100);
transString.Write(bin,0,100);
transString.Close();
Fin.Close();
Fout.Close();
}

#endregion
private void PasswordToByte(SymmetricAlgorithm des)
{
byte[] byteA = new byte[8];
byte[] byteB = new byte[8];

System.Text.Encoding ascii = Encoding.ASCII;
ToByte(byteA,ascii.GetBytes("PuKai"));
ToByte(byteB,ascii.GetBytes("PuKai"));

des.Key = byteA;
des.IV = byteB;
}

private void ToByte(byte[] byteC,byte[] encoding)
{
if(byteC.Length >= encoding.Length)
{
for(int i = 0;i < encoding.Length;i++)
{
byteC = encoding;
}
}
else
for(int i = 0;i < byteC.Length;i++)
{
byteC = encoding;
}
}
}

/// <summary>
/// 传出加密解密实现类实例
/// </summary>
public class ShowClass
{
static public string[] stringClass = new string[2];
public ShowClass()
{

}
GetEncryptorptor
static public object GetEncryptorDecryptor(int nItem)
{
switch(nItem)
{
case 0:
return (new EncryptorDecryptor("System.Security.Cryptography.DESCryptoServiceProvider"));
case 1:
return (new EncryptorDecryptor("System.Security.Cryptography.RC2CryptoServiceProvider"));
}
return null;
}
static public string[] GetEncryptorDecryptorString()
{
stringClass[0] = "DES";
stringClass[1] = "RC2";
return stringClass;

}

}

}
//////////////////////////利用实现类中ShowClass.GetClassName and GetEncryptorptor对可以轻松得到实现类(DLL)中加密实例,只用修改增加DLL类中相应的类,就可以是程序的到其他加密算法功能///////////////////////////
窗体类
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using EncryptorDecryptor;
using OutInterface;

namespace 加密解密
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Timer timerOpacity;
private System.Windows.Forms.OpenFileDialog openFileDialogEncryptor;
private System.Windows.Forms.OpenFileDialog openFileDialogDecryptor;
private System.Windows.Forms.TextBox textBoxEncryptor;
private System.Windows.Forms.Button btEncryptorFile;
private System.Windows.Forms.Label labelEncryptorFile;
private System.Windows.Forms.Button btEncryptor;
private System.Windows.Forms.GroupBox groupBoxEncryptor;
private System.Windows.Forms.ComboBox comboBoxEncryptor;
private System.Windows.Forms.Label labelEncryptor;
private System.Windows.Forms.Label labelDecryptorFile;
private System.Windows.Forms.TextBox textBoxDecryptor;
private System.Windows.Forms.Button btDecryptorFile;
private System.Windows.Forms.GroupBox groupBoxDecryptor;
private System.Windows.Forms.ComboBox comboBoxDecryptor;
private System.Windows.Forms.Label labelDecryptor;
private System.Windows.Forms.Button btDecryptor;
private System.ComponentModel.IContainer components;

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.components = new System.ComponentModel.Container();
this.textBoxEncryptor = new System.Windows.Forms.TextBox();
this.btEncryptorFile = new System.Windows.Forms.Button();
this.labelEncryptorFile = new System.Windows.Forms.Label();
this.btEncryptor = new System.Windows.Forms.Button();
this.labelDecryptorFile = new System.Windows.Forms.Label();
this.textBoxDecryptor = new System.Windows.Forms.TextBox();
this.btDecryptorFile = new System.Windows.Forms.Button();
this.btDecryptor = new System.Windows.Forms.Button();
this.groupBoxEncryptor = new System.Windows.Forms.GroupBox();
this.comboBoxEncryptor = new System.Windows.Forms.ComboBox();
this.labelEncryptor = new System.Windows.Forms.Label();
this.groupBoxDecryptor = new System.Windows.Forms.GroupBox();
this.comboBoxDecryptor = new System.Windows.Forms.ComboBox();
this.labelDecryptor = new System.Windows.Forms.Label();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.timerOpacity = new System.Windows.Forms.Timer(this.components);
this.openFileDialogEncryptor = new System.Windows.Forms.OpenFileDialog();
this.openFileDialogDecryptor = new System.Windows.Forms.OpenFileDialog();
this.groupBoxEncryptor.SuspendLayout();
this.groupBoxDecryptor.SuspendLayout();
this.SuspendLayout();
//
// textBoxEncryptor
//
this.textBoxEncryptor.Location = new System.Drawing.Point(80, 24);
this.textBoxEncryptor.Name = "textBoxEncryptor";
this.textBoxEncryptor.Size = new System.Drawing.Size(448, 21);
this.textBoxEncryptor.TabIndex = 0;
this.textBoxEncryptor.Text = "";
this.textBoxEncryptor.TextChanged += new System.EventHandler(this.textBoxEncryptor_TextChanged);
//
// btEncryptorFile
//
this.btEncryptorFile.Location = new System.Drawing.Point(552, 24);
this.btEncryptorFile.Name = "btEncryptorFile";
this.btEncryptorFile.Size = new System.Drawing.Size(120, 23);
this.btEncryptorFile.TabIndex = 1;
this.btEncryptorFile.Text = "【选择加密文件】";
this.btEncryptorFile.Click += new System.EventHandler(this.btEncryptorFile_Click);
//
// labelEncryptorFile
//
this.labelEncryptorFile.Location = new System.Drawing.Point(8, 24);
this.labelEncryptorFile.Name = "labelEncryptorFile";
this.labelEncryptorFile.Size = new System.Drawing.Size(72, 23);
this.labelEncryptorFile.TabIndex = 2;
this.labelEncryptorFile.Text = "文件地址:";
//
// btEncryptor
//
this.btEncryptor.Location = new System.Drawing.Point(576, 64);
this.btEncryptor.Name = "btEncryptor";
this.btEncryptor.TabIndex = 3;
this.btEncryptor.Text = "加密";
this.btEncryptor.Click += new System.EventHandler(this.btEncryptor_Click);
//
// labelDecryptorFile
//
this.labelDecryptorFile.Location = new System.Drawing.Point(8, 24);
this.labelDecryptorFile.Name = "labelDecryptorFile";
this.labelDecryptorFile.Size = new System.Drawing.Size(72, 23);
this.labelDecryptorFile.TabIndex = 4;
this.labelDecryptorFile.Text = "文件地址:";
//
// textBoxDecryptor
//
this.textBoxDecryptor.Location = new System.Drawing.Point(80, 24);
this.textBoxDecryptor.Name = "textBoxDecryptor";
this.textBoxDecryptor.Size = new System.Drawing.Size(448, 21);
this.textBoxDecryptor.TabIndex = 5;
this.textBoxDecryptor.Text = "";
this.textBoxDecryptor.TextChanged += new System.EventHandler(this.textBoxDecryptor_TextChanged);
//
// btDecryptorFile
//
this.btDecryptorFile.Location = new System.Drawing.Point(552, 24);
this.btDecryptorFile.Name = "btDecryptorFile";
this.btDecryptorFile.Size = new System.Drawing.Size(120, 23);
this.btDecryptorFile.TabIndex = 6;
this.btDecryptorFile.Text = "【选择解密文件】";
this.btDecryptorFile.Click += new System.EventHandler(this.btDecryptorFile_Click);
//
// btDecryptor
//
this.btDecryptor.Location = new System.Drawing.Point(576, 64);
this.btDecryptor.Name = "btDecryptor";
this.btDecryptor.TabIndex = 7;
this.btDecryptor.Text = "解密";
this.btDecryptor.Click += new System.EventHandler(this.btDecryptor_Click);
//
// groupBoxEncryptor
//
this.groupBoxEncryptor.Controls.Add(this.comboBoxEncryptor);
this.groupBoxEncryptor.Controls.Add(this.labelEncryptor);
this.groupBoxEncryptor.Controls.Add(this.labelEncryptorFile);
this.groupBoxEncryptor.Controls.Add(this.textBoxEncryptor);
this.groupBoxEncryptor.Controls.Add
(this.btEncryptorFile);
this.groupBoxEncryptor.Controls.Add(this.btEncryptor);
this.groupBoxEncryptor.Location = new System.Drawing.Point(16, 8);
this.groupBoxEncryptor.Name = "groupBoxEncryptor";
this.groupBoxEncryptor.Size = new System.Drawing.Size(704, 100);
this.groupBoxEncryptor.TabIndex = 8;
this.groupBoxEncryptor.TabStop = false;
this.groupBoxEncryptor.Text = "加密";
//
// comboBoxEncryptor
//
this.comboBoxEncryptor.Location = new System.Drawing.Point(376, 72);
this.comboBoxEncryptor.Name = "comboBoxEncryptor";
this.comboBoxEncryptor.Size = new System.Drawing.Size(152, 20);
this.comboBoxEncryptor.TabIndex = 5;
//
// labelEncryptor
//
this.labelEncryptor.Location = new System.Drawing.Point(280, 72);
this.labelEncryptor.Name = "labelEncryptor";
this.labelEncryptor.Size = new System.Drawing.Size(96, 23);
this.labelEncryptor.TabIndex = 4;
this.labelEncryptor.Text = "选择加密方式:";
//
// groupBoxDecryptor
//
this.groupBoxDecryptor.Controls.Add(this.comboBoxDecryptor);
this.groupBoxDecryptor.Controls.Add(this.labelDecryptor);
this.groupBoxDecryptor.Controls.Add(this.textBoxDecryptor);
this.groupBoxDecryptor.Controls.Add(this.labelDecryptorFile);
this.groupBoxDecryptor.Controls.Add(this.btDecryptorFile);
this.groupBoxDecryptor.Controls.Add(this.btDecryptor);
this.groupBoxDecryptor.Location = new System.Drawing.Point(16, 128);
this.groupBoxDecryptor.Name = "groupBoxDecryptor";
this.groupBoxDecryptor.Size = new System.Drawing.Size(704, 100);
this.groupBoxDecryptor.TabIndex = 9;
this.groupBoxDecryptor.TabStop = false;
this.groupBoxDecryptor.Text = "解密";
//
// comboBoxDecryptor
//
this.comboBoxDecryptor.Location = new System.Drawing.Point(376, 64);
this.comboBoxDecryptor.Name = "comboBoxDecryptor";
this.comboBoxDecryptor.Size = new System.Drawing.Size(152, 20);
this.comboBoxDecryptor.TabIndex = 9;
//
// labelDecryptor
//
this.labelDecryptor.Location = new System.Drawing.Point(280, 64);
this.labelDecryptor.Name = "labelDecryptor";
this.labelDecryptor.Size = new System.Drawing.Size(96, 23);
this.labelDecryptor.TabIndex = 8;
this.labelDecryptor.Text = "选择解密方式:";
//
// statusBar1
//
this.statusBar1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.statusBar1.Location = new System.Drawing.Point(0, 240);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(712, 22);
this.statusBar1.TabIndex = 10;
this.statusBar1.Text = "版权信息:普恺 Qq:1325625 | E-mail:BabyCr" +
"azy@Qq.com";
//
// timerOpacity
//
this.timerOpacity.Tick += new System.EventHandler(this.timerOpacity_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.DarkGray;
this.ClientSize = new System.Drawing.Size(712, 262);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.groupBoxDecryptor);
this.Controls.Add(this.groupBoxEncryptor);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBoxEncryptor.ResumeLayout(false);
this.groupBoxDecryptor.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
try
{
this.timerOpacity.Enabled = true;
this.Opacity = 0;
this.textBoxEncryptor.Enabled = false;
this.textBoxDecryptor.Enabled = false;
this.openFileDialogEncryptor.Filter = "所有文件|*.*";
// OutInterface myInterFace = (OutInterface)ShowClass.GetClassName();
foreach(object a in ShowClass.GetEncryptorDecryptorString())
{
this.comboBoxEncryptor.Items.Add(a);
this.comboBoxDecryptor.Items.Add(a);
}

this.btEncryptor.Enabled = false;
this.btDecryptor.Enabled = false;
}
catch(System.Exception ee)
{
MessageBox.Show(ee.Message);
}
}

private void timerOpacity_Tick(object sender, System.EventArgs e)
{
while(this.Opacity < 0.99)
{
this.Opacity +=0.01;
}
this.timerOpacity.Enabled = false;
}

private void btEncryptorFile_Click(object sender, System.EventArgs e)
{
this.openFileDialogEncryptor.ShowDialog();
this.textBoxEncryptor.Text = this.openFileDialogEncryptor.FileName;
}

private void btDecryptorFile_Click(object sender, System.EventArgs e)
{
this.openFileDialogDecryptor.ShowDialog();
this.textBoxDecryptor.Text = this.openFileDialogDecryptor.FileName;

}

private void textBoxEncryptor_TextChanged(object sender, System.EventArgs e)
{
if(this.textBoxEncryptor.Text != "")
this.btEncryptor.Enabled = true;
}

private void textBoxDecryptor_TextChanged(object sender, System.EventArgs e)
{
if(this.textBoxDecryptor.Text != "")
this.btDecryptor.Enabled = true;
}

private void btEncryptor_Click(object sender, System.EventArgs e)
{
OutInterface.OutInterface myInterface = (OutInterface.OutInterface)EncryptorDecryptor.ShowClass.GetEncryptorDecryptor(this.comboBoxEncryptor.SelectedIndex);
myInterface.Encryptor(this.textBoxEncryptor.Text,this.textBoxEncryptor.Text + "." + EncryptorDecryptor.ShowClass.GetEncryptorDecryptorString()[this.comboBoxEncryptor.SelectedIndex]);
}

private void btDecryptor_Click(object sender, System.EventArgs e)
{
OutInterface.OutInterface myInterface = (OutInterface.OutInterface)EncryptorDecryptor.ShowClass.GetEncryptorDecryptor(this.comboBoxDecryptor.SelectedIndex);
myInterface.Decryptor(this.textBoxDecryptor.Text,this.textBoxDecryptor.Text.Replace("." + EncryptorDecryptor.ShowClass.GetEncryptorDecryptorString()[this.comboBoxDecryptor.SelectedIndex],""));
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: