첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 스테가노그래피 이미지 만들기

----------------------------------------------------------------------------------------------------


TestProject.zip


SteganographyState.cs

 

 

namespace TestProject

{

    /// <summary>

    /// 스테가노그래피 상태

    /// </summary>

    public enum SteganographyState

    {

        /// <summary>

        /// HIDING

        /// </summary>

        HIDING,

 

        /// <summary>

        /// FILLING_WITH_ZEROS

        /// </summary>

        FILLING_WITH_ZEROS

    };

}

 

 

SteganographyHelper.cs

 

 

using System.Drawing;

 

namespace TestProject

{

    /// <summary>

    /// 스테가노그래피 헬퍼

    /// </summary>

    public static class SteganographyHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 비트맵 구하기 - GetBitmap(sourceText, sourceBitmap)

 

        /// <summary>

        /// 비트맵 구하기

        /// </summary>

        /// <param name="sourceText">소스 텍스트</param>

        /// <param name="sourceBitmap">소스 비트맵</param>

        /// <returns>비트맵</returns>

        public static Bitmap GetBitmap(string sourceText, Bitmap sourceBitmap)

        {

            SteganographyState state = SteganographyState.HIDING;

 

            int  characterIndex = 0;

            int  characterValue = 0;

            long colorUnitIndex = 0;

            int  zeros          = 0;

            int  red            = 0;

            int  green          = 0;

            int  blue           = 0;

 

            Bitmap targetBitmap = sourceBitmap.Clone() as Bitmap;

 

            for(int y = 0; y < sourceBitmap.Height; y++)

            {

                for(int x = 0; x < sourceBitmap.Width; x++)

                {

                    Color pixel = sourceBitmap.GetPixel(x, y);

 

                    pixel = Color.FromArgb(pixel.R - pixel.R % 2, pixel.G - pixel.G % 2, pixel.B - pixel.B % 2);

 

                    red   = pixel.R;

                    green = pixel.G;

                    blue  = pixel.B;

 

                    for(int i = 0; i < 3; i++)

                    {

                        if(colorUnitIndex % 8 == 0)

                        {

                            if(zeros == 8)

                            {

                                if((colorUnitIndex - 1) % 3 < 2)

                                {

                                    targetBitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));

                                }

 

                                return targetBitmap;

                            }

 

                            if(characterIndex >= sourceText.Length)

                            {

                                state = SteganographyState.FILLING_WITH_ZEROS;

                            }

                            else

                            {

                                characterValue = sourceText[characterIndex++];

                            }

                        }

 

                        switch(colorUnitIndex % 3)

                        {

                            case 0 :

                            {

                                if(state == SteganographyState.HIDING)

                                {

                                    red += characterValue % 2;

 

                                    characterValue /= 2;

                                }

 

                                break;

                            }

                            case 1 :

                            {

                                if(state == SteganographyState.HIDING)

                                {

                                    green += characterValue % 2;

 

                                    characterValue /= 2;

                                }

 

                                break;

                            }

                            case 2 :

                            {

                                if(state == SteganographyState.HIDING)

                                {

                                    blue += characterValue % 2;

 

                                    characterValue /= 2;

                                }

 

                                targetBitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));

 

                                break;

                            }

                        }

 

                        colorUnitIndex++;

 

                        if(state == SteganographyState.FILLING_WITH_ZEROS)

                        {

                            zeros++;

                        }

                    }

                }

            }

 

            return targetBitmap;

        }

 

        #endregion

        #region 텍스트 구하기 - GetText(sourceBitmap)

 

        /// <summary>

        /// 텍스트 구하기

        /// </summary>

        /// <param name="sourceBitmap">소스 비트맵</param>

        /// <returns>텍스트</returns>

        public static string GetText(Bitmap sourceBitmap)

        {

            int    colorUnitIndex = 0;

            int    characterValue = 0;

            string targetText     = string.Empty;

 

            for(int y = 0; y < sourceBitmap.Height; y++)

            {

                for(int x = 0; x < sourceBitmap.Width; x++)

                {

                    Color pixel = sourceBitmap.GetPixel(x, y);

 

                    for(int i = 0; i < 3; i++)

                    {

                        switch(colorUnitIndex % 3)

                        {

                            case 0 :

                            {

                                characterValue = characterValue * 2 + pixel.R % 2;

 

                                break;

                            }

                            case 1 :

                            {

                                characterValue = characterValue * 2 + pixel.G % 2;

 

                                break;

                            }

                            case 2 :

                            {

                                characterValue = characterValue * 2 + pixel.B % 2;

 

                                break;

                            }

                        }

 

                        colorUnitIndex++;

 

                        if(colorUnitIndex % 8 == 0)

                        {

                            characterValue = ReverseBits(characterValue);

 

                            if(characterValue == 0)

                            {

                                return targetText;

                            }

 

                            char character = (char)characterValue;

 

                            targetText += character.ToString();

                        }

                    }

                }

            }

 

            return targetText;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 비트 반전하기 - ReverseBits(sourceValue)

 

        /// <summary>

        /// 비트 반전하기

        /// </summary>

        /// <param name="sourceValue">소스 값</param>

        /// <returns>비트 반전 값</returns>

        public static int ReverseBits(int sourceValue)

        {

            int targetValue = 0;

 

            for(int i = 0; i < 8; i++)

            {

                targetValue = targetValue * 2 + sourceValue % 2;

 

                sourceValue /= 2;

            }

 

            return targetValue;

        }

 

        #endregion

    }

}

 

 

AESHelper.cs

 

 

using System;

using System.IO;

using System.Text;

using System.Security.Cryptography;

 

namespace TestProject

{

    /// <summary>

    /// AES 헬퍼

    /// </summary>

    public static class AESHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Field

 

        #region Field

 

        /// <summary>

        /// SALT 배열

        /// </summary>

        private static byte[] _saltArray = Encoding.ASCII.GetBytes("123456789abcdefghijkllmn");

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 암호화 하기 - Encrypt(sourceText, key)

 

        /// <summary>

        /// 암호화 하기

        /// </summary>

        /// <param name="sourceText">소스 텍스트</param>

        /// <param name="key"></param>

        /// <returns>암호화 텍스트</returns>

        public static string Encrypt(string sourceText, string key)

        {

            string          targetText      = null;

            RijndaelManaged rijndaelManaged = null;

 

            try

            {

                Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(key, _saltArray);

 

                rijndaelManaged = new RijndaelManaged();

 

                rijndaelManaged.Key = rfc2898DeriveBytes.GetBytes(rijndaelManaged.KeySize / 8);

 

                ICryptoTransform cryptoTransform = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);

 

                using(MemoryStream memoryStream = new MemoryStream())

                {

                    memoryStream.Write(BitConverter.GetBytes(rijndaelManaged.IV.Length), 0, sizeof(int));

 

                    memoryStream.Write(rijndaelManaged.IV, 0, rijndaelManaged.IV.Length);

 

                    using(CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))

                    {

                        using(StreamWriter streamWriter = new StreamWriter(cryptoStream))

                        {

                            streamWriter.Write(sourceText);

                        }

                    }

 

                    targetText = Convert.ToBase64String(memoryStream.ToArray());

                }

            }

            finally

            {

                if(rijndaelManaged != null)

                {

                    rijndaelManaged.Clear();

                }

            }

 

            return targetText;

        }

 

        #endregion

        #region 복호화 하기 - Decrypt(sourceText, key)

 

        /// <summary>

        /// 복호화 하기

        /// </summary>

        /// <param name="sourceText">소스 텍스트</param>

        /// <param name="key"></param>

        /// <returns>복호화 텍스트</returns>

        public static string Decrypt(string sourceText, string key)

        {

            RijndaelManaged rijndaelManaged = null;

            string          targetText      = null;

 

            try

            {

                Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(key, _saltArray);

 

                byte[] sourceByteArray = Convert.FromBase64String(sourceText);

 

                using(MemoryStream memoryStream = new MemoryStream(sourceByteArray))

                {

                    rijndaelManaged = new RijndaelManaged();

 

                    rijndaelManaged.Key = rfc2898DeriveBytes.GetBytes(rijndaelManaged.KeySize / 8);

                    rijndaelManaged.IV  = ReadByteArray(memoryStream);

 

                    ICryptoTransform dryptoTransform = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);

 

                    using(CryptoStream cryptoStream = new CryptoStream(memoryStream, dryptoTransform, CryptoStreamMode.Read))

                    {

                        using(StreamReader streamReader = new StreamReader(cryptoStream))

                        {

                            targetText = streamReader.ReadToEnd();

                        }

                    }

                }

            }

            finally

            {

                if(rijndaelManaged != null)

                {

                    rijndaelManaged.Clear();

                }

            }

 

            return targetText;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 바이트 배열 읽기 - ReadByteArray(stream)

 

        /// <summary>

        /// 바이트 배열 읽기

        /// </summary>

        /// <param name="stream">스트림</param>

        /// <returns>바이트 배열</returns>

        private static byte[] ReadByteArray(Stream stream)

        {

            byte[] rawLengthArray = new byte[sizeof(int)];

 

            if(stream.Read(rawLengthArray, 0, rawLengthArray.Length) != rawLengthArray.Length)

            {

                throw new SystemException();

            }

 

            byte[] bufferArray = new byte[BitConverter.ToInt32(rawLengthArray, 0)];

 

            if(stream.Read(bufferArray, 0, bufferArray.Length) != bufferArray.Length)

            {

                throw new SystemException();

            }

 

            return bufferArray;

        }

 

        #endregion

    }

}

 

 

MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 소스 비트맵

        /// </summary>

        private Bitmap sourceBitmap = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 열기 메뉴 항목 클릭시 처리하기 - openMenuItem_Click(sender, e)

 

        /// <summary>

        /// 열기 메뉴 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void openMenuItem_Click(object sender, EventArgs e)

        {

            if(this.openFileDialog.ShowDialog() == DialogResult.OK)

            {

                this.pictureBox.Image = Image.FromFile(this.openFileDialog.FileName);

 

                this.encryptButton.Enabled = true;

                this.decryptButton.Enabled = true;

            }

        }

 

        #endregion

        #region 종료 메뉴 항목 클릭시 처리하기 - exitMenuItem_Click(sender, e)

 

        /// <summary>

        /// 종료 메뉴 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void exitMenuItem_Click(object sender, EventArgs e)

        {

            Close();

        }

 

        #endregion

 

        #region 암호화 버튼 클릭시 처리하기 - encryptButton_Click(sender, e)

 

        /// <summary>

        /// 암호화 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void encryptButton_Click(object sender, EventArgs e)

        {

            if(this.pictureBox.Image == null)

            {

                MessageBox.Show(this, "이미지 파일을 선택해 주시기 바랍니다.", "INFORMATION", MessageBoxButtons.OK,

                    MessageBoxIcon.Information);

 

                return;

            }

 

            this.sourceBitmap = this.pictureBox.Image as Bitmap;

 

            string sourceText = this.textBox.Text;

 

            if(string.IsNullOrWhiteSpace(sourceText))

            {

                MessageBox.Show(this, "텍스트를 입력해 주시기 바랍니다.", "INFORMATION", MessageBoxButtons.OK,

                   MessageBoxIcon.Information);

 

                this.textBox.Focus();

 

                return;

            }

 

            if(this.keyTextBox.Text.Length < 8)

            {

                MessageBox.Show(this, "비밀키는 8자리 이상 입력해 주시기 바랍니다.", "INFORMATION", MessageBoxButtons.OK, 

                    MessageBoxIcon.Information);

 

                this.keyTextBox.Focus();

 

                return;

            }

            else

            {

                sourceText = AESHelper.Encrypt(sourceText, this.keyTextBox.Text);

            }

 

            sourceBitmap = SteganographyHelper.GetBitmap(sourceText, sourceBitmap);

 

            if(this.saveFileDialog.ShowDialog() == DialogResult.OK)

            {

                switch(this.saveFileDialog.FilterIndex)

                {

                    case 0 :

                    {

                        this.sourceBitmap.Save(this.saveFileDialog.FileName, ImageFormat.Png);

 

                        break;

                    }

                    case 1 :

                    {

                        this.sourceBitmap.Save(this.saveFileDialog.FileName, ImageFormat.Bmp);

 

                        break;

                    }

                }

            }

 

            ClearControlData();

        }

 

        #endregion

        #region 복호화 버튼 클릭시 처리하기 - decryptButton_Click(sender, e)

 

        /// <summary>

        /// 복호화 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void decryptButton_Click(object sender, EventArgs e)

        {

            this.sourceBitmap = this.pictureBox.Image as Bitmap;

 

            string text = SteganographyHelper.GetText(sourceBitmap);

 

            try

            {

                text = AESHelper.Decrypt(text, this.keyTextBox.Text);

            }

            catch

            {

                MessageBox.Show(this, "비밀키가 일치하지 않습니다.", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information);

 

                this.keyTextBox.Focus();

 

                return;

            }

 

            this.textBox.Text = text;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 컨트롤 데이터 지우기 - ClearControlData()

 

        /// <summary>

        /// 컨트롤 데이터 지우기

        /// </summary>

        private void ClearControlData()

        {

            this.pictureBox.Image = null;

 

            this.textBox.Clear();

 

            this.keyTextBox.Clear();

 

            this.encryptButton.Enabled = false;

            this.decryptButton.Enabled = false;

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요