⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 基于LSB算法的信息隐藏技术。
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LSB_Algorithm
{
    public partial class Form1 : Form
    {
        //用以保存最大可隐藏的信息大小
        private long _maxInfoSize = 0;

        public Form1()
        {
            InitializeComponent();
        }

        #region 以下为信息隐藏TabPage的事件处理

        /// <summary>
        /// 选择作为载体的BMP图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void browsepic_btn_Click(object sender, EventArgs e)
        {
            OpenFileDialog filedlg = new OpenFileDialog();
            filedlg.Filter = "24位位图 (*.bmp)|*.bmp";
            if (filedlg.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(filedlg.FileName, FileMode.Open, FileAccess.Read);
                using (Bitmap bmp = new Bitmap(fs))
                {
                    if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                    {
                        MessageBox.Show("选择的图片并不是24位位图!", "LSB",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        _maxInfoSize = (fs.Length - 54) / 4 - 3;
                        picpath_tb.Text = filedlg.FileName;
                       // picsize_lbl.Text = string.Format("{0:N0} 字节", fs.Length);
                       // maxinfo_lbl.Text = string.Format("{0:N0} 字节", _maxInfoSize);
                    }
                }
                fs.Close();
            }
        }

        /// <summary>
        /// 选择待隐藏的文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void browsehiding_tb_Click(object sender, EventArgs e)
        {
            OpenFileDialog filedlg = new OpenFileDialog();
            filedlg.Filter = "所有文件 (*.*)|*.*";
            if (filedlg.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(filedlg.FileName, FileMode.Open, FileAccess.Read);
                if (fs.Length > _maxInfoSize)
                {
                    MessageBox.Show(string.Format("所选文件过大!\n最大可隐藏文件大小为:{0} 字节", _maxInfoSize), "LSB",
                                                  MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    hiding_tb.Text = filedlg.FileName;
                   // infosize_lbl.Text = string.Format("{0:N0} 字节", fs.Length);
                }
                fs.Close();
            }
        }

        /// <summary>
        /// 生成包含隐藏信息的图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void generate_btn_Click(object sender, EventArgs e)
        {
            if (picpath_tb.Text == string.Empty)
            {
                MessageBox.Show("请选择载体图像!", "LSB",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (picpath_tb.Text == string.Empty)
            {
                MessageBox.Show("请选择要隐藏的文件!", "LSB",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //备份原始文件
            string backupFile = string.Format(@"{0}\{1}_original.bmp", Path.GetDirectoryName(picpath_tb.Text), Path.GetFileNameWithoutExtension(picpath_tb.Text));
            File.Copy(picpath_tb.Text, backupFile, true);
            //生成图像
            LSBEncrypt lsb = new LSBEncrypt(picpath_tb.Text, hiding_tb.Text);
            lsb.ExecuteEncrypt();

            MessageBox.Show("已成功生成含有隐藏信息的图像!", "LSB",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion


        #region 以下为信息提取TabPage的事件处理

        /// <summary>
        /// 选择需要解码的BMP图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectPic_btn_Click(object sender, EventArgs e)
        {
            OpenFileDialog filedlg = new OpenFileDialog();
            filedlg.Filter = "24位位图 (*.bmp)|*.bmp";
            if (filedlg.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(filedlg.FileName, FileMode.Open, FileAccess.Read);
                using (Bitmap bmp = new Bitmap(fs))
                {
                    if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                    {
                        MessageBox.Show("选择的图片并不是24位位图!", "LSB",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        originalPic_tb.Text = filedlg.FileName;
                        //bmpSize_lbl.Text = string.Format("{0:N0} 字节", fs.Length);
                    }
                }
                fs.Close();
            }
        }

        /// <summary>
        /// 选择隐藏信息要保存到的路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectSavePath_btn_Click(object sender, EventArgs e)
        {
            SaveFileDialog filedlg = new SaveFileDialog();
            filedlg.Filter = "所有文件 (*.*)|*.*";
            if (filedlg.ShowDialog() == DialogResult.OK)
            {
                savePath_tb.Text = filedlg.FileName;
            }
        }

        /// <summary>
        /// 提取隐藏信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void extract_btn_Click(object sender, EventArgs e)
        {
            if (originalPic_tb.Text == string.Empty)
            {
                MessageBox.Show("请选择要提取隐藏信息的图像!", "LSB",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (savePath_tb.Text == string.Empty)
            {
                MessageBox.Show("请选择隐藏信息的保存路径!", "LSB",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            LSBDecrypt decrypt = new LSBDecrypt(originalPic_tb.Text, savePath_tb.Text);
            if (decrypt.ExecuteDecrypt())
            {
                MessageBox.Show("已成功提取隐藏信息!", "LSB",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("所选择的图像似乎不包含任何隐藏信息!", "LSB",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        #endregion

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -