?? form1.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 + -