[精讚] [會員登入]
161

[C#] 加密圖片,透過Logistic混沌演算法

加密圖片,透過Logistic混沌演算法

分享此文連結 //n.sfs.tw/15829

分享連結 [C#] 加密圖片,透過Logistic混沌演算法@大塚 宏 ~認真玩・輕鬆學~
(文章歡迎轉載,務必尊重版權註明連結來源)
最後編修
2022-05-11 09:33:28 By 大塚 宏
 

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;
        }
    }
}
 

END

你可能感興趣的文章

加密解密(AES)、動態產生IV 加密解密(AES)、動態產生IV

[C#] 字串加密後寫入圖片 字串加密後寫入圖片

[C#] 加密圖片,透過Logistic混沌演算法 加密圖片,透過Logistic混沌演算法

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[VB.NET] A=B小遊戲(附原始碼) A=B小遊戲(附原始碼)

HTML-語意化標籤練習 2022/08/19代辦事項 看課外書 udemy線上課程 發布一篇文章

getCoinInfo(API測試) getCoinInfo

計算友好數 var counter = 1; function find(num){ var a = num; var atotal

寫給新安東京董事長的祝賀詞 第一次挑戰寫日文敬語,記錄一下 親愛なる志摩会長へ: 志摩会長、入社以来一年間、本当にお世話になりました。 新安と共に厳