本文采用Logistic混沌置乱算法对图片进行加密处理
1.首先读入一副图片
private void button1_Click(object sender, EventArgs e)//加载图片
{
OpenFileDialog Pic = new OpenFileDialog();
Pic.Filter = "*.jpg|*.jpg|*.png|*.png";
if (Pic.ShowDialog() == DialogResult.OK)
{
Ini_Pic.Image = Image.FromFile(Pic.FileName );
}
}
2.图像加密,混沌加密系数为0.99
private void button2_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(Ini_Pic.Image);
double x = 0.99;
int ih = bmp.Height;
int iw = bmp.Width;
Color c = new Color();
for (int i = 0; i < 100; i++)
{
x = 4 * x * (1 - x);
}
for (int j = 0; j < ih; j++)
{
for (int i = 0; i < iw; i++)
{
c = bmp.GetPixel(i, j);
int chaos = 0, k;
for (int w = 0; w < 8; w++)
{
x = 4 * x * (1 - x);
if (x >= 0.5) k = 1;
else k = 0;
chaos = ((chaos << 1) | k);
}
// c =
bmp.SetPixel(i, j, Color.FromArgb(c.R ^ (int)chaos,
c.G ^ (int)chaos,
c.B ^ (int)chaos));
}
}
AftDeal_Pic.Image = bmp;
}
3.解密(如果自己设置加密系数,切记解密系数与加密系数一致)
private void button3_Click(object sender, EventArgs e)//解密
{
if (AftDeal_Pic .Image != null)
{
Bitmap bm1 = new Bitmap(AftDeal_Pic .Image);
Color c = new Color();
double x = 0.99;
int ih = bm1.Height, iw = bm1.Height;
for (int i = 0; i < 100; i++)
{
x = 4 * x * (1 - x);
}
for (int j = 0; j < ih; j++)
{
for (int i = 0; i < iw; i++)
{
c = bm1.GetPixel(i, j);
int chaos = 0, k;
for (int w = 0; w < 8; w++)
{
x = 4 * x * (1 - x);
if (x >= 0.5) k = 1;
else k = 0;
chaos = ((chaos << 1) | k);
}
// c =
bm1.SetPixel(i, j, Color.FromArgb(c.R ^ (int)chaos,
c.G ^ (int)chaos,
c.B ^ (int)chaos));
}
}
AftDeal_Pic .Image = bm1;
}
else
MessageBox.Show("请先加密!");
}
4.保存
private void button4_Click(object sender, EventArgs e)
{
Bitmap bmpS = new Bitmap(AftDeal_Pic .Image );
SaveFileDialog sa = new SaveFileDialog();
sa.Filter = "*.jpg|*.jpg|*.png|*.png";
if (sa.ShowDialog() == DialogResult.OK)
{
bmpS.Save(sa.FileName );
}
}
5.退出程序通过exit()方法结束进程。
注意:
如果不能正常解密,请注意检验输入的加密系数