Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 18k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Security.Cryptography;
  8. using System.IO;
  9. namespace ch3_11
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.GroupBox groupBox1;
  17. private System.Windows.Forms.TextBox textBox1;
  18. private System.Windows.Forms.Button button1;
  19. private System.Windows.Forms.Label label1;
  20. private System.Windows.Forms.Label label2;
  21. private System.Windows.Forms.Button button2;
  22. private System.Windows.Forms.Label label3;
  23. private System.Windows.Forms.Button button3;
  24. private System.Windows.Forms.GroupBox groupBox2;
  25. private System.Windows.Forms.Button button4;
  26. private System.Windows.Forms.Label label4;
  27. private System.Windows.Forms.TextBox textBox5;
  28. private System.Windows.Forms.Button button5;
  29. private System.Windows.Forms.TextBox textBox4;
  30. private System.Windows.Forms.TextBox textBox3;
  31. private System.Windows.Forms.Label label5;
  32. private System.Windows.Forms.TextBox textBox2;
  33. private System.Windows.Forms.Button button6;
  34. private System.Windows.Forms.Label label6;
  35. private System.Windows.Forms.TextBox textBox7;
  36. private System.Windows.Forms.Button button7;
  37. private System.Windows.Forms.TextBox textBox6;
  38. private System.Windows.Forms.Label label7;
  39. private System.Windows.Forms.Label label8;
  40. /// <summary>
  41. /// 必需的设计器变量。
  42. /// </summary>
  43. private System.ComponentModel.Container components = null;
  44. public Form1()
  45. {
  46. //
  47. // Windows 窗体设计器支持所必需的
  48. //
  49. InitializeComponent();
  50. //
  51. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  52. //
  53. }
  54. /// <summary>
  55. /// 清理所有正在使用的资源。
  56. /// </summary>
  57. protected override void Dispose( bool disposing )
  58. {
  59. if( disposing )
  60. {
  61. if (components != null) 
  62. {
  63. components.Dispose();
  64. }
  65. }
  66. base.Dispose( disposing );
  67. }
  68. #region Windows Form Designer generated code
  69. /// <summary>
  70. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  71. /// 此方法的内容。
  72. /// </summary>
  73. private void InitializeComponent()
  74. {
  75. this.groupBox1 = new System.Windows.Forms.GroupBox();
  76. this.label3 = new System.Windows.Forms.Label();
  77. this.button2 = new System.Windows.Forms.Button();
  78. this.textBox4 = new System.Windows.Forms.TextBox();
  79. this.label2 = new System.Windows.Forms.Label();
  80. this.label1 = new System.Windows.Forms.Label();
  81. this.textBox3 = new System.Windows.Forms.TextBox();
  82. this.button1 = new System.Windows.Forms.Button();
  83. this.textBox1 = new System.Windows.Forms.TextBox();
  84. this.button3 = new System.Windows.Forms.Button();
  85. this.groupBox2 = new System.Windows.Forms.GroupBox();
  86. this.button4 = new System.Windows.Forms.Button();
  87. this.label4 = new System.Windows.Forms.Label();
  88. this.textBox5 = new System.Windows.Forms.TextBox();
  89. this.button5 = new System.Windows.Forms.Button();
  90. this.label5 = new System.Windows.Forms.Label();
  91. this.textBox2 = new System.Windows.Forms.TextBox();
  92. this.button6 = new System.Windows.Forms.Button();
  93. this.textBox7 = new System.Windows.Forms.TextBox();
  94. this.label6 = new System.Windows.Forms.Label();
  95. this.button7 = new System.Windows.Forms.Button();
  96. this.textBox6 = new System.Windows.Forms.TextBox();
  97. this.label7 = new System.Windows.Forms.Label();
  98. this.label8 = new System.Windows.Forms.Label();
  99. this.groupBox1.SuspendLayout();
  100. this.groupBox2.SuspendLayout();
  101. this.SuspendLayout();
  102. // 
  103. // groupBox1
  104. // 
  105. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  106. this.label8,
  107. this.button6,
  108. this.textBox2,
  109. this.label5,
  110. this.button3,
  111. this.label3,
  112. this.button2,
  113. this.textBox4,
  114. this.label2,
  115. this.label1,
  116. this.textBox3,
  117. this.button1,
  118. this.textBox1});
  119. this.groupBox1.Location = new System.Drawing.Point(24, 16);
  120. this.groupBox1.Name = "groupBox1";
  121. this.groupBox1.Size = new System.Drawing.Size(392, 248);
  122. this.groupBox1.TabIndex = 0;
  123. this.groupBox1.TabStop = false;
  124. this.groupBox1.Text = "文件加密";
  125. // 
  126. // label3
  127. // 
  128. this.label3.Location = new System.Drawing.Point(24, 36);
  129. this.label3.Name = "label3";
  130. this.label3.Size = new System.Drawing.Size(88, 16);
  131. this.label3.TabIndex = 7;
  132. this.label3.Text = "选择加密文件";
  133. // 
  134. // button2
  135. // 
  136. this.button2.Location = new System.Drawing.Point(280, 172);
  137. this.button2.Name = "button2";
  138. this.button2.TabIndex = 6;
  139. this.button2.Text = "确定";
  140. this.button2.Click += new System.EventHandler(this.button2_Click);
  141. // 
  142. // textBox4
  143. // 
  144. this.textBox4.Location = new System.Drawing.Point(144, 172);
  145. this.textBox4.Name = "textBox4";
  146. this.textBox4.PasswordChar = '*';
  147. this.textBox4.Size = new System.Drawing.Size(120, 21);
  148. this.textBox4.TabIndex = 5;
  149. this.textBox4.Text = "";
  150. // 
  151. // label2
  152. // 
  153. this.label2.Location = new System.Drawing.Point(24, 176);
  154. this.label2.Name = "label2";
  155. this.label2.Size = new System.Drawing.Size(100, 16);
  156. this.label2.TabIndex = 4;
  157. this.label2.Text = "确认密码";
  158. // 
  159. // label1
  160. // 
  161. this.label1.Location = new System.Drawing.Point(24, 136);
  162. this.label1.Name = "label1";
  163. this.label1.Size = new System.Drawing.Size(100, 16);
  164. this.label1.TabIndex = 3;
  165. this.label1.Text = "设定密码";
  166. // 
  167. // textBox3
  168. // 
  169. this.textBox3.Location = new System.Drawing.Point(144, 128);
  170. this.textBox3.Name = "textBox3";
  171. this.textBox3.PasswordChar = '*';
  172. this.textBox3.Size = new System.Drawing.Size(120, 21);
  173. this.textBox3.TabIndex = 2;
  174. this.textBox3.Text = "";
  175. // 
  176. // button1
  177. // 
  178. this.button1.Location = new System.Drawing.Point(280, 32);
  179. this.button1.Name = "button1";
  180. this.button1.TabIndex = 1;
  181. this.button1.Text = "浏览...";
  182. this.button1.Click += new System.EventHandler(this.button1_Click);
  183. // 
  184. // textBox1
  185. // 
  186. this.textBox1.Location = new System.Drawing.Point(144, 32);
  187. this.textBox1.Name = "textBox1";
  188. this.textBox1.Size = new System.Drawing.Size(120, 21);
  189. this.textBox1.TabIndex = 0;
  190. this.textBox1.Text = "";
  191. // 
  192. // button3
  193. // 
  194. this.button3.Enabled = false;
  195. this.button3.Location = new System.Drawing.Point(24, 216);
  196. this.button3.Name = "button3";
  197. this.button3.TabIndex = 8;
  198. this.button3.Text = "开始加密";
  199. this.button3.Click += new System.EventHandler(this.button3_Click);
  200. // 
  201. // groupBox2
  202. // 
  203. this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
  204. this.button7,
  205. this.textBox6,
  206. this.label7,
  207. this.label6,
  208. this.textBox7,
  209. this.button5,
  210. this.label4,
  211. this.button4,
  212. this.textBox5});
  213. this.groupBox2.Location = new System.Drawing.Point(24, 288);
  214. this.groupBox2.Name = "groupBox2";
  215. this.groupBox2.Size = new System.Drawing.Size(392, 160);
  216. this.groupBox2.TabIndex = 1;
  217. this.groupBox2.TabStop = false;
  218. this.groupBox2.Text = "文件解密";
  219. // 
  220. // button4
  221. // 
  222. this.button4.Location = new System.Drawing.Point(280, 32);
  223. this.button4.Name = "button4";
  224. this.button4.TabIndex = 9;
  225. this.button4.Text = "浏览...";
  226. this.button4.Click += new System.EventHandler(this.button4_Click);
  227. // 
  228. // label4
  229. // 
  230. this.label4.Location = new System.Drawing.Point(32, 36);
  231. this.label4.Name = "label4";
  232. this.label4.Size = new System.Drawing.Size(88, 16);
  233. this.label4.TabIndex = 11;
  234. this.label4.Text = "选择解密文件";
  235. // 
  236. // textBox5
  237. // 
  238. this.textBox5.Location = new System.Drawing.Point(144, 32);
  239. this.textBox5.Name = "textBox5";
  240. this.textBox5.Size = new System.Drawing.Size(120, 21);
  241. this.textBox5.TabIndex = 8;
  242. this.textBox5.Text = "";
  243. // 
  244. // button5
  245. // 
  246. this.button5.Location = new System.Drawing.Point(280, 110);
  247. this.button5.Name = "button5";
  248. this.button5.TabIndex = 12;
  249. this.button5.Text = "开始解密";
  250. this.button5.Click += new System.EventHandler(this.button5_Click);
  251. // 
  252. // label5
  253. // 
  254. this.label5.Location = new System.Drawing.Point(24, 72);
  255. this.label5.Name = "label5";
  256. this.label5.Size = new System.Drawing.Size(88, 23);
  257. this.label5.TabIndex = 9;
  258. this.label5.Text = "输出文件名";
  259. // 
  260. // textBox2
  261. // 
  262. this.textBox2.Location = new System.Drawing.Point(144, 72);
  263. this.textBox2.Name = "textBox2";
  264. this.textBox2.Size = new System.Drawing.Size(120, 21);
  265. this.textBox2.TabIndex = 10;
  266. this.textBox2.Text = "";
  267. // 
  268. // button6
  269. // 
  270. this.button6.Location = new System.Drawing.Point(280, 72);
  271. this.button6.Name = "button6";
  272. this.button6.TabIndex = 11;
  273. this.button6.Text = "浏览...";
  274. this.button6.Click += new System.EventHandler(this.button6_Click);
  275. // 
  276. // textBox7
  277. // 
  278. this.textBox7.Location = new System.Drawing.Point(144, 112);
  279. this.textBox7.Name = "textBox7";
  280. this.textBox7.PasswordChar = '*';
  281. this.textBox7.Size = new System.Drawing.Size(120, 21);
  282. this.textBox7.TabIndex = 13;
  283. this.textBox7.Text = "";
  284. // 
  285. // label6
  286. // 
  287. this.label6.Location = new System.Drawing.Point(32, 112);
  288. this.label6.Name = "label6";
  289. this.label6.Size = new System.Drawing.Size(80, 23);
  290. this.label6.TabIndex = 14;
  291. this.label6.Text = "输入密码";
  292. // 
  293. // button7
  294. // 
  295. this.button7.Location = new System.Drawing.Point(280, 68);
  296. this.button7.Name = "button7";
  297. this.button7.TabIndex = 17;
  298. this.button7.Text = "浏览...";
  299. this.button7.Click += new System.EventHandler(this.button7_Click);
  300. // 
  301. // textBox6
  302. // 
  303. this.textBox6.Location = new System.Drawing.Point(144, 72);
  304. this.textBox6.Name = "textBox6";
  305. this.textBox6.Size = new System.Drawing.Size(120, 21);
  306. this.textBox6.TabIndex = 16;
  307. this.textBox6.Text = "";
  308. // 
  309. // label7
  310. // 
  311. this.label7.Location = new System.Drawing.Point(32, 72);
  312. this.label7.Name = "label7";
  313. this.label7.Size = new System.Drawing.Size(88, 23);
  314. this.label7.TabIndex = 15;
  315. this.label7.Text = "输出文件名";
  316. // 
  317. // label8
  318. // 
  319. this.label8.ForeColor = System.Drawing.Color.Red;
  320. this.label8.Location = new System.Drawing.Point(24, 104);
  321. this.label8.Name = "label8";
  322. this.label8.Size = new System.Drawing.Size(168, 23);
  323. this.label8.TabIndex = 12;
  324. this.label8.Text = "请注意:密码必须大于6位";
  325. // 
  326. // Form1
  327. // 
  328. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  329. this.ClientSize = new System.Drawing.Size(440, 469);
  330. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  331.   this.groupBox2,
  332.   this.groupBox1});
  333. this.Name = "Form1";
  334. this.Text = "文件的加密解密";
  335. this.groupBox1.ResumeLayout(false);
  336. this.groupBox2.ResumeLayout(false);
  337. this.ResumeLayout(false);
  338. }
  339. #endregion
  340. /// <summary>
  341. /// 应用程序的主入口点。
  342. /// </summary>
  343. [STAThread]
  344. static void Main() 
  345. {
  346. Application.Run(new Form1());
  347. }
  348. private void button3_Click(object sender, System.EventArgs e)
  349. {
  350. //获得待加密文件名
  351. string inName=this.textBox1.Text;
  352. //获得保存文件名
  353. string outName=this.textBox2.Text;
  354.             //设定初始向量
  355. byte[]  desIV={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  356. byte[]  desKey={};
  357.             //根据密码算出密钥
  358. string keyString=this.textBox3.Text;
  359. if (keyString.Length>=8)
  360. {
  361. desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
  362. (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
  363. (byte)keyString[6],(byte)keyString[7]};
  364. }
  365.     
  366. if (keyString.Length==6)
  367. {
  368. desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
  369.   (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
  370.   0x07,0x08};
  371. }
  372. if (keyString.Length==7)
  373. {
  374. desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
  375.   (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
  376.   (byte)keyString[6],0x07};
  377. }
  378. //创建文件流分别指向输入和输出文件
  379. FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
  380. FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
  381. fout.SetLength(0);
  382.     
  383. //每次的中间流.
  384. byte[] bin = new byte[100]; 
  385. //代表已经加密的流的大小
  386. int complete=0;
  387. //代表要加密文件总的大小
  388. long totlen = fin.Length; 
  389. //每次写入的大小
  390. int len;   
  391. //创建DES对象
  392. DES des = new DESCryptoServiceProvider();     
  393. //创建加密流
  394. CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
  395.             
  396. //从输入文件中读取流,然后加密到输出文件中
  397. while(complete < totlen)
  398. {
  399. len = fin.Read(bin, 0, 100);
  400. encStream.Write(bin, 0, len);
  401. complete = complete + len;
  402. }
  403.     
  404. //关闭流
  405. encStream.Close();  
  406. fout.Close();
  407. fin.Close();                   
  408.      MessageBox.Show("文件加密已经完成","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  409.     }
  410. private void button5_Click(object sender, System.EventArgs e)
  411. {
  412. //获得要解密的文件名
  413. string inName=this.textBox5.Text;
  414. //获得要保存的文件名
  415. string outName=this.textBox6.Text;
  416. //设定初始向量
  417. byte[]  desIV={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  418. byte[]  desKey={};
  419. //根据密码算出密钥
  420. string keyString=this.textBox7.Text;
  421. if (keyString.Length>=8)
  422. {
  423. desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
  424.  (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
  425.  (byte)keyString[6],(byte)keyString[7]};
  426. }
  427.     
  428. if (keyString.Length==6)
  429. {
  430. desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
  431.  (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
  432.  0x07,0x08};
  433. }
  434. if (keyString.Length==7)
  435. {
  436. desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
  437.  (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
  438.  (byte)keyString[6],0x07};
  439. }
  440. try
  441. {
  442. //创建文件流分别指向输入和输出文件
  443. FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
  444. FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
  445. fout.SetLength(0);
  446.        
  447. //每次的中间流.
  448. byte[] bin = new byte[100]; 
  449. //代表已经解密的流的大小
  450. int complete=0;
  451. //代表要解密文件总的大小
  452. long totlen = fin.Length; 
  453. //每次写入的大小
  454. int len;                   
  455.                 
  456. //创建DES对象
  457. DES des = new DESCryptoServiceProvider();     
  458. //创建解密流
  459. CryptoStream decStream = new  CryptoStream(fout, des.CreateDecryptor(desKey,desIV), CryptoStreamMode.Write);
  460.                 
  461.  
  462. //从输入文件中读取流,然后解密到输出文件中
  463. while(complete < totlen)
  464. {
  465. len = fin.Read(bin, 0, 100);
  466. decStream.Write(bin, 0, len);
  467. complete=complete+len;
  468. }
  469.  
  470.   //关闭流
  471. decStream.Close();  
  472. fout.Close();
  473. fin.Close();     
  474. MessageBox.Show("文件解密已经完成","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  475. }
  476. catch (Exception error)
  477. {
  478.             //密码不正确的警告
  479. MessageBox.Show("操作有误: "+error.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
  480. }
  481. }
  482. private void button1_Click(object sender, System.EventArgs e)
  483. {
  484. OpenFileDialog ofDialog=new OpenFileDialog();
  485. if (ofDialog.ShowDialog()==DialogResult.OK)
  486. {
  487. //将待加密文件的路径写到文本框
  488. this.textBox1.Text=ofDialog.FileName;
  489. };
  490. }
  491. private void button6_Click(object sender, System.EventArgs e)
  492. {
  493. SaveFileDialog sfDialog=new SaveFileDialog();
  494. //设置过滤器
  495. sfDialog.Filter="My encrypt file(*.mef)|*.mef|All files(*.*)|*.*";
  496. if (sfDialog.ShowDialog()==DialogResult.OK)
  497. {
  498.    //将加密文件的保存路径写到文本框
  499. this.textBox2.Text=sfDialog.FileName;
  500. }
  501. }
  502. private void button4_Click(object sender, System.EventArgs e)
  503. {
  504. OpenFileDialog ofDialog=new OpenFileDialog();
  505. //设置过滤器
  506. ofDialog.Filter="My encrypt file(*.mef)|*.mef|All files(*.*)|*.*";
  507. if (ofDialog.ShowDialog()==DialogResult.OK)
  508. {
  509. //将待解密文件的路径写到文本框
  510. this.textBox5.Text=ofDialog.FileName;
  511. };
  512. }
  513. private void button7_Click(object sender, System.EventArgs e)
  514. {
  515. SaveFileDialog sfDialog=new SaveFileDialog();
  516. if (sfDialog.ShowDialog()==DialogResult.OK)
  517. {
  518. //将解密文件的保存路径写到文本框
  519. this.textBox6.Text=sfDialog.FileName;
  520. }
  521. }
  522. private void button2_Click(object sender, System.EventArgs e)
  523. {
  524. //确认密码是否输入正确
  525. if (this.textBox3.Text.Length<6 | this.textBox3.Text!=this.textBox4.Text)
  526. {
  527. MessageBox.Show("请选择正确的密码","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
  528. }
  529. else
  530. {
  531. //确认加密文件名和保存文件名是否正确
  532. if (this.textBox1.Text=="")
  533. {
  534. MessageBox.Show("请选择加密文件","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
  535. }
  536. else
  537. {
  538. if (this.textBox2.Text=="")
  539. {
  540. MessageBox.Show("请输入保存文件名","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
  541. }
  542. else
  543. {
  544. //现在可以进行加密了
  545. this.button3.Enabled=true;
  546. }
  547. }
  548. }
  549. }
  550. }
  551. }