⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 本程序通过混合光学双稳模型的迭代方程对多媒体文件进行混沌加密、解密(加密解密文件分别以.en和.de结尾)
💻 CS
字号:
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 混沌密码加密
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox tbxKey;
		private System.Windows.Forms.Button btnSelect;
		private System.Windows.Forms.Button btnEncrypt;
		private System.Windows.Forms.Button btnDecrypt;
		private System.Windows.Forms.PictureBox pbOriginal;
		private System.Windows.Forms.PictureBox pbEncrypted;
		private System.Windows.Forms.PictureBox pbDecrypted;
		private System.Windows.Forms.OpenFileDialog openFileDialog;

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		/// 
		private double x;
		private const double xb=2.5;
		private const int A=4;
		private string bmpFileName;
		private const int headNum=118;
		private const string fileEnSuffix=".en";
		private const string fileDeSuffix=".de";
		private System.Windows.Forms.Button bnClear;
		private System.ComponentModel.Container components = null;
		

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.label1 = new System.Windows.Forms.Label();
			this.tbxKey = new System.Windows.Forms.TextBox();
			this.btnSelect = new System.Windows.Forms.Button();
			this.btnEncrypt = new System.Windows.Forms.Button();
			this.btnDecrypt = new System.Windows.Forms.Button();
			this.pbOriginal = new System.Windows.Forms.PictureBox();
			this.pbEncrypted = new System.Windows.Forms.PictureBox();
			this.pbDecrypted = new System.Windows.Forms.PictureBox();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.bnClear = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.bnClear);
			this.groupBox1.Controls.Add(this.tbxKey);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(0, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(920, 56);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.pbOriginal);
			this.groupBox2.Controls.Add(this.btnSelect);
			this.groupBox2.Location = new System.Drawing.Point(0, 64);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(304, 304);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "原文件";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.pbEncrypted);
			this.groupBox3.Controls.Add(this.btnEncrypt);
			this.groupBox3.Location = new System.Drawing.Point(312, 64);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(304, 304);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "加密文件";
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.Add(this.pbDecrypted);
			this.groupBox4.Controls.Add(this.btnDecrypt);
			this.groupBox4.Location = new System.Drawing.Point(624, 64);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(304, 304);
			this.groupBox4.TabIndex = 3;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "解密文件";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(53, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "密钥:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// tbxKey
			// 
			this.tbxKey.Location = new System.Drawing.Point(56, 17);
			this.tbxKey.Name = "tbxKey";
			this.tbxKey.TabIndex = 1;
			this.tbxKey.Text = "3";
			// 
			// btnSelect
			// 
			this.btnSelect.Location = new System.Drawing.Point(115, 17);
			this.btnSelect.Name = "btnSelect";
			this.btnSelect.TabIndex = 0;
			this.btnSelect.Text = "选择文件";
			this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
			// 
			// btnEncrypt
			// 
			this.btnEncrypt.Location = new System.Drawing.Point(115, 16);
			this.btnEncrypt.Name = "btnEncrypt";
			this.btnEncrypt.TabIndex = 0;
			this.btnEncrypt.Text = "加密";
			this.btnEncrypt.Click += new System.EventHandler(this.btnEncrypt_Click);
			// 
			// btnDecrypt
			// 
			this.btnDecrypt.Location = new System.Drawing.Point(115, 16);
			this.btnDecrypt.Name = "btnDecrypt";
			this.btnDecrypt.TabIndex = 0;
			this.btnDecrypt.Text = "解密";
			this.btnDecrypt.Click += new System.EventHandler(this.btnDecrypt_Click);
			// 
			// pbOriginal
			// 
			this.pbOriginal.Location = new System.Drawing.Point(8, 56);
			this.pbOriginal.Name = "pbOriginal";
			this.pbOriginal.Size = new System.Drawing.Size(288, 232);
			this.pbOriginal.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
			this.pbOriginal.TabIndex = 1;
			this.pbOriginal.TabStop = false;
			// 
			// pbEncrypted
			// 
			this.pbEncrypted.Location = new System.Drawing.Point(8, 56);
			this.pbEncrypted.Name = "pbEncrypted";
			this.pbEncrypted.Size = new System.Drawing.Size(288, 232);
			this.pbEncrypted.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
			this.pbEncrypted.TabIndex = 2;
			this.pbEncrypted.TabStop = false;
			// 
			// pbDecrypted
			// 
			this.pbDecrypted.Location = new System.Drawing.Point(8, 56);
			this.pbDecrypted.Name = "pbDecrypted";
			this.pbDecrypted.Size = new System.Drawing.Size(288, 232);
			this.pbDecrypted.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
			this.pbDecrypted.TabIndex = 2;
			this.pbDecrypted.TabStop = false;
			// 
			// bnClear
			// 
			this.bnClear.Location = new System.Drawing.Point(832, 16);
			this.bnClear.Name = "bnClear";
			this.bnClear.TabIndex = 2;
			this.bnClear.Text = "清空";
			this.bnClear.Visible = false;
			this.bnClear.Click += new System.EventHandler(this.bnClear_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(928, 373);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Name = "Form1";
			this.Text = "采用混合光学双稳模型对多媒体文件加密";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void btnSelect_Click(object sender, System.EventArgs e)
		{
			this.openFileDialog.Filter="位图文件(*.bmp)|*.bmp";
			this.openFileDialog.Title="打开文件";
			if(this.openFileDialog.ShowDialog()!=DialogResult.Cancel)
			{
				Bitmap bmp=new Bitmap(this.openFileDialog.FileName);
				bmpFileName=this.openFileDialog.FileName;
				this.pbOriginal.Image=bmp;
				this.pbOriginal.Refresh();

				if(this.pbEncrypted.Image!=null)
				{
					this.pbEncrypted.Image.Dispose();
					this.pbEncrypted.Image=null;
					this.pbEncrypted.Refresh();
				}
				if(this.pbDecrypted.Image!=null)
				{
					this.pbDecrypted.Image.Dispose();
					this.pbDecrypted.Image=null;
					this.pbDecrypted.Refresh();
				}
			}

		}

		private byte Generate8K()
		{
			string str="";
			for(int i=0;i<8;i++)
			{
				x=A*Math.Pow(Math.Sin(x-xb),2);
				if(x>(2.0/3.0)*A)
					str+="1";
				else 
					str+="0";
			}
			return Convert.ToByte(str,2);
		}

		private void btnEncrypt_Click(object sender, System.EventArgs e)
		{
			if(this.pbEncrypted.Image!=null)
				this.pbEncrypted.Image.Dispose();
			if(this.pbDecrypted.Image!=null)
			{
				this.pbDecrypted.Image.Dispose();
				this.pbDecrypted.Image=null;
				this.pbDecrypted.Refresh();
			}

			FileStream fsToWrite = new FileStream(bmpFileName+fileEnSuffix, FileMode.Create);
			// Create the writer for data.
			BinaryWriter w = new BinaryWriter(fsToWrite);

			FileStream fs=new FileStream(this.bmpFileName,FileMode.Open,FileAccess.Read);
			BinaryReader r = new BinaryReader(fs);

			//去除文件头
			for(int i=0;i<headNum;i++)
			{
				w.Write(r.ReadByte());
			}

			//对数据段加密
			x=Convert.ToDouble(this.tbxKey.Text);
			try
			{
				while(true)
				{
					byte code=Convert.ToByte(r.ReadByte()^this.Generate8K());
					w.Write(code);
					//w.Write(r.ReadByte());
				}
			}
			catch(EndOfStreamException)
			{
			}
			finally
			{
				w.Close();
				r.Close();
				fsToWrite.Close();
				fs.Close();
			}
			//打开加密文件
			Bitmap bmp=new Bitmap(bmpFileName+fileEnSuffix);
			this.pbEncrypted.Image=bmp;
			this.pbEncrypted.Refresh();
		}

		private void btnDecrypt_Click(object sender, System.EventArgs e)
		{
			if(this.pbDecrypted.Image!=null)
			{
				this.pbDecrypted.Image.Dispose();
				
			}
			
			FileStream fsToWrite = new FileStream(bmpFileName+fileDeSuffix, FileMode.Create);
			// Create the writer for data.
			BinaryWriter w = new BinaryWriter(fsToWrite);

			FileStream fs=new FileStream(this.bmpFileName+fileEnSuffix,FileMode.Open,FileAccess.Read);
			BinaryReader r = new BinaryReader(fs);

			//去除文件头
			for(int i=0;i<headNum;i++)
			{
				w.Write(r.ReadByte());
			}

			//对数据段加密
			x=Convert.ToDouble(this.tbxKey.Text);
			try
			{
				while(true)
				{
					byte code=Convert.ToByte(r.ReadByte()^this.Generate8K());
					w.Write(code);
					//w.Write(r.ReadByte());
				}
			}
			catch(EndOfStreamException)
			{
			}
			finally
			{
				w.Close();
				r.Close();
				fsToWrite.Close();
				fs.Close();
			}
			//打开加密文件
			Bitmap bmp=new Bitmap(bmpFileName+fileDeSuffix);
			this.pbDecrypted.Image=bmp;
			this.pbDecrypted.Refresh();
		}

		private void bnClear_Click(object sender, System.EventArgs e)
		{

			
			this.pbOriginal.Image.Dispose();
			this.pbEncrypted.Image.Dispose();
			this.pbDecrypted.Image.Dispose();

			//this.Refresh();
			this.pbOriginal.Image=null;
			this.pbEncrypted.Image=null;
			this.pbDecrypted.Image=null;

			this.pbOriginal.Refresh();
			this.pbEncrypted.Refresh();
			this.pbDecrypted.Refresh();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -