C#图片处理----基于Logistic混沌算法的图像加密

本文采用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()方法结束进程。

注意:

如果不能正常解密,请注意检验输入的加密系数

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值