目录
实践过程
效果
代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void Form_Load(object sender, EventArgs e)
{
FileMenu(Application.ExecutablePath + ",", Application.ExecutablePath);
string[] str = Environment.GetCommandLineArgs();
try
{
string strFile = "";
for (int i =; i < str.Length; i++)
strFile += str[i];
FileInfo FInfo = new FileInfo(strFile);
if (FInfo.Extension.ToLower() == ".mr")
{
textBox.Text = strFile;
button.Enabled = false;
button.Enabled = true;
}
}
catch
{
}
}
private void button_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox.Text = openFileDialog1.FileName;
FileInfo FInfo = new FileInfo(textBox.Text);
if (FInfo.Extension.ToLower() == ".mr")
{
button.Enabled = false;
button.Enabled = true;
}
else
{
button.Enabled = true;
button.Enabled = false;
}
}
}
private void button_Click(object sender, EventArgs e)
{
if (textBox.Text != "")
{
if (textBox.Text.Length < 6)
MessageBox.Show("密码不能小于位!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
EDncrypt myEDncrypt = new EDncrypt(textBox.Text, textBox2.Text, progressBar1);
myEDncrypt.StartEncrypt();
progressBar.Value = 0;
}
}
}
private void button_Click(object sender, EventArgs e)
{
if (textBox.Text != "")
{
if (textBox.Text.Length < 6)
MessageBox.Show("密码不能小于位!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
EDncrypt myEDncrypt = new EDncrypt(textBox.Text, textBox2.Text, progressBar1);
myEDncrypt.StartDncrypt();
progressBar.Value = 0;
}
}
}
public static void FileMenu(string strPath, string strName)
{
try
{
Registry.ClassesRoot.CreateSubKey(".mr");
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey(".mr", true);
RKey.SetValue("", "mrfile");
RKey.Close();
Registry.ClassesRoot.CreateSubKey("mrfile");
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrfile", true);
RKey.CreateSubKey("DefaultIcon");
RKey.CreateSubKey("shell");
RKey.Close();
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrfile\\DefaultIcon", true);
RKey.SetValue("", strPath);
RKey.Close();
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrfile\\shell", true);
RKey.CreateSubKey("解密");
RKey.Close();
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrfile\\shell\\解密", true);
RKey.CreateSubKey("command");
RKey.Close();
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrfile\\shell\\解密\\command", true);
RKey.SetValue("", strName + " \\F %1");
RKey.Close();
}
catch
{
}
}
}
#region 加密、解密类
class EDncrypt
{
#region 定义全局变量及类对象
private string strFile = "";
private string strNewFile = "";
private string strPwd = "";
private ProgressBar PBar = null;
private Thread EThread = null;
private Thread DThread = null;
#endregion
public EDncrypt(string name, string pwd, ProgressBar pb)
{
strFile = name;
strPwd = pwd;
PBar = pb;
EThread = new Thread(new ThreadStart(this.myEThread));
DThread = new Thread(new ThreadStart(this.myDThread));
}
private void myEThread()
{
byte[] btRKey = new byte[];
if (strPwd.Length ==)
{
btRKey = new byte[]
{
(byte) strPwd[], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[], (byte) strPwd[0], (byte) strPwd[1]
};
}
if (strPwd.Length ==)
{
btRKey = new byte[]
{
(byte) strPwd[], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[], (byte) strPwd[6], (byte) strPwd[0]
};
}
if (strPwd.Length >=)
{
btRKey = new byte[]
{
(byte) strPwd[], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[], (byte) strPwd[6], (byte) strPwd[7]
};
}
FileStream FStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
FileStream NewFStream = new FileStream(strFile + ".mr", FileMode.OpenOrCreate, FileAccess.Write);
NewFStream.SetLength((long));
byte[] buffer = new byte[x400];
int MinNum =;
long length = FStream.Length;
int MaxNum = (int) (length / ((long)x400));
PBar.Maximum = MaxNum;
DES myDES = new DESCryptoServiceProvider();
CryptoStream CStream =
new CryptoStream(NewFStream, myDES.CreateEncryptor(btRKey, btRKey), CryptoStreamMode.Write);
while (MinNum < length)
{
int count = FStream.Read(buffer,, 0x400);
CStream.Write(buffer,, count);
MinNum += count;
try
{
if (PBar.Value < PBar.Maximum)
{
PBar.Value++;
}
}
catch
{
}
}
CStream.Close();
NewFStream.Close();
FStream.Close();
File.Delete(strFile);
MessageBox.Show("文件加密成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public void StartEncrypt()
{
EThread.Start();
}
private void myDThread()
{
FileStream FStream = null;
FileStream NewFStream = null;
CryptoStream CStream = null;
try
{
try
{
byte[] btRKey = new byte[];
if (strPwd.Length ==)
{
btRKey = new byte[]
{
(byte) strPwd[], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[], (byte) strPwd[0], (byte) strPwd[1]
};
}
if (strPwd.Length ==)
{
btRKey = new byte[]
{
(byte) strPwd[], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[], (byte) strPwd[6], (byte) strPwd[0]
};
}
if (strPwd.Length >=)
{
btRKey = new byte[]
{
(byte) strPwd[], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[], (byte) strPwd[6], (byte) strPwd[7]
};
}
FStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
strNewFile = strFile.Substring(, strFile.Length - 3);
NewFStream = new FileStream(strNewFile, FileMode.OpenOrCreate, FileAccess.Write);
NewFStream.SetLength((long));
byte[] buffer = new byte[x400];
int MinNum =;
long length = FStream.Length;
int MaxNum = (int) (length / ((long)x400));
PBar.Maximum = MaxNum;
DES myDES = new DESCryptoServiceProvider();
CStream = new CryptoStream(NewFStream, myDES.CreateDecryptor(btRKey, btRKey),
CryptoStreamMode.Write);
while (MinNum < length)
{
int count = FStream.Read(buffer,, 0x400);
CStream.Write(buffer,, count);
MinNum += count;
try
{
if (PBar.Value < PBar.Maximum)
{
PBar.Value++;
}
}
catch
{
}
}
MessageBox.Show("文件解密成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("文件解密失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
finally
{
CStream.Close();
FStream.Close();
NewFStream.Close();
}
}
public void StartDncrypt()
{
DThread.Start();
}
}
#endregion
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.button = new System.Windows.Forms.Button();
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.progressBar = new System.Windows.Forms.ProgressBar();
this.groupBox = new System.Windows.Forms.GroupBox();
this.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.groupBox.SuspendLayout();
this.SuspendLayout();
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(6, 17);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(161, 12);
this.label.TabIndex = 0;
this.label.Text = "请选择要加密或解密的文件:";
this.textBox.Location = new System.Drawing.Point(32, 35);
this.textBox.Name = "textBox1";
this.textBox.Size = new System.Drawing.Size(263, 21);
this.textBox.TabIndex = 1;
this.button.Location = new System.Drawing.Point(301, 33);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(33, 23);
this.button.TabIndex = 2;
this.button.Text = "…";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(7, 71);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(83, 12);
this.label.TabIndex = 3;
this.label.Text = "加/解密密码:";
this.textBox.Location = new System.Drawing.Point(92, 68);
this.textBox.Name = "textBox2";
this.textBox.PasswordChar = '*';
this.textBox.Size = new System.Drawing.Size(151, 21);
this.textBox.TabIndex = 4;
this.label.AutoSize = true;
this.label.ForeColor = System.Drawing.Color.Red;
this.label.Location = new System.Drawing.Point(243, 71);
this.label.Name = "label3";
this.label.Size = new System.Drawing.Size(95, 12);
this.label.TabIndex = 5;
this.label.Text = "(密码应大于6位)";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(7, 101);
this.label.Name = "label4";
this.label.Size = new System.Drawing.Size(83, 12);
this.label.TabIndex = 6;
this.label.Text = "加/解密进度:";
this.progressBar.Location = new System.Drawing.Point(92, 99);
this.progressBar.Name = "progressBar1";
this.progressBar.Size = new System.Drawing.Size(242, 18);
this.progressBar.TabIndex = 7;
this.groupBox.Controls.Add(this.label1);
this.groupBox.Controls.Add(this.progressBar1);
this.groupBox.Controls.Add(this.textBox1);
this.groupBox.Controls.Add(this.label4);
this.groupBox.Controls.Add(this.button1);
this.groupBox.Controls.Add(this.label3);
this.groupBox.Controls.Add(this.label2);
this.groupBox.Controls.Add(this.textBox2);
this.groupBox.Location = new System.Drawing.Point(5, 5);
this.groupBox.Name = "groupBox1";
this.groupBox.Size = new System.Drawing.Size(342, 123);
this.groupBox.TabIndex = 8;
this.groupBox.TabStop = false;
this.groupBox.Text = "文件加/解密设置";
this.button.Enabled = false;
this.button.Location = new System.Drawing.Point(223, 134);
this.button.Name = "button2";
this.button.Size = new System.Drawing.Size(59, 27);
this.button.TabIndex = 9;
this.button.Text = "加密";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button2_Click);
this.button.Enabled = false;
this.button.Location = new System.Drawing.Point(288, 134);
this.button.Name = "button3";
this.button.Size = new System.Drawing.Size(59, 27);
this.button.TabIndex = 9;
this.button.Text = "解密";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button3_Click);
this.openFileDialog.FileName = "openFileDialog1";
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 167);
this.Controls.Add(this.button);
this.Controls.Add(this.button);
this.Controls.Add(this.groupBox);
this.MaximizeBox = false;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "对文件进行加密保护";
this.Load += new System.EventHandler(this.Form_Load);
this.groupBox.ResumeLayout(false);
this.groupBox.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.ProgressBar progressBar;
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button;
private System.Windows.Forms.OpenFileDialog openFileDialog;
}