📄 passworddes.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace Full
{
/// <summary>
/// PasswordDES 的摘要说明。
/// </summary>
public class PasswordDES : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtAddPassword;
private System.Windows.Forms.TextBox txtAddPasswordOut;
private System.Windows.Forms.Button btnBrower1;
private System.Windows.Forms.Button btnAddPassword;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.TextBox txtPasswordResect;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TextBox txtPasswordIn;
private System.Windows.Forms.Button btnPassword;
private System.Windows.Forms.Button btnBrower2;
private System.Windows.Forms.TextBox txtPasswordOutFile;
private System.Windows.Forms.TextBox txtAddPasswordFile;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public PasswordDES()
{
//
// 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.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtAddPassword = new System.Windows.Forms.TextBox();
this.txtAddPasswordOut = new System.Windows.Forms.TextBox();
this.btnBrower1 = new System.Windows.Forms.Button();
this.btnAddPassword = new System.Windows.Forms.Button();
this.txtPassword = new System.Windows.Forms.TextBox();
this.txtPasswordResect = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtPasswordIn = new System.Windows.Forms.TextBox();
this.btnPassword = new System.Windows.Forms.Button();
this.btnBrower2 = new System.Windows.Forms.Button();
this.txtPasswordOutFile = new System.Windows.Forms.TextBox();
this.txtAddPasswordFile = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.txtPasswordResect);
this.groupBox1.Controls.Add(this.txtPassword);
this.groupBox1.Controls.Add(this.btnAddPassword);
this.groupBox1.Controls.Add(this.btnBrower1);
this.groupBox1.Controls.Add(this.txtAddPasswordOut);
this.groupBox1.Controls.Add(this.txtAddPassword);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(344, 144);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "加密设置";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "被加密文件名";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 48);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "加密输出文件";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 80);
this.label3.Name = "label3";
this.label3.TabIndex = 2;
this.label3.Text = "设密大于6位";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 112);
this.label4.Name = "label4";
this.label4.TabIndex = 3;
this.label4.Text = "重设密码";
//
// txtAddPassword
//
this.txtAddPassword.Location = new System.Drawing.Point(112, 16);
this.txtAddPassword.Name = "txtAddPassword";
this.txtAddPassword.Size = new System.Drawing.Size(224, 21);
this.txtAddPassword.TabIndex = 4;
this.txtAddPassword.Text = "textBox1";
//
// txtAddPasswordOut
//
this.txtAddPasswordOut.Location = new System.Drawing.Point(112, 48);
this.txtAddPasswordOut.Name = "txtAddPasswordOut";
this.txtAddPasswordOut.Size = new System.Drawing.Size(224, 21);
this.txtAddPasswordOut.TabIndex = 5;
this.txtAddPasswordOut.Text = "textBox2";
//
// btnBrower1
//
this.btnBrower1.Location = new System.Drawing.Point(256, 80);
this.btnBrower1.Name = "btnBrower1";
this.btnBrower1.TabIndex = 6;
this.btnBrower1.Text = "浏览文件";
this.btnBrower1.Click += new System.EventHandler(this.btnBrower1_Click);
//
// btnAddPassword
//
this.btnAddPassword.Location = new System.Drawing.Point(256, 112);
this.btnAddPassword.Name = "btnAddPassword";
this.btnAddPassword.TabIndex = 7;
this.btnAddPassword.Text = "加密文件";
this.btnAddPassword.Click += new System.EventHandler(this.btnAddPassword_Click);
//
// txtPassword
//
this.txtPassword.Location = new System.Drawing.Point(112, 80);
this.txtPassword.Name = "txtPassword";
this.txtPassword.PasswordChar = '*';
this.txtPassword.TabIndex = 8;
this.txtPassword.Text = "textBox3";
//
// txtPasswordResect
//
this.txtPasswordResect.Location = new System.Drawing.Point(112, 112);
this.txtPasswordResect.Name = "txtPasswordResect";
this.txtPasswordResect.PasswordChar = '*';
this.txtPasswordResect.TabIndex = 9;
this.txtPasswordResect.Text = "textBox4";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txtPasswordIn);
this.groupBox2.Controls.Add(this.btnPassword);
this.groupBox2.Controls.Add(this.btnBrower2);
this.groupBox2.Controls.Add(this.txtPasswordOutFile);
this.groupBox2.Controls.Add(this.txtAddPasswordFile);
this.groupBox2.Controls.Add(this.label5);
this.groupBox2.Controls.Add(this.label7);
this.groupBox2.Controls.Add(this.label8);
this.groupBox2.Location = new System.Drawing.Point(8, 168);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(344, 144);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "解密设置";
//
// txtPasswordIn
//
this.txtPasswordIn.Location = new System.Drawing.Point(112, 80);
this.txtPasswordIn.Name = "txtPasswordIn";
this.txtPasswordIn.PasswordChar = '*';
this.txtPasswordIn.TabIndex = 9;
this.txtPasswordIn.Text = "textBox4";
//
// btnPassword
//
this.btnPassword.Location = new System.Drawing.Point(256, 112);
this.btnPassword.Name = "btnPassword";
this.btnPassword.TabIndex = 7;
this.btnPassword.Text = "解密文件";
this.btnPassword.Click += new System.EventHandler(this.btnPassword_Click);
//
// btnBrower2
//
this.btnBrower2.Location = new System.Drawing.Point(256, 80);
this.btnBrower2.Name = "btnBrower2";
this.btnBrower2.TabIndex = 6;
this.btnBrower2.Text = "浏览文件";
this.btnBrower2.Click += new System.EventHandler(this.btnBrower2_Click);
//
// txtPasswordOutFile
//
this.txtPasswordOutFile.Location = new System.Drawing.Point(112, 48);
this.txtPasswordOutFile.Name = "txtPasswordOutFile";
this.txtPasswordOutFile.Size = new System.Drawing.Size(224, 21);
this.txtPasswordOutFile.TabIndex = 5;
this.txtPasswordOutFile.Text = "textBox2";
//
// txtAddPasswordFile
//
this.txtAddPasswordFile.Location = new System.Drawing.Point(112, 16);
this.txtAddPasswordFile.Name = "txtAddPasswordFile";
this.txtAddPasswordFile.Size = new System.Drawing.Size(224, 21);
this.txtAddPasswordFile.TabIndex = 4;
this.txtAddPasswordFile.Text = "textBox1";
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 80);
this.label5.Name = "label5";
this.label5.TabIndex = 3;
this.label5.Text = "输入密码";
//
// label7
//
this.label7.Location = new System.Drawing.Point(8, 48);
this.label7.Name = "label7";
this.label7.TabIndex = 1;
this.label7.Text = "解密输出文件";
//
// label8
//
this.label8.Location = new System.Drawing.Point(8, 16);
this.label8.Name = "label8";
this.label8.TabIndex = 0;
this.label8.Text = "被加密文件名";
//
// PasswordDES
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(360, 317);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "PasswordDES";
this.Text = "PasswordDES";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
//打开文件
private void btnBrower1_Click(object sender, System.EventArgs e)
{
if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.txtAddPassword.Text = this.openFileDialog1.FileName;
}
}
//加密文件
private void btnAddPassword_Click(object sender, System.EventArgs e)
{
if(this.txtAddPassword.Text == "")
{
MessageBox.Show("请选择被加密文件!");
return;
}
if(this.txtAddPasswordOut.Text == "")
{
MessageBox.Show("请选择加密输出文件!");
return;
}
if(this.txtPassword.Text.Length < 6 | this.txtPassword.Text != this.txtPasswordResect.Text)
{
MessageBox.Show("请输入正确密码文件!");
return;
}
string myInFileName = this.txtAddPassword.Text;
string myOutFileName = this.txtAddPasswordOut.Text;
string myKeyString = this.txtPassword.Text;
byte[] myDESIV = {0 * 01,0 * 02,0 * 03,0 * 04,0 * 05,0 * 06,0 * 07,0 * 08};
byte[] myDESKey = {};
if(myKeyString.Length == 6)
{
myDESKey = new byte[] {(byte)myKeyString[0],(byte)myKeyString[1],(byte)myKeyString[2],(byte)myKeyString[3],(byte)myKeyString[4],(byte)myKeyString[5],0 * 07,0 * 08};
}
if(myKeyString.Length == 7)
{
myDESKey = new byte[] {(byte)myKeyString[0],(byte)myKeyString[1],(byte)myKeyString[2],(byte)myKeyString[3],(byte)myKeyString[4],(byte)myKeyString[5],(byte)myKeyString[6],0 * 07};
}
if(myKeyString.Length >= 8)
{
myDESKey = new byte[] {(byte)myKeyString[0],(byte)myKeyString[1],(byte)myKeyString[2],(byte)myKeyString[3],(byte)myKeyString[4],(byte)myKeyString[5],(byte)myKeyString[6],(byte)myKeyString[7]};
}
FileStream myInFileStream = new FileStream(myInFileName,FileMode.Open,FileAccess.Read);
FileStream myOutFileStream = new FileStream(myOutFileName,FileMode.OpenOrCreate,FileAccess.Write);
myOutFileStream.SetLength(0);
byte[] insertData = new byte[100];
int completedLength = 0;
long inFileSize = myInFileStream.Length;
DES myDES = new DESCryptoServiceProvider();
CryptoStream encryptStream = new CryptoStream(myOutFileStream,myDES.CreateEncryptor(myDESKey,myDESIV),CryptoStreamMode.Write);
while(completedLength < inFileSize)
{
int length = myInFileStream.Read(insertData,0,100);
encryptStream.Write(insertData,0,length);
completedLength = completedLength + length;
}
encryptStream.Close();
myOutFileStream.Close();
myInFileStream.Close();
MessageBox.Show("加密成功");
}
private void btnBrower2_Click(object sender, System.EventArgs e)
{
if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.txtAddPasswordFile.Text = this.openFileDialog1.FileName;
}
}
private void btnPassword_Click(object sender, System.EventArgs e)
{
if(this.txtAddPasswordFile.Text == "")
{
MessageBox.Show("请选择被解密文件!");
return;
}
if(this.txtPasswordOutFile.Text == "")
{
MessageBox.Show("请选择解密输出文件!");
return;
}
if(this.txtPasswordIn.Text.Length < 6)
{
MessageBox.Show("请输入正确密码文件!");
return;
}
string myInFileName = this.txtAddPasswordFile.Text;
string myOutFileName = this.txtPasswordOutFile.Text;
string myKeyString = this.txtPasswordIn.Text;
byte[] myDESIV = {0 * 01,0 * 02,0 * 03,0 * 04,0 * 05,0 * 06,0 * 07,0 * 08};
byte[] myDESKey = {};
if(myKeyString.Length == 6)
{
myDESKey = new byte[] {(byte)myKeyString[0],(byte)myKeyString[1],(byte)myKeyString[2],(byte)myKeyString[3],(byte)myKeyString[4],(byte)myKeyString[5],0 * 07,0 * 08};
}
if(myKeyString.Length == 7)
{
myDESKey = new byte[] {(byte)myKeyString[0],(byte)myKeyString[1],(byte)myKeyString[2],(byte)myKeyString[3],(byte)myKeyString[4],(byte)myKeyString[5],(byte)myKeyString[6],0 * 07};
}
if(myKeyString.Length >= 8)
{
myDESKey = new byte[] {(byte)myKeyString[0],(byte)myKeyString[1],(byte)myKeyString[2],(byte)myKeyString[3],(byte)myKeyString[4],(byte)myKeyString[5],(byte)myKeyString[6],(byte)myKeyString[7]};
}
FileStream myInFileStream = new FileStream(myInFileName,FileMode.Open,FileAccess.Read);
FileStream myOutFileStream = new FileStream(myOutFileName,FileMode.OpenOrCreate,FileAccess.Write);
myOutFileStream.SetLength(0);
byte[] insertData = new byte[100];
int completedLength = 0;
long inFileSize = myInFileStream.Length;
DES myDES = new DESCryptoServiceProvider();
CryptoStream encryptStream = new CryptoStream(myOutFileStream,myDES.CreateDecryptor(myDESKey,myDESIV),CryptoStreamMode.Write);
while(completedLength < inFileSize)
{
int length = myInFileStream.Read(insertData,0,100);
encryptStream.Write(insertData,0,length);
completedLength = completedLength + length;
}
encryptStream.Close();
myOutFileStream.Close();
myInFileStream.Close();
MessageBox.Show("加密成功");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -