Advertisement
Razali

ImageSteganoraphy

Aug 11th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7.  
  8. namespace Image_Steganography
  9. {
  10.     class ImageSteganoraphy
  11.     {
  12.  
  13.         #region Variables
  14.         private Bitmap convertedBitmapImage;
  15.         private Bitmap encodedBitmapImage;
  16.         #endregion
  17.  
  18.         #region Properties
  19.  
  20.         public Image OriginalImage { get; set; }
  21.  
  22.         public Bitmap BitmapImage { get{ return convertedBitmapImage;}}
  23.  
  24.         public Bitmap EncodedBitmapImage { get { return encodedBitmapImage; } }
  25.         #endregion
  26.  
  27.  
  28.         #region Constructor
  29.         public ImageSteganoraphy(String sourcePath)
  30.         {
  31.  
  32.             // 1)Get the original Image from a file path
  33.             try
  34.             {
  35.                 OriginalImage = Image.FromFile(sourcePath);
  36.             }
  37.             catch(Exception e)
  38.             {
  39.                 throw new Exception(e.Message.ToString());
  40.             }
  41.  
  42.             // 2)Convert the image to Bitmap
  43.             convertedBitmapImage = new Bitmap(OriginalImage);
  44.  
  45.         }
  46.         #endregion
  47.  
  48.         #region Methods
  49.  
  50.         private Color GetPixel(Bitmap imageToProcess, int x, int y)
  51.         {
  52.             return imageToProcess.GetPixel(x, y);
  53.         }
  54.  
  55.         private int GetRedValue(Color pixel)
  56.         {
  57.             return pixel.R;
  58.         }
  59.         private int GetGreenValue(Color pixel)
  60.         {
  61.             return pixel.G;
  62.         }
  63.         private int GetBlueValue(Color pixel)
  64.         {
  65.            
  66.             return pixel.B;
  67.         }
  68.  
  69.         private Color CreateNewPixel(int redValue, int greenValue, int blueValue)
  70.         {
  71.             return Color.FromArgb(redValue, greenValue, blueValue);
  72.         }
  73.  
  74.         private Color GetBitmapPixel(Bitmap imageToProcess, int x, int y)
  75.         {
  76.             return imageToProcess.GetPixel(x, y);
  77.         }
  78.  
  79.         private void SetBitmapPixel(Bitmap imageToProcess, int x, int y,int redValue, int greenValue, int blueValue)
  80.         {
  81.             Color newPixel = CreateNewPixel(redValue, greenValue, blueValue);
  82.             imageToProcess.SetPixel(x, y, newPixel);
  83.         }
  84.         private void SetBitmapPixel(Bitmap imageToProcess,int x, int y, Color pixel)
  85.         {
  86.             imageToProcess.SetPixel(x, y, pixel);
  87.         }
  88.  
  89.         public void Encode(String encodeString, String fileName)
  90.         {
  91.             /* 1)Convert String to Hex
  92.              * Example: "Hello" ->"48656C6C6F"
  93.              *
  94.              * */
  95.             String hexString = HexMap.StringToHex(encodeString);
  96.  
  97.             /*2) Get row col pairs of each hex character from hex map
  98.              * ----------- HEX MAP ------------
  99.              *      col 0   1   2   3
  100.              *  row 0 {'1','2','3','4'},
  101.                 row 1 {'5','6','7','8'},
  102.                 row 2 {'9','0','A','B'},
  103.                 row 3 {'C','D','E','F'}
  104.              * */
  105.             int[,] rowcol = HexMap.HexToRowCol(hexString);
  106.  
  107.             encodedBitmapImage = EncodeBitmap(convertedBitmapImage, rowcol);
  108.  
  109.             //Save bitmap
  110.             encodedBitmapImage.Save(fileName);
  111.         }
  112.  
  113.         private Bitmap EncodeBitmap(Bitmap imageToProcess, int[,] rowcol)
  114.         {
  115.             int rowcolIndex = 0;
  116.             int width=0, height=0;
  117.  
  118.             //Encode all rowcol to bitmap
  119.             for(width = 0; width<= imageToProcess.Width; width++)
  120.             {
  121.                 for(height = 0; height <= imageToProcess.Height; height++)
  122.                 {
  123.                     Color pixelToEncode = GetBitmapPixel(imageToProcess, width, height);
  124.  
  125.                     //ENCODING 1 HEX ROW COL PAIR TO RED & GREEN
  126.                     int redValue = GetRedValue(pixelToEncode);
  127.                     int newRedValue = ChangeLastDigit(rowcol[rowcolIndex, 0], redValue);
  128.  
  129.                     int greenValue = GetGreenValue(pixelToEncode);
  130.                     int newGreenValue = ChangeLastDigit(rowcol[rowcolIndex, 1], greenValue);
  131.  
  132.                     Color newPixel = Color.FromArgb(newRedValue, newGreenValue, GetBlueValue(pixelToEncode));
  133.                     SetBitmapPixel(imageToProcess, width, height, newPixel);
  134.  
  135.                     rowcolIndex++;
  136.  
  137.                     //Finished Encoding
  138.                     if (rowcolIndex == rowcol.Length/2)
  139.                         break;
  140.                 }
  141.  
  142.                 //Finished Encoding
  143.                 if (rowcolIndex == rowcol.Length / 2)
  144.                     break;
  145.             }
  146.  
  147.             //Encoding trailing bits to demark end of encoding
  148.            
  149.              //* Trailing bits have last digits of rgb value as 1, 2, 3, 4, 5, 6
  150.              //* Example
  151.              //* Pixel 1: Red 221    ->1
  152.              //* Pixel 1: Green 122  ->2
  153.              //* Pixel 1: Blue 003   ->3
  154.              //* Pixel 2: Red 224    ->4
  155.              //* Pixel 2: Green 135  ->5
  156.              //* Pixel 2: Blue 216   ->6
  157.              //* Hence i have used 2 pixels to denote end of encoding
  158.  
  159.             if (height + 1 >= imageToProcess.Height)
  160.             {
  161.                 height = 0;
  162.                 width++;
  163.             }
  164.             else
  165.             {
  166.                 height++;
  167.             }
  168.  
  169.             Color _pixelToEncode = GetBitmapPixel(imageToProcess, width, height);
  170.             int red = _pixelToEncode.R;
  171.             int newRed = ChangeLastDigit(1, red);
  172.  
  173.             int green = _pixelToEncode.G;
  174.             int newGreen = ChangeLastDigit(2, green);
  175.  
  176.             int blue = _pixelToEncode.B;
  177.             int newBlue = ChangeLastDigit(3, blue);
  178.  
  179.             Color _newPixel = Color.FromArgb(newRed, newGreen, newBlue);
  180.             SetBitmapPixel(imageToProcess, width, height, _newPixel);
  181.  
  182.             if (height + 1 >= imageToProcess.Height)
  183.             {
  184.                 height = 0;
  185.                 width++;
  186.             }
  187.             else
  188.             {
  189.                 height++;
  190.             }
  191.  
  192.             _pixelToEncode = GetBitmapPixel(imageToProcess, width, height);
  193.             red = _pixelToEncode.R;
  194.             newRed = ChangeLastDigit(4, red);
  195.  
  196.             green = _pixelToEncode.G;
  197.             newGreen = ChangeLastDigit(5, green);
  198.  
  199.             blue = _pixelToEncode.B;
  200.             newBlue = ChangeLastDigit(5, blue);
  201.  
  202.             _newPixel = Color.FromArgb(newRed, newGreen, newBlue);
  203.             SetBitmapPixel(imageToProcess, width, height, _newPixel);
  204.  
  205.  
  206.             //Save bitmap
  207.             return imageToProcess;
  208.         }
  209.         private bool IsTrailingPixel(Bitmap img, int row, int col)
  210.         {
  211.             Color pixel_1 = GetBitmapPixel(img, row, col);
  212.  
  213.             int red, blue, green;
  214.             red = GetRedValue(pixel_1);
  215.             green = GetGreenValue(pixel_1);
  216.             blue = GetBlueValue(pixel_1);
  217.  
  218.             //1, 2, 3 found, next find 4, 5, 6
  219.             if(GetLastDigit(red) == 1 && GetLastDigit(green) == 2 && GetLastDigit(blue) == 3)
  220.             {
  221.                 if (col + 1 >= img.Height)
  222.                 {
  223.                     col = 0;
  224.                     row++;
  225.                 }
  226.                 else
  227.                 {
  228.                     col++;
  229.                 }
  230.                 Color pixel_2 = GetBitmapPixel(img, row, col);
  231.  
  232.                 red = GetRedValue(pixel_2);
  233.                 green = GetGreenValue(pixel_2);
  234.                 blue = GetBlueValue(pixel_2);
  235.                
  236.                 if(GetLastDigit(red) == 4 && GetLastDigit(green) == 5 && GetLastDigit(blue) == 5)
  237.                 {
  238.                     return true;
  239.                 }
  240.             }
  241.             return false;
  242.         }
  243.         public String Decode(String imgPath)
  244.         {
  245.             int width=0, height=0;
  246.             String decodedHex = String.Empty;
  247.  
  248.             Bitmap img = new Bitmap(imgPath);
  249.             //Decode all rowcol to bitmap
  250.             for (width = 0; width <= img.Width; width++)
  251.             {
  252.                 for (height = 0; height <= img.Height; height++)
  253.                 {
  254.                     if(IsTrailingPixel(img,width,height))
  255.                     {
  256.                         return  HexMap.HexToString(decodedHex);
  257.                     }
  258.  
  259.                     Color pixel = img.GetPixel(width, height);
  260.                     int row = GetLastDigit(GetRedValue(pixel));
  261.                     int col = GetLastDigit(GetGreenValue(pixel));
  262.                     decodedHex += HexMap.RCTOHEX(row, col);
  263.                    
  264.                 }
  265.             }
  266.  
  267.             return  HexMap.HexToString(decodedHex);
  268.         }
  269.         private int ChangeLastDigit(int digit, int value)
  270.         {
  271.             return value / 10 * 10 + digit;
  272.         }
  273.         private int GetLastDigit(int value)
  274.         {
  275.             return value % 10;
  276.         }
  277.         #endregion
  278.     }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement