博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用图片对文件进行加密与解密
阅读量:6183 次
发布时间:2019-06-21

本文共 13529 字,大约阅读时间需要 45 分钟。

 

 

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 EncryptTextFileTwo  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 = "jpg,bmp,gif|*.jpg;*.gif;*.bmp"; 25 if (openFileDialog1.ShowDialog() == DialogResult.OK) 26 {
27 pictureBox1.ImageLocation = openFileDialog1.FileName; 28 } 29 } 30 //打开加密文件 31 private void button2_Click(object sender, EventArgs e) 32 {
33 openFileDialog1.Filter = "文本文件|*.txt"; 34 if (openFileDialog1.ShowDialog() == DialogResult.OK) 35 {
36 textBox1.Text = openFileDialog1.FileName; 37 } 38 } 39 // 加密 40 private void button3_Click(object sender, EventArgs e) 41 {
42 try 43 {
44 if (pictureBox1.ImageLocation == null) 45 { MessageBox.Show("请选择一幅图片用于加密"); return; } 46 if (textBox1.Text == "") 47 { MessageBox.Show("请选择加密文件路径"); return; } 48 //图片流 49 FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read); 50 //加密文件流 51 FileStream fsText = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read); 52 //初始化Key IV 53 byte[] bykey = new byte[16]; 54 byte[] byIv = new byte[8]; 55 fsPic.Read(bykey, 0, 16); 56 fsPic.Read(byIv, 0, 8); 57 //临时加密文件 58 string strPath = textBox1.Text;//加密文件的路径 59 int intLent = strPath.LastIndexOf("\\") + 1; 60 int intLong = strPath.Length; 61 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称 62 string strLinPath = "C:\\" + strName;//临时加密文件路径,所以被加密的文件不可以放在C盘的根目录下 63 FileStream fsOut = File.Open(strLinPath, FileMode.Create, FileAccess.Write); 64 //开始加密 65 RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//des进行加 66 BinaryReader br = new BinaryReader(fsText);//从要加密的文件中读出文件内容 67 CryptoStream cs = new CryptoStream(fsOut, desc.CreateEncryptor(bykey, byIv), CryptoStreamMode.Write);//写入临时加密文件 68 cs.Write(br.ReadBytes((int)fsText.Length), 0, (int)fsText.Length);//写入加密流 69 cs.FlushFinalBlock(); 70 cs.Flush(); 71 cs.Close(); 72 fsPic.Close(); 73 fsText.Close(); 74 fsOut.Close(); 75 File.Delete(textBox1.Text.TrimEnd());//册除原文件 76 File.Copy(strLinPath, textBox1.Text);//复制加密文件 77 File.Delete(strLinPath);//册除临时文件 78 MessageBox.Show("加密成功"); 79 pictureBox1.ImageLocation = null; 80 textBox1.Text = ""; 81 } 82 catch (Exception ee) 83 {
84 MessageBox.Show(ee.Message); 85 } 86 87 } 88 89 //解密 90 private void button4_Click(object sender, EventArgs e) 91 {
92 try 93 {
94 //图片流 95 FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read); 96 //解密文件流 97 FileStream fsOut = File.Open(textBox1.Text, FileMode.Open, FileAccess.Read); 98 //初始化Key IV 99 byte[] bykey = new byte[16]; 100 byte[] byIv = new byte[8]; 101 fsPic.Read(bykey, 0, 16); 102 fsPic.Read(byIv, 0, 8); 103 //临时解密文件 104 string strPath = textBox1.Text;//加密文件的路径 105 int intLent = strPath.LastIndexOf("\\") + 1; 106 int intLong = strPath.Length; 107 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称 108 string strLinPath = "C:\\" + strName;//临时解密文件路径 109 FileStream fs = new FileStream(strLinPath, FileMode.Create, FileAccess.Write); 110 //开始解密 111 RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//des进行解 112 CryptoStream csDecrypt = new CryptoStream(fsOut, desc.CreateDecryptor(bykey, byIv), CryptoStreamMode.Read);//读出加密文件 113 BinaryReader sr = new BinaryReader(csDecrypt);//从要加密流中读出文件内容 114 BinaryWriter sw = new BinaryWriter(fs);//写入解密流 115 sw.Write(sr.ReadBytes(Convert.ToInt32(fsOut.Length)));// 116 sw.Flush(); 117 sw.Close(); 118 sr.Close(); 119 fs.Close(); 120 fsOut.Close(); 121 fsPic.Close(); 122 csDecrypt.Flush(); 123 124 File.Delete(textBox1.Text.TrimEnd());//册除原文件 125 File.Copy(strLinPath, textBox1.Text);//复制加密文件 126 File.Delete(strLinPath);//册除临时文件 127 MessageBox.Show("解密成功"); 128 pictureBox1.ImageLocation = null; 129 textBox1.Text = ""; 130 } 131 catch (Exception ee) 132 {
133 MessageBox.Show(ee.Message); 134 } 135 } 136 } 137 }

