对文本文件进行加密与解密

 

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.Security.Cryptography;
10 using System.IO;
11
12 namespace EncryptTextFileOne
13 {
14 public partial class Form1 : Form
15 {
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 //选择加密解密文件路径
22 private void button1_Click(object sender, EventArgs e)
23 {
24 openFileDialog1.Filter = "文本文件|*.txt|*.*|*.*";
25 if (openFileDialog1.ShowDialog() == DialogResult.OK)
26 {
27 textBox1.Text = openFileDialog1.FileName;
28 }
29 }
30 ///*************************/
31
32 //加密
33 private void button2_Click(object sender, EventArgs e)
34 {
35 if (textBox1.Text == "")
36 { MessageBox.Show("请选择要加密的文件"); }
37 else
38 {
39 try
40 {
41 string strPath = textBox1.Text;//加密文件的路径
42 int intLent = strPath.LastIndexOf("\\") + 1;
43 int intLong = strPath.Length;
44 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称
45 int intTxt = strName.LastIndexOf(".");
46 int intTextLeng = strName.Length;
47 string strTxt = strName.Substring(intTxt, intTextLeng - intTxt);//取出文件的扩展名
48 strName = strName.Substring(0, intTxt);
49 //加密后的文件名及路径
50 string strOutName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + "Out" + strTxt;
51 byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
52 byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
53 RijndaelManaged myRijndael = new RijndaelManaged();
54 FileStream fsOut = File.Open(strOutName, FileMode.Create, FileAccess.Write);
55 FileStream fsIn = File.Open(strPath, FileMode.Open, FileAccess.Read);
56 //写入加密文本文件
57 CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Write);
58 //读加密文本
59 BinaryReader br = new BinaryReader(fsIn);
60 csDecrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length);
61 csDecrypt.FlushFinalBlock();
62 csDecrypt.Close();
63 fsIn.Close();
64 fsOut.Close();
65 if (MessageBox.Show("加密成功!加密后的文件名及路径为:\n" + strOutName + ",是否册除源文件", "信息提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
66 {
67 File.Delete(strPath);
68 textBox1.Text = "";
69 }
70 else
71 { textBox1.Text = ""; }
72 }
73 catch (Exception ee)
74 {
75 MessageBox.Show(ee.Message);
76 }
77 }
78 }
79
80
81 //解密
82 private void button3_Click(object sender, EventArgs e)
83 {
84 if (textBox1.Text == "")
85 {
86 MessageBox.Show("请选择要解密的文件路径");
87 }
88 else
89 {
90 string strPath = textBox1.Text;//加密文件的路径
91 int intLent = strPath.LastIndexOf("\\") + 1;
92 int intLong = strPath.Length;
93 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称
94 int intTxt = strName.LastIndexOf(".");
95 int intTextLeng = strName.Length;
96 strName = strName.Substring(0, intTxt);
97
98 if (strName.LastIndexOf("Out") != -1)
99 {
100 strName = strName.Substring(0, strName.LastIndexOf("Out"));
101
102 }
103 else
104 {
105 strName = strName + "In";
106 }
107 //加密后的文件名及路径
108 string strInName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + ".txt";
109 byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
110 byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
111 RijndaelManaged myRijndael = new RijndaelManaged();
112 FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);
113 CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, IV), CryptoStreamMode.Read);
114 StreamReader sr = new StreamReader(csDecrypt);//把文件读出来
115 StreamWriter sw = new StreamWriter(strInName);//解密后文件写入一个新的文件
116 sw.Write(sr.ReadToEnd());
117 sw.Flush();
118 sw.Close();
119 sr.Close();
120 fsOut.Close();
121 if (MessageBox.Show("解密成功!解密后的文件名及路径为:"+strInName+",是否册除源文件", "信息提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
122 {
123 File.Delete(strPath);
124 textBox1.Text = "";
125 }
126 else
127 {
128 textBox1.Text = "";
129 }
130
131 }
132 }
133
134
135 /***************************/
136 }
137 }
复制代码

Form1.Designer.cs

