EncryptFile:
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 12 namespace EncryptFile 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void button1_Click(object sender, EventArgs e) 22 { 23 openFileDialog1.Filter = "*.*|*.*"; 24 openFileDialog1.ShowDialog(); 25 textBox1.Text = openFileDialog1.FileName; 26 button2.Enabled = true; 27 } 28 29 private void button2_Click(object sender, EventArgs e) 30 { 31 string myFile = textBox1.Text; 32 string myPassword = textBox2.Text; 33 string myEnFile = textBox3.Text; 34 try 35 { 36 byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 37 byte[] myKey = System.Text.Encoding.UTF8.GetBytes(myPassword); 38 FileStream myInStream = new FileStream(myFile, FileMode.Open, FileAccess.Read); 39 FileStream myOutStream = new FileStream(myEnFile, FileMode.OpenOrCreate, FileAccess.Write); 40 myOutStream.SetLength(0); 41 byte[] myBytes = new byte[100]; 42 long myInLength = 0; 43 long myLength = myInStream.Length; 44 DES myProvider = new DESCryptoServiceProvider(); 45 CryptoStream myCryptoStream = new CryptoStream(myOutStream, myProvider.CreateEncryptor(myKey, myIV), CryptoStreamMode.Write); 46 while (myInLength < myLength) 47 { 48 int mylen = myInStream.Read(myBytes, 0, 100); 49 myCryptoStream.Write(myBytes, 0, mylen); 50 myInLength += mylen; 51 } 52 myCryptoStream.Close(); 53 myInStream.Close(); 54 myOutStream.Close(); 55 MessageBox.Show("加密文件成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 56 } 57 catch (Exception ex) 58 { 59 MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 60 } 61 } 62 } 63 }
Form1.Designer.cs
View Code
1 namespace EncryptFile 2 { 3 partial class Form1 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 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 ///26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.textBox3 = new System.Windows.Forms.TextBox(); 32 this.label3 = new System.Windows.Forms.Label(); 33 this.textBox2 = new System.Windows.Forms.TextBox(); 34 this.label2 = new System.Windows.Forms.Label(); 35 this.button2 = new System.Windows.Forms.Button(); 36 this.button1 = new System.Windows.Forms.Button(); 37 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 38 this.textBox1 = new System.Windows.Forms.TextBox(); 39 this.label1 = new System.Windows.Forms.Label(); 40 this.SuspendLayout(); 41 // 42 // textBox3 43 // 44 this.textBox3.Location = new System.Drawing.Point(123, 67); 45 this.textBox3.Name = "textBox3"; 46 this.textBox3.Size = new System.Drawing.Size(105, 21); 47 this.textBox3.TabIndex = 14; 48 // 49 // label3 50 // 51 this.label3.AutoSize = true; 52 this.label3.Location = new System.Drawing.Point(11, 70); 53 this.label3.Name = "label3"; 54 this.label3.Size = new System.Drawing.Size(101, 12); 55 this.label3.TabIndex = 13; 56 this.label3.Text = "加密后文件路径:"; 57 // 58 // textBox2 59 // 60 this.textBox2.Location = new System.Drawing.Point(123, 40); 61 this.textBox2.Name = "textBox2"; 62 this.textBox2.PasswordChar = '*'; 63 this.textBox2.Size = new System.Drawing.Size(105, 21); 64 this.textBox2.TabIndex = 12; 65 // 66 // label2 67 // 68 this.label2.AutoSize = true; 69 this.label2.Location = new System.Drawing.Point(11, 43); 70 this.label2.Name = "label2"; 71 this.label2.Size = new System.Drawing.Size(107, 12); 72 this.label2.TabIndex = 11; 73 this.label2.Text = "加密密码(8位):"; 74 // 75 // button2 76 // 77 this.button2.Enabled = false; 78 this.button2.Location = new System.Drawing.Point(123, 94); 79 this.button2.Name = "button2"; 80 this.button2.Size = new System.Drawing.Size(78, 23); 81 this.button2.TabIndex = 8; 82 this.button2.Text = "加密"; 83 this.button2.UseVisualStyleBackColor = true; 84 this.button2.Click += new System.EventHandler(this.button2_Click); 85 // 86 // button1 87 // 88 this.button1.Location = new System.Drawing.Point(232, 11); 89 this.button1.Name = "button1"; 90 this.button1.Size = new System.Drawing.Size(38, 23); 91 this.button1.TabIndex = 7; 92 this.button1.Text = "选择"; 93 this.button1.UseVisualStyleBackColor = true; 94 this.button1.Click += new System.EventHandler(this.button1_Click); 95 // 96 // textBox1 97 // 98 this.textBox1.Location = new System.Drawing.Point(123, 13); 99 this.textBox1.Name = "textBox1"; 100 this.textBox1.ReadOnly = true; 101 this.textBox1.Size = new System.Drawing.Size(105, 21); 102 this.textBox1.TabIndex = 10; 103 // 104 // label1 105 // 106 this.label1.AutoSize = true; 107 this.label1.Location = new System.Drawing.Point(11, 16); 108 this.label1.Name = "label1"; 109 this.label1.Size = new System.Drawing.Size(77, 12); 110 this.label1.TabIndex = 9; 111 this.label1.Text = "原文件路径:"; 112 // 113 // Form1 114 // 115 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 116 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 117 this.ClientSize = new System.Drawing.Size(283, 129); 118 this.Controls.Add(this.textBox3); 119 this.Controls.Add(this.label3); 120 this.Controls.Add(this.textBox2); 121 this.Controls.Add(this.label2); 122 this.Controls.Add(this.button2); 123 this.Controls.Add(this.button1); 124 this.Controls.Add(this.textBox1); 125 this.Controls.Add(this.label1); 126 this.MaximizeBox = false; 127 this.MinimizeBox = false; 128 this.Name = "Form1"; 129 this.ShowInTaskbar = false; 130 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 131 this.Text = "使用对称算法加密文件"; 132 this.ResumeLayout(false); 133 this.PerformLayout(); 134 135 } 136 137 #endregion 138 139 private System.Windows.Forms.TextBox textBox3; 140 private System.Windows.Forms.Label label3; 141 private System.Windows.Forms.TextBox textBox2; 142 private System.Windows.Forms.Label label2; 143 private System.Windows.Forms.Button button2; 144 private System.Windows.Forms.Button button1; 145 private System.Windows.Forms.OpenFileDialog openFileDialog1; 146 private System.Windows.Forms.TextBox textBox1; 147 private System.Windows.Forms.Label label1; 148 } 149 }
UnEncryptFile:
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 12 namespace UnEncryptFile 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void button1_Click(object sender, EventArgs e) 22 { 23 openFileDialog1.Filter = "*.*|*.*"; 24 openFileDialog1.ShowDialog(); 25 textBox1.Text = openFileDialog1.FileName; 26 button2.Enabled = true; 27 } 28 29 private void button2_Click(object sender, EventArgs e) 30 { 31 string str1 = textBox1.Text; 32 string strPwd = textBox2.Text; 33 string str2 = textBox3.Text; 34 try 35 { 36 byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 37 byte[] myKey = System.Text.Encoding.UTF8.GetBytes(strPwd); 38 FileStream myFileIn = new FileStream(str1, FileMode.Open, FileAccess.Read); 39 FileStream myFileOut = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write); 40 myFileOut.SetLength(0); 41 byte[] myBytes = new byte[100]; 42 long myLength = myFileIn.Length; 43 long myInLength = 0; 44 DES myProvider = new DESCryptoServiceProvider(); 45 CryptoStream myDeStream = new CryptoStream(myFileOut, myProvider.CreateDecryptor(myKey, myIV), CryptoStreamMode.Write); 46 while (myInLength < myLength) 47 { 48 int mylen = myFileIn.Read(myBytes, 0, 100); 49 myDeStream.Write(myBytes, 0, mylen); 50 myInLength += mylen; 51 } 52 myDeStream.Close(); 53 myFileOut.Close(); 54 myFileIn.Close(); 55 MessageBox.Show("解密文件成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 56 } 57 catch (Exception ex) 58 { 59 MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 60 } 61 } 62 } 63 }
Form1.Designer.cs
View Code
1 namespace UnEncryptFile 2 { 3 partial class Form1 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 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 ///26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.textBox3 = new System.Windows.Forms.TextBox(); 32 this.label3 = new System.Windows.Forms.Label(); 33 this.textBox2 = new System.Windows.Forms.TextBox(); 34 this.label2 = new System.Windows.Forms.Label(); 35 this.textBox1 = new System.Windows.Forms.TextBox(); 36 this.button2 = new System.Windows.Forms.Button(); 37 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 38 this.label1 = new System.Windows.Forms.Label(); 39 this.button1 = new System.Windows.Forms.Button(); 40 this.SuspendLayout(); 41 // 42 // textBox3 43 // 44 this.textBox3.Location = new System.Drawing.Point(126, 67); 45 this.textBox3.Name = "textBox3"; 46 this.textBox3.Size = new System.Drawing.Size(100, 21); 47 this.textBox3.TabIndex = 14; 48 // 49 // label3 50 // 51 this.label3.AutoSize = true; 52 this.label3.Location = new System.Drawing.Point(18, 70); 53 this.label3.Name = "label3"; 54 this.label3.Size = new System.Drawing.Size(101, 12); 55 this.label3.TabIndex = 13; 56 this.label3.Text = "解密后文件路径:"; 57 // 58 // textBox2 59 // 60 this.textBox2.Location = new System.Drawing.Point(126, 40); 61 this.textBox2.Name = "textBox2"; 62 this.textBox2.PasswordChar = '*'; 63 this.textBox2.Size = new System.Drawing.Size(100, 21); 64 this.textBox2.TabIndex = 12; 65 // 66 // label2 67 // 68 this.label2.AutoSize = true; 69 this.label2.Location = new System.Drawing.Point(18, 43); 70 this.label2.Name = "label2"; 71 this.label2.Size = new System.Drawing.Size(107, 12); 72 this.label2.TabIndex = 11; 73 this.label2.Text = "解密密码(8位):"; 74 // 75 // textBox1 76 // 77 this.textBox1.Location = new System.Drawing.Point(126, 13); 78 this.textBox1.Name = "textBox1"; 79 this.textBox1.ReadOnly = true; 80 this.textBox1.Size = new System.Drawing.Size(100, 21); 81 this.textBox1.TabIndex = 10; 82 // 83 // button2 84 // 85 this.button2.Enabled = false; 86 this.button2.Location = new System.Drawing.Point(126, 94); 87 this.button2.Name = "button2"; 88 this.button2.Size = new System.Drawing.Size(78, 23); 89 this.button2.TabIndex = 7; 90 this.button2.Text = "解密"; 91 this.button2.UseVisualStyleBackColor = true; 92 this.button2.Click += new System.EventHandler(this.button2_Click); 93 // 94 // openFileDialog1 95 // 96 this.openFileDialog1.FileName = "openFileDialog1"; 97 // 98 // label1 99 // 100 this.label1.AutoSize = true; 101 this.label1.Location = new System.Drawing.Point(18, 16); 102 this.label1.Name = "label1"; 103 this.label1.Size = new System.Drawing.Size(77, 12); 104 this.label1.TabIndex = 9; 105 this.label1.Text = "原文件路径:"; 106 // 107 // button1 108 // 109 this.button1.Location = new System.Drawing.Point(229, 11); 110 this.button1.Name = "button1"; 111 this.button1.Size = new System.Drawing.Size(38, 23); 112 this.button1.TabIndex = 8; 113 this.button1.Text = "选择"; 114 this.button1.UseVisualStyleBackColor = true; 115 this.button1.Click += new System.EventHandler(this.button1_Click); 116 // 117 // Form1 118 // 119 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 120 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 121 this.ClientSize = new System.Drawing.Size(283, 129); 122 this.Controls.Add(this.textBox3); 123 this.Controls.Add(this.label3); 124 this.Controls.Add(this.textBox2); 125 this.Controls.Add(this.label2); 126 this.Controls.Add(this.textBox1); 127 this.Controls.Add(this.button2); 128 this.Controls.Add(this.label1); 129 this.Controls.Add(this.button1); 130 this.MaximizeBox = false; 131 this.MinimizeBox = false; 132 this.Name = "Form1"; 133 this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show; 134 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 135 this.Text = "使用对称算法解密文件"; 136 this.ResumeLayout(false); 137 this.PerformLayout(); 138 139 } 140 141 #endregion 142 143 private System.Windows.Forms.TextBox textBox3; 144 private System.Windows.Forms.Label label3; 145 private System.Windows.Forms.TextBox textBox2; 146 private System.Windows.Forms.Label label2; 147 private System.Windows.Forms.TextBox textBox1; 148 private System.Windows.Forms.Button button2; 149 private System.Windows.Forms.OpenFileDialog openFileDialog1; 150 private System.Windows.Forms.Label label1; 151 private System.Windows.Forms.Button button1; 152 } 153 }
作者: 出处: 关于作者:专注于.Net、WCF和移动互联网开发。 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过ynbt_wang@163.com联系我,非常感谢。 。