Form1.Designer.cs

View Code
1 namespace EncryptTextFileTwo   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.groupBox1 = new System.Windows.Forms.GroupBox(); 32 this.button4 = new System.Windows.Forms.Button(); 33 this.button3 = new System.Windows.Forms.Button(); 34 this.textBox1 = new System.Windows.Forms.TextBox(); 35 this.button2 = new System.Windows.Forms.Button(); 36 this.label2 = new System.Windows.Forms.Label(); 37 this.label1 = new System.Windows.Forms.Label(); 38 this.pictureBox1 = new System.Windows.Forms.PictureBox(); 39 this.button1 = new System.Windows.Forms.Button(); 40 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 41 this.groupBox1.SuspendLayout(); 42 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 43 this.SuspendLayout(); 44 // 45 // groupBox1 46 // 47 this.groupBox1.Controls.Add(this.button4); 48 this.groupBox1.Controls.Add(this.button3); 49 this.groupBox1.Controls.Add(this.textBox1); 50 this.groupBox1.Controls.Add(this.button2); 51 this.groupBox1.Controls.Add(this.label2); 52 this.groupBox1.Controls.Add(this.label1); 53 this.groupBox1.Controls.Add(this.pictureBox1); 54 this.groupBox1.Controls.Add(this.button1); 55 this.groupBox1.Location = new System.Drawing.Point(11, 3); 56 this.groupBox1.Name = "groupBox1"; 57 this.groupBox1.Size = new System.Drawing.Size(282, 206); 58 this.groupBox1.TabIndex = 0; 59 this.groupBox1.TabStop = false; 60 // 61 // button4 62 // 63 this.button4.Location = new System.Drawing.Point(189, 172); 64 this.button4.Name = "button4"; 65 this.button4.Size = new System.Drawing.Size(75, 23); 66 this.button4.TabIndex = 15; 67 this.button4.Text = "解密"; 68 this.button4.UseVisualStyleBackColor = true; 69 this.button4.Click += new System.EventHandler(this.button4_Click); 70 // 71 // button3 72 // 73 this.button3.Location = new System.Drawing.Point(90, 172); 74 this.button3.Name = "button3"; 75 this.button3.Size = new System.Drawing.Size(75, 23); 76 this.button3.TabIndex = 14; 77 this.button3.Text = "加密"; 78 this.button3.UseVisualStyleBackColor = true; 79 this.button3.Click += new System.EventHandler(this.button3_Click); 80 // 81 // textBox1 82 // 83 this.textBox1.Location = new System.Drawing.Point(21, 145); 84 this.textBox1.Name = "textBox1"; 85 this.textBox1.Size = new System.Drawing.Size(243, 21); 86 this.textBox1.TabIndex = 13; 87 // 88 // button2 89 // 90 this.button2.Location = new System.Drawing.Point(170, 116); 91 this.button2.Name = "button2"; 92 this.button2.Size = new System.Drawing.Size(94, 23); 93 this.button2.TabIndex = 12; 94 this.button2.Text = "打开文本文件\r\n"; 95 this.button2.UseVisualStyleBackColor = true; 96 this.button2.Click += new System.EventHandler(this.button2_Click); 97 // 98 // label2 99 // 100 this.label2.AutoSize = true; 101 this.label2.Location = new System.Drawing.Point(21, 126); 102 this.label2.Name = "label2"; 103 this.label2.Size = new System.Drawing.Size(65, 12); 104 this.label2.TabIndex = 11; 105 this.label2.Text = "文本文件:"; 106 // 107 // label1 108 // 109 this.label1.AutoSize = true; 110 this.label1.Location = new System.Drawing.Point(17, 26); 111 this.label1.Name = "label1"; 112 this.label1.Size = new System.Drawing.Size(65, 12); 113 this.label1.TabIndex = 10; 114 this.label1.Text = "显示图片:"; 115 // 116 // pictureBox1 117 // 118 this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 119 this.pictureBox1.Location = new System.Drawing.Point(17, 45); 120 this.pictureBox1.Name = "pictureBox1"; 121 this.pictureBox1.Size = new System.Drawing.Size(247, 61); 122 this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 123 this.pictureBox1.TabIndex = 9; 124 this.pictureBox1.TabStop = false; 125 // 126 // button1 127 // 128 this.button1.Location = new System.Drawing.Point(187, 15); 129 this.button1.Name = "button1"; 130 this.button1.Size = new System.Drawing.Size(75, 23); 131 this.button1.TabIndex = 8; 132 this.button1.Text = "打开图片"; 133 this.button1.UseVisualStyleBackColor = true; 134 this.button1.Click += new System.EventHandler(this.button1_Click); 135 // 136 // Form1 137 // 138 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 139 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 140 this.ClientSize = new System.Drawing.Size(304, 218); 141 this.Controls.Add(this.groupBox1); 142 this.MaximizeBox = false; 143 this.MinimizeBox = false; 144 this.Name = "Form1"; 145 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 146 this.Text = "利用图片对文件进行加密与解密"; 147 this.groupBox1.ResumeLayout(false); 148 this.groupBox1.PerformLayout(); 149 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 150 this.ResumeLayout(false); 151 152 } 153 154 #endregion 155 156 private System.Windows.Forms.GroupBox groupBox1; 157 private System.Windows.Forms.Button button4; 158 private System.Windows.Forms.Button button3; 159 private System.Windows.Forms.TextBox textBox1; 160 private System.Windows.Forms.Button button2; 161 private System.Windows.Forms.Label label2; 162 private System.Windows.Forms.Label label1; 163 private System.Windows.Forms.PictureBox pictureBox1; 164 private System.Windows.Forms.Button button1; 165 private System.Windows.Forms.OpenFileDialog openFileDialog1; 166 } 167 }

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

转载于:https://www.cnblogs.com/wifi/articles/2473401.html

你可能感兴趣的文章
springboot初探
查看>>
dandelion.exe总是生成桌面连接,安装金山词霸绑定了dandelion.exe
查看>>
vue中使用jquery报错 $ is not defined
查看>>
TCP粘包分析,及解决方法。
查看>>
expected date of childbirth
查看>>
47、我的C#学习笔记13
查看>>
协议RFC对应表
查看>>
Centos 6.2部署zabbix2.2.1
查看>>
LINUX下多路径(multi-path)介绍及使用
查看>>
抽象编程语言(APL)开发架构
查看>>
dragsort:很不错的拖动排序zujian
查看>>
linux设备驱动第五篇:驱动中的并发与竟态
查看>>
B树和B+树的总结
查看>>
原创:微信小程序亲测体验,公众号入口曝光!
查看>>
WebRTC如何颠覆移动世界
查看>>
3G技术特点及市场分析
查看>>
linux 通过ssh代理上网
查看>>
SQL Server 备份简述
查看>>
文件上传-文件名长度绕过白名单限制
查看>>
samba服务器配置
查看>>