View Code
  1 namespace EncryptTextFileOne
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.groupBox1 = new System.Windows.Forms.GroupBox();
32 this.button2 = new System.Windows.Forms.Button();
33 this.button3 = new System.Windows.Forms.Button();
34 this.button1 = new System.Windows.Forms.Button();
35 this.textBox1 = new System.Windows.Forms.TextBox();
36 this.label1 = new System.Windows.Forms.Label();
37 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
38 this.groupBox1.SuspendLayout();
39 this.SuspendLayout();
40 //
41 // groupBox1
42 //
43 this.groupBox1.Controls.Add(this.button2);
44 this.groupBox1.Controls.Add(this.button3);
45 this.groupBox1.Controls.Add(this.button1);
46 this.groupBox1.Controls.Add(this.textBox1);
47 this.groupBox1.Controls.Add(this.label1);
48 this.groupBox1.Location = new System.Drawing.Point(9, 3);
49 this.groupBox1.Name = "groupBox1";
50 this.groupBox1.Size = new System.Drawing.Size(302, 108);
51 this.groupBox1.TabIndex = 1;
52 this.groupBox1.TabStop = false;
53 this.groupBox1.Text = "加密与解密";
54 //
55 // button2
56 //
57 this.button2.Location = new System.Drawing.Point(17, 79);
58 this.button2.Name = "button2";
59 this.button2.Size = new System.Drawing.Size(75, 23);
60 this.button2.TabIndex = 7;
61 this.button2.Text = "加密(&D)";
62 this.button2.UseVisualStyleBackColor = true;
63 this.button2.Click += new System.EventHandler(this.button2_Click);
64 //
65 // button3
66 //
67 this.button3.Location = new System.Drawing.Point(211, 79);
68 this.button3.Name = "button3";
69 this.button3.Size = new System.Drawing.Size(75, 23);
70 this.button3.TabIndex = 6;
71 this.button3.Text = "解密(&F)";
72 this.button3.UseVisualStyleBackColor = true;
73 this.button3.Click += new System.EventHandler(this.button3_Click);
74 //
75 // button1
76 //
77 this.button1.Location = new System.Drawing.Point(199, 12);
78 this.button1.Name = "button1";
79 this.button1.Size = new System.Drawing.Size(87, 23);
80 this.button1.TabIndex = 5;
81 this.button1.Text = "选择文件(&Q)";
82 this.button1.UseVisualStyleBackColor = true;
83 this.button1.Click += new System.EventHandler(this.button1_Click);
84 //
85 // textBox1
86 //
87 this.textBox1.Location = new System.Drawing.Point(17, 44);
88 this.textBox1.Name = "textBox1";
89 this.textBox1.Size = new System.Drawing.Size(269, 21);
90 this.textBox1.TabIndex = 4;
91 //
92 // label1
93 //
94 this.label1.AutoSize = true;
95 this.label1.Location = new System.Drawing.Point(15, 17);
96 this.label1.Name = "label1";
97 this.label1.Size = new System.Drawing.Size(65, 12);
98 this.label1.TabIndex = 3;
99 this.label1.Text = "文件路径:";
100 //
101 // Form1
102 //
103 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
104 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
105 this.ClientSize = new System.Drawing.Size(326, 119);
106 this.Controls.Add(this.groupBox1);
107 this.MaximizeBox = false;
108 this.MinimizeBox = false;
109 this.Name = "Form1";
110 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
111 this.Text = "对文本文件进行加密与解密";
112 this.groupBox1.ResumeLayout(false);
113 this.groupBox1.PerformLayout();
114 this.ResumeLayout(false);
115
116 }
117
118 #endregion
119
120 private System.Windows.Forms.GroupBox groupBox1;
121 private System.Windows.Forms.Button button2;
122 private System.Windows.Forms.Button button3;
123 private System.Windows.Forms.Button button1;
124 private System.Windows.Forms.TextBox textBox1;
125 private System.Windows.Forms.Label label1;
126 private System.Windows.Forms.OpenFileDialog openFileDialog1;
127 }
128 }



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值