使用口令加密可执行文件

 

 

Form1.cs

View Code
复制代码
  1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.IO;
10 using System.Security.Cryptography;
11 using Microsoft.Win32;
12
13 namespace EncryptExe
14 {
15 public partial class Form1 : Form
16 {
17 public Form1()
18 {
19 InitializeComponent();
20 }
21
22 private void Form1_Load(object sender, EventArgs e)
23 {
24 FileMenu(Application.ExecutablePath + ",0", Application.ExecutablePath);
25 string[] str = Environment.GetCommandLineArgs();
26 try
27 {
28 string strFile = "";
29 for (int i = 2; i < str.Length; i++)
30 strFile += str[i];
31 FileInfo FInfo = new FileInfo(strFile);
32 if (FInfo.Extension.ToLower() == ".mrexe")
33 textBox1.Text = strFile;
34 }
35 catch { }
36 }
37
38 //选择要加密或解密的文件
39 private void button1_Click(object sender, EventArgs e)
40 {
41 openFileDialog1.Filter = "*.exe(exe可执行文件)|*.exe|*.mrexe(mrexe加密文件)|*.mrexe|*.*(所有文件)|*.*";
42 if (openFileDialog1.ShowDialog() == DialogResult.OK)
43 textBox1.Text = openFileDialog1.FileName;
44 }
45
46 //加密EXE文件
47 private void button2_Click(object sender, EventArgs e)
48 {
49 string strPwd = textBox2.Text;
50 byte[] btRKey = new byte[0];
51 if (strPwd.Length == 6)
52 {
53 btRKey = new byte[] { (byte)strPwd[0], (byte)strPwd[1], (byte)strPwd[2], (byte)strPwd[3], (byte)strPwd[4], (byte)strPwd[5], (byte)strPwd[0], (byte)strPwd[1] };
54 }
55 if (strPwd.Length == 7)
56 {
57 btRKey = new byte[] { (byte)strPwd[0], (byte)strPwd[1], (byte)strPwd[2], (byte)strPwd[3], (byte)strPwd[4], (byte)strPwd[5], (byte)strPwd[6], (byte)strPwd[0] };
58 }
59 if (strPwd.Length >= 8)
60 {
61 btRKey = new byte[] { (byte)strPwd[0], (byte)strPwd[1], (byte)strPwd[2], (byte)strPwd[3], (byte)strPwd[4], (byte)strPwd[5], (byte)strPwd[6], (byte)strPwd[7] };
62 }
63 FileStream FStream = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
64 FileStream NewFStream = new FileStream(textBox1.Text + ".mrexe", FileMode.OpenOrCreate, FileAccess.Write);
65 NewFStream.SetLength((long)0);
66 byte[] buffer = new byte[0x400];
67 int MinNum = 0;
68 long length = FStream.Length;
69 int MaxNum = (int)(length / ((long)0x400));
70 DES myDES = new DESCryptoServiceProvider();
71 CryptoStream CStream = new CryptoStream(NewFStream, myDES.CreateEncryptor(btRKey, btRKey), CryptoStreamMode.Write);
72 while (MinNum < length)
73 {
74 int count = FStream.Read(buffer, 0, 0x400);
75 CStream.Write(buffer, 0, count);
76 MinNum += count;
77 }
78 CStream.Close();
79 NewFStream.Close();
80 FStream.Close();
81 File.Delete(textBox1.Text);
82 MessageBox.Show("使用口令加密可执行文件成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
83 }
84
85 //解密EXE文件
86 private void button3_Click(object sender, EventArgs e)
87 {
88 string strPwd = textBox2.Text;
89 FileStream FStream = null;
90 FileStream NewFStream = null;
91 CryptoStream CStream = null;
92 try
93 {
94 try
95 {
96 byte[] btRKey = new byte[0];
97 if (strPwd.Length == 6)
98 {
99 btRKey = new byte[] { (byte)strPwd[0], (byte)strPwd[1], (byte)strPwd[2], (byte)strPwd[3], (byte)strPwd[4], (byte)strPwd[5], (byte)strPwd[0], (byte)strPwd[1] };
100 }
101 if (strPwd.Length == 7)
102 {
103 btRKey = new byte[] { (byte)strPwd[0], (byte)strPwd[1], (byte)strPwd[2], (byte)strPwd[3], (byte)strPwd[4], (byte)strPwd[5], (byte)strPwd[6], (byte)strPwd[0] };
104 }
105 if (strPwd.Length >= 8)
106 {
107 btRKey = new byte[] { (byte)strPwd[0], (byte)strPwd[1], (byte)strPwd[2], (byte)strPwd[3], (byte)strPwd[4], (byte)strPwd[5], (byte)strPwd[6], (byte)strPwd[7] };
108 }
109 FStream = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
110 string strNewFile = textBox1.Text.Substring(0, textBox1.Text.Length - 6);
111 NewFStream = new FileStream(strNewFile, FileMode.OpenOrCreate, FileAccess.Write);
112 NewFStream.SetLength((long)0);
113 byte[] buffer = new byte[0x400];
114 int MinNum = 0;
115 long length = FStream.Length;
116 int MaxNum = (int)(length / ((long)0x400));
117 DES myDES = new DESCryptoServiceProvider();
118 CStream = new CryptoStream(NewFStream, myDES.CreateDecryptor(btRKey, btRKey), CryptoStreamMode.Write);
119 while (MinNum < length)
120 {
121 int count = FStream.Read(buffer, 0, 0x400);
122 CStream.Write(buffer, 0, count);
123 MinNum += count;
124 }
125 CStream.Close();
126 FStream.Close();
127 NewFStream.Close();
128 File.Delete(textBox1.Text);
129 System.Diagnostics.Process.Start(strNewFile);
130 }
131 catch
132 {
133 MessageBox.Show("口令错误!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
134 textBox2.Focus();
135 }
136 }
137 finally
138 {
139 CStream.Close();
140 FStream.Close();
141 NewFStream.Close();
142 }
143 }
144
145 //创建快捷菜单
146 public static void FileMenu(string strPath, string strName)
147 {
148 try
149 {
150 Registry.ClassesRoot.CreateSubKey(".mrexe");
151 RegistryKey RKey1 = Registry.ClassesRoot.OpenSubKey(".mrexe", true);
152 RKey1.SetValue("", "mrexefile");
153 RKey1.Close();
154 Registry.ClassesRoot.CreateSubKey("mrexefile");
155 RegistryKey RKey2 = Registry.ClassesRoot.OpenSubKey("mrexefile", true);
156 RKey2.CreateSubKey("DefaultIcon");
157 RKey2.CreateSubKey("shell");
158 RKey2.Close();
159 RegistryKey RKey3 = Registry.ClassesRoot.OpenSubKey("mrexefile\\DefaultIcon", true);
160 RKey3.SetValue("", strPath);
161 RKey3.Close();
162 RegistryKey RKey4 = Registry.ClassesRoot.OpenSubKey("mrexefile\\shell", true);
163 RKey4.CreateSubKey("使用口令打开");
164 RKey4.Close();
165 RegistryKey RKey5 = Registry.ClassesRoot.OpenSubKey("mrexefile\\shell\\使用口令打开", true);
166 RKey5.CreateSubKey("command");
167 RKey5.Close();
168 RegistryKey RKey6 = Registry.ClassesRoot.OpenSubKey("mrexefile\\shell\\使用口令打开\\command", true);
169 RKey6.SetValue("", strName + " \\F %1");
170 RKey6.Close();
171 }
172 catch
173 {
174 }
175 }
176 }
177 }
复制代码

Form1.Designer.cs

View Code
  1 namespace EncryptExe
2 {
3 partial class Form1
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows 窗体设计器生成的代码
24
25 /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.button3 = new System.Windows.Forms.Button();
32 this.button2 = new System.Windows.Forms.Button();
33 this.groupBox1 = new System.Windows.Forms.GroupBox();
34 this.label1 = new System.Windows.Forms.Label();
35 this.textBox1 = new System.Windows.Forms.TextBox();
36 this.button1 = new System.Windows.Forms.Button();
37 this.label3 = new System.Windows.Forms.Label();
38 this.label2 = new System.Windows.Forms.Label();
39 this.textBox2 = new System.Windows.Forms.TextBox();
40 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
41 this.groupBox1.SuspendLayout();
42 this.SuspendLayout();
43 //
44 // button3
45 //
46 this.button3.Location = new System.Drawing.Point(250, 107);
47 this.button3.Name = "button3";
48 this.button3.Size = new System.Drawing.Size(59, 27);
49 this.button3.TabIndex = 11;
50 this.button3.Text = "打开";
51 this.button3.UseVisualStyleBackColor = true;
52 this.button3.Click += new System.EventHandler(this.button3_Click);
53 //
54 // button2
55 //
56 this.button2.Location = new System.Drawing.Point(185, 107);
57 this.button2.Name = "button2";
58 this.button2.Size = new System.Drawing.Size(59, 27);
59 this.button2.TabIndex = 12;
60 this.button2.Text = "加密";
61 this.button2.UseVisualStyleBackColor = true;
62 this.button2.Click += new System.EventHandler(this.button2_Click);
63 //
64 // groupBox1
65 //
66 this.groupBox1.Controls.Add(this.label1);
67 this.groupBox1.Controls.Add(this.textBox1);
68 this.groupBox1.Controls.Add(this.button1);
69 this.groupBox1.Controls.Add(this.label3);
70 this.groupBox1.Controls.Add(this.label2);
71 this.groupBox1.Controls.Add(this.textBox2);
72 this.groupBox1.Location = new System.Drawing.Point(5, 9);
73 this.groupBox1.Name = "groupBox1";
74 this.groupBox1.Size = new System.Drawing.Size(304, 92);
75 this.groupBox1.TabIndex = 10;
76 this.groupBox1.TabStop = false;
77 this.groupBox1.Text = "Exe文件加/解密设置";
78 //
79 // label1
80 //
81 this.label1.AutoSize = true;
82 this.label1.Location = new System.Drawing.Point(6, 17);
83 this.label1.Name = "label1";
84 this.label1.Size = new System.Drawing.Size(95, 12);
85 this.label1.TabIndex = 0;
86 this.label1.Text = "请选择Exe文件:";
87 //
88 // textBox1
89 //
90 this.textBox1.Location = new System.Drawing.Point(32, 35);
91 this.textBox1.Name = "textBox1";
92 this.textBox1.Size = new System.Drawing.Size(225, 21);
93 this.textBox1.TabIndex = 1;
94 //
95 // button1
96 //
97 this.button1.Location = new System.Drawing.Point(263, 33);
98 this.button1.Name = "button1";
99 this.button1.Size = new System.Drawing.Size(33, 23);
100 this.button1.TabIndex = 2;
101 this.button1.Text = "";
102 this.button1.UseVisualStyleBackColor = true;
103 this.button1.Click += new System.EventHandler(this.button1_Click);
104 //
105 // label3
106 //
107 this.label3.AutoSize = true;
108 this.label3.ForeColor = System.Drawing.Color.Red;
109 this.label3.Location = new System.Drawing.Point(201, 66);
110 this.label3.Name = "label3";
111 this.label3.Size = new System.Drawing.Size(95, 12);
112 this.label3.TabIndex = 5;
113 this.label3.Text = "(密码应大于6位)";
114 //
115 // label2
116 //
117 this.label2.AutoSize = true;
118 this.label2.Location = new System.Drawing.Point(7, 66);
119 this.label2.Name = "label2";
120 this.label2.Size = new System.Drawing.Size(65, 12);
121 this.label2.TabIndex = 3;
122 this.label2.Text = "加密口令:";
123 //
124 // textBox2
125 //
126 this.textBox2.Location = new System.Drawing.Point(73, 63);
127 this.textBox2.Name = "textBox2";
128 this.textBox2.PasswordChar = '*';
129 this.textBox2.Size = new System.Drawing.Size(127, 21);
130 this.textBox2.TabIndex = 4;
131 //
132 // Form1
133 //
134 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
135 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
136 this.ClientSize = new System.Drawing.Size(316, 140);
137 this.Controls.Add(this.button3);
138 this.Controls.Add(this.button2);
139 this.Controls.Add(this.groupBox1);
140 this.MaximizeBox = false;
141 this.MinimizeBox = false;
142 this.Name = "Form1";
143 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
144 this.Text = "使用口令加密可执行文件";
145 this.Load += new System.EventHandler(this.Form1_Load);
146 this.groupBox1.ResumeLayout(false);
147 this.groupBox1.PerformLayout();
148 this.ResumeLayout(false);
149
150 }
151
152 #endregion
153
154 private System.Windows.Forms.Button button3;
155 private System.Windows.Forms.Button button2;
156 private System.Windows.Forms.GroupBox groupBox1;
157 private System.Windows.Forms.Label label1;
158 private System.Windows.Forms.TextBox textBox1;
159 private System.Windows.Forms.Button button1;
160 private System.Windows.Forms.Label label3;
161 private System.Windows.Forms.Label label2;
162 private System.Windows.Forms.TextBox textBox2;
163 private System.Windows.Forms.OpenFileDialog openFileDialog1;
164 }
165 }




作者: 墨明棋妙
出处: http://www.cnblogs.com/ynbt/
关于作者:专注于.Net、WCF和移动互联网开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过ynbt_wang@163.com联系我,非常感谢。 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值