目录
实践过程
效果
代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
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() == ".mrrar")
textBox.Text = strFile;
}
catch { }
}
private void button_Click(object sender, EventArgs e)
{
openFileDialog.Filter = "*.rar(rar压缩文件)|*.rar|*.mrrar(mrrar加密文件)|*.mrrar|*.*(所有文件)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
textBox.Text = openFileDialog1.FileName;
}
private void button_Click(object sender, EventArgs e)
{
string strPwd = textBox.Text;
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[5], (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[5], (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[5], (byte)strPwd[6], (byte)strPwd[7] };
}
FileStream FStream = new FileStream(textBox.Text, FileMode.Open, FileAccess.Read);
FileStream NewFStream = new FileStream(textBox.Text + ".mrrar", FileMode.OpenOrCreate, FileAccess.Write);
NewFStream.SetLength((long));
byte[] buffer = new byte[x400];
int MinNum =;
long length = FStream.Length;
int MaxNum = (int)(length / ((long)x400));
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;
}
CStream.Close();
NewFStream.Close();
FStream.Close();
File.Delete(textBox.Text);
MessageBox.Show("使用口令加密rar文件成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button_Click(object sender, EventArgs e)
{
string strPwd = textBox.Text;
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[5], (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[5], (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[5], (byte)strPwd[6], (byte)strPwd[7] };
}
FStream = new FileStream(textBox.Text, FileMode.Open, FileAccess.Read);
string strNewFile = textBox.Text.Substring(0, textBox1.Text.Length - 6);
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));
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;
}
CStream.Close();
FStream.Close();
NewFStream.Close();
File.Delete(textBox.Text);
System.Diagnostics.Process.Start(strNewFile);
}
catch
{
MessageBox.Show("口令错误!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox.Focus();
}
}
finally
{
CStream.Close();
FStream.Close();
NewFStream.Close();
}
}
public static void FileMenu(string strPath, string strName)
{
try
{
Registry.ClassesRoot.CreateSubKey(".mrrar");
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey(".mrrar", true);
RKey.SetValue("", "mrrarfile");
RKey.Close();
Registry.ClassesRoot.CreateSubKey("mrrarfile");
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrrarfile", true);
RKey.CreateSubKey("DefaultIcon");
RKey.CreateSubKey("shell");
RKey.Close();
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrrarfile\\DefaultIcon", true);
RKey.SetValue("", strPath);
RKey.Close();
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrrarfile\\shell", true);
RKey.CreateSubKey("使用口令打开");
RKey.Close();
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrrarfile\\shell\\使用口令打开", true);
RKey.CreateSubKey("command");
RKey.Close();
RegistryKey RKey = Registry.ClassesRoot.OpenSubKey("mrrarfile\\shell\\使用口令打开\\command", true);
RKey.SetValue("", strName + " \\F %1");
RKey.Close();
}
catch
{
}
}
}
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.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.groupBox = new System.Windows.Forms.GroupBox();
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.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.groupBox.SuspendLayout();
this.SuspendLayout();
this.button.Location = new System.Drawing.Point(250, 107);
this.button.Name = "button3";
this.button.Size = new System.Drawing.Size(59, 27);
this.button.TabIndex = 11;
this.button.Text = "打开";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button3_Click);
this.button.Location = new System.Drawing.Point(185, 107);
this.button.Name = "button2";
this.button.Size = new System.Drawing.Size(59, 27);
this.button.TabIndex = 12;
this.button.Text = "加密";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button2_Click);
this.groupBox.Controls.Add(this.label1);
this.groupBox.Controls.Add(this.textBox1);
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, 9);
this.groupBox.Name = "groupBox1";
this.groupBox.Size = new System.Drawing.Size(304, 92);
this.groupBox.TabIndex = 10;
this.groupBox.TabStop = false;
this.groupBox.Text = "rar文件加/解密设置";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(6, 17);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(95, 12);
this.label.TabIndex = 0;
this.label.Text = "请选择rar文件:";
this.textBox.Location = new System.Drawing.Point(32, 35);
this.textBox.Name = "textBox1";
this.textBox.Size = new System.Drawing.Size(225, 21);
this.textBox.TabIndex = 1;
this.button.Location = new System.Drawing.Point(263, 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.ForeColor = System.Drawing.Color.Red;
this.label.Location = new System.Drawing.Point(201, 66);
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, 66);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 3;
this.label.Text = "加密口令:";
this.textBox.Location = new System.Drawing.Point(73, 63);
this.textBox.Name = "textBox2";
this.textBox.PasswordChar = '*';
this.textBox.Size = new System.Drawing.Size(127, 21);
this.textBox.TabIndex = 4;
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(, 140);
this.Controls.Add(this.button);
this.Controls.Add(this.button);
this.Controls.Add(this.groupBox);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "用口令加密rar压缩文件";
this.Load += new System.EventHandler(this.Form_Load);
this.groupBox.ResumeLayout(false);
this.groupBox.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button;
private System.Windows.Forms.GroupBox groupBox;
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.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.OpenFileDialog openFileDialog;
}