using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AES_2
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string file = "";
if (txtPath.Text != "")
{
file = txtPath.Text;
}
else
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//該值確定是否可以選擇多個檔案
dialog.Title = "請選擇圖片";
dialog.Filter = "圖片檔案(*.bmp)|*.bmp";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
file = dialog.FileName;
txtPath.Text = file;
}
}
Bitmap enBit = Encrypt(new Bitmap(file), 3.8, 0.5);
pictureBox1.Image = enBit;
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(@"d:\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
/// <summary>
/// 基于Logistic模型的混沌加解密
/// </summary>
/// <param name="src">要处理的图像数据</param>
/// <param name="u">应属于[3.57,4]</param>
/// <param name="x0">应属于(0,1)</param>
/// <returns></returns>
public static Bitmap Encrypt(Bitmap src, double u, double x0)
{
Bitmap dest = new Bitmap(src.Width, src.Height);
double x = logistic(u, x0, 2000);
int key;
for (int i = 0; i < src.Width; i++)
{
for (int j = 0; j < src.Height; j++)
{
Color srcColor = src.GetPixel(i, j);
x = logistic(u, x, 5);
key = Convert.ToInt32(Math.Floor(x * 1000)) % 256;
int r = key ^ srcColor.R;
x = logistic(u, x, 5);
key = Convert.ToInt32(Math.Floor(x * 1000)) % 256;
int g = key ^ srcColor.G;
x = logistic(u, x, 5);
key = Convert.ToInt32(Math.Floor(x * 1000)) % 256;
int b = key ^ srcColor.B;
dest.SetPixel(i, j, Color.FromArgb(r, g, b));
}
}
return dest;
}
private static double logistic(double u, double x, int n)
{
for (int i = 0; i < n; i++)
{
x = u * x * (1 - x);
}
return x;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
if (txtPath.Text != "") tkBar();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
if (txtPath.Text != "") tkBar();
}
private void tkBar()
{
string file = txtPath.Text;
double u = trackBar1.Value / 100.0;
double x0 = trackBar2.Value / 100.0;
Bitmap enBit = Encrypt(new Bitmap(file), u, x0);
pictureBox1.Image = enBit;
}
}
}