网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > 网络攻防 > 安全文摘 > C#简单加密解密  

C#简单加密解密

2005-11-29  作者:BitsCN整理  来源:中国网管联盟  点评 投稿 收藏

接口类:
using System;

namespace OutInterface
{
/// <summary>
/// Class1 的摘要说明。
/// </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) 中国网管联盟bitsCN.com
{
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); 网管bitscn_com
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; 网管网www.bitscn.com
}
}
}


/// <summary>
/// 传出加密解密实现类实例
/// </summary>
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; 网管联盟bitsCN@com

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;

中国网管联盟bitsCN.com


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(); 中国网管联盟bitsCN.com
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();
网管u家u.bitscn@com

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); 网管u家u.bitscn@com
//
// 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 = "文件地址:"; 中国网管论坛bbs.bitsCN.com
//
// 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"; 网管联盟bitsCN_com
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); 中国网管联盟bitsCN.com
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";

网管联盟bitsCN_com


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)));

网管u家u.bitscn@com


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);

网管论坛bbs_bitsCN_com


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); 网管网www_bitscn_com
}
}

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; 网管u家u.bitscn@com
}

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],""));
}


}
}

 上一篇:从内存中加载并启动一个exe   下一篇:对抗杀毒软件的内存扫描
C#简单加密解密 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: