C#简单加密解密[利用反射获取加密解密类
|
骇客基地 阅读: 时间:2005-11-1 1:04:53 来源:www.hookbase.com
| |
接口类: using System; namespace OutInterface { /// /// Class1 的摘要说明。 /// 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 { /// /// 实现DES加密解密 /// 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 /// 传出加密解密实现类实例 /// 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 OutInterface; 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 // 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 /// /// 应用程序的主入口点。 /// [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
|
|
|
注意事项: |
|
* 【C#简单加密解密[利用反射获取加密解密类】文章由骇客基地网上搜集,其立场行为并不代表本站。
* 如果您发现该文章若无意中侵犯到您的权利,请联系我们!
* 未经本站明确许可,任何网站不得非法盗链及抄袭本站资源;如引用页面,请注明来自本站,谢谢您的支持!
| |
|
| |
|
|
| |
|
|
| |
中国·黑客·骇客·基地 请使用IE6.0版本, 分辩率1024×768进行浏览 www.hookbase.com
站长:利客 Email:hookbase@163.com Copyright © 2004-2009 All Rights Reserved. 粤ICP备05000985号 |
|