[精讚] [會員登入]
150

[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則留言。

訪客留言

[無留言]

隨機好文

[實戰分享] 投資LUNA,一天能獲利多少? 實戰過程-與LUNA廝殺一天(3/9 22點~3/10 23點)的成果

[新手教學] 虛擬貨幣新手入門指南 與其說是指南,不如說是我自己越寫越雜的筆記,若有問題歡迎留言指教

寶可夢-朱紫感想 朱紫的道館館主們,絕大部分都是"兼職",沒有全職道館主 例如,普通系館主,是個普通的上班族 電系道館

[對戰筆記] 巧遇伊布統一隊 朱紫的第一篇對戰文 剛好遇到伊布戰隊,非常有趣的陣容,就來分享啦~ 對手排名 看到對方名次,直接嚇到,1萬名以內啊 通常

[寶可夢朱紫] Vtuber道館有獎挑戰賽 [活動分享] Vtuber道館有獎挑戰賽,開幕啦! 有興趣挑戰的訓練家們 請於每周二11:00~23:00 至「緋尹夜は