C#简单加密解密[利用反射获取加密解密类]
接口类:
using System;
namespace OutInte易做图ce
{
///
/// Class1 的摘要说明。
///
public
inte易做图ce OutInte易做图ce
{
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 OutInte易做图ce;
using
System.Security;
namespace EncryptorDecryptor
{
///
/// 实现DES加密解密
///
public class
EncryptorDecryptor:OutInte易做图ce.OutInte易做图ce
{
public Type
cryptographyType;
public EncryptorDecryptor()
{
//
// TODO:
在此处添加构造函数逻辑
//
}
public EncryptorDecryptor(string str)
{
cryptographyType = Type.GetType(str);
}
#region OutInte易做图ce 成员
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
/// 传出加密解密实现类实例
///
public class ShowClass
{
static public string[]
stringClass = new string[2];
public ShowClass()
{
}
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;
}
}
}
窗体类
using System;
using System.Drawing;
using
System.Collections;
using System.ComponentModel;
using
System.Windows.Forms;
using System.Data;
using EncryptorDecryptor;
using OutInte易做图ce;
namespace 加密解密
{
///
/// Form1 的摘要说明。
///
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 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected
override void Dispose( bool disposing )
{
if( disposing )
{
if
(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
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
/
补充:综合编程 , 安全编程 ,