Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9.  
  10. namespace SteganographyTest
  11. {
  12. class Program
  13. {
  14. static void Main()
  15. {
  16. byte[] hiddenBytes = Util.BitmapToByteArray(Image.FromFile("hidden.png"));
  17. Encode(hiddenBytes, "innocuous.png", "encoded.png");
  18. byte[] loadedHiddenBytes = Decode("encoded.png");
  19. Util.ByteArrayToBitmap(loadedHiddenBytes).Save("decoded.png", ImageFormat.Png);
  20. CreateMask("innocuous.png", "encoded.png");
  21. }
  22.  
  23. public static void Encode(byte[] hiddenBytes, string inputImageFileName, string outputImageFileName)
  24. {
  25. // Loading the data we want to hide to a byte array
  26. byte[] hiddenLengthBytes = BitConverter.GetBytes(hiddenBytes.Length);
  27. byte[] hiddenCombinedBytes = Util.Combine(hiddenLengthBytes, hiddenBytes);
  28.  
  29. // Loading an innocuous image we want to store the hidden data in to a byte array
  30. Image innocuousBmp = Image.FromFile(inputImageFileName);
  31. byte[] rgbComponents = Util.RgbComponentsToBytes(innocuousBmp);
  32.  
  33. // Encoding the hidden data into the innocuous image, and storing it to file.
  34. byte[] encodedRgbComponents = EncodeBytes(hiddenCombinedBytes, rgbComponents);
  35. Bitmap encodedBmp = Util.ByteArrayToBitmap(encodedRgbComponents, innocuousBmp.Width, innocuousBmp.Height);
  36. encodedBmp.Save(outputImageFileName, ImageFormat.Png);
  37. }
  38.  
  39. private static byte[] EncodeBytes(byte[] hiddenBytes, byte[] innocuousBytes)
  40. {
  41. BitArray hiddenBits = new BitArray(hiddenBytes);
  42. byte[] encodedBitmapRgbComponents = new byte[innocuousBytes.Length];
  43. for (int i = 0; i < innocuousBytes.Length; i++)
  44. {
  45. if (i < hiddenBits.Length)
  46. {
  47. byte evenByte = (byte)(innocuousBytes[i] - innocuousBytes[i] % 2);
  48. encodedBitmapRgbComponents[i] = (byte)(evenByte + (hiddenBits[i] ? 1 : 0));
  49. }
  50. else
  51. {
  52. encodedBitmapRgbComponents[i] = innocuousBytes[i];
  53. }
  54. }
  55. return encodedBitmapRgbComponents;
  56. }
  57.  
  58. public static byte[] Decode(string imageFileName)
  59. {
  60. // Loading the seemingly innocuous image with hidden data into a byte array
  61. Bitmap loadedEncodedBmp = new Bitmap(imageFileName);
  62. byte[] loadedEncodedRgbComponents = Util.RgbComponentsToBytes(loadedEncodedBmp);
  63.  
  64. const int bytesInInt = 4;
  65. byte[] loadedHiddenLengthBytes = DecodeBytes(loadedEncodedRgbComponents, 0, bytesInInt);
  66. int loadedHiddenLength = BitConverter.ToInt32(loadedHiddenLengthBytes, 0);
  67. byte[] loadedHiddenBytes = DecodeBytes(loadedEncodedRgbComponents, bytesInInt, loadedHiddenLength);
  68. return loadedHiddenBytes;
  69. }
  70.  
  71. private static byte[] DecodeBytes(byte[] innocuousLookingData, int byteIndex, int byteCount)
  72. {
  73. const int bitsInBytes = 8;
  74. int bitCount = byteCount * bitsInBytes;
  75. int bitIndex = byteIndex * bitsInBytes;
  76. bool[] loadedHiddenBools = new bool[bitCount];
  77. for (int i = 0; i < bitCount; i++)
  78. {
  79. loadedHiddenBools[i] = innocuousLookingData[i + bitIndex] % 2 == 1;
  80. }
  81. BitArray loadedHiddenBits = new BitArray(loadedHiddenBools);
  82. byte[] loadedHiddenBytes = new byte[loadedHiddenBits.Length / bitsInBytes];
  83. loadedHiddenBits.CopyTo(loadedHiddenBytes, 0);
  84. return loadedHiddenBytes;
  85. }
  86.  
  87. public static void CreateMask(string inputImageFileName1, string inputImageFileName2)
  88. {
  89. Image image1 = Image.FromFile(inputImageFileName1);
  90. Image image2 = Image.FromFile(inputImageFileName2);
  91. Bitmap bmp1 = new Bitmap(image1);
  92. Bitmap bmp2 = new Bitmap(image2);
  93. Bitmap maskDiff = new Bitmap(bmp1);
  94. Bitmap maskParity1 = new Bitmap(bmp1);
  95. Bitmap maskParity2 = new Bitmap(bmp2);
  96.  
  97. for (int i = 0; i < maskDiff.Height; i++)
  98. {
  99. for (int j = 0; j < maskDiff.Width; j++)
  100. {
  101. Color px1 = bmp1.GetPixel(j, i);
  102. Color px2 = bmp2.GetPixel(j, i);
  103.  
  104. int maskDiffIntensity = 255 - Math.Abs(px2.R - px1.R) * 85 - Math.Abs(px2.G - px1.G) * 85 - Math.Abs(px2.B - px1.B) * 85;
  105. maskDiff.SetPixel(j, i, Color.FromArgb(maskDiffIntensity, maskDiffIntensity, maskDiffIntensity));
  106.  
  107. int maskParityIntensity1 = (px1.R % 2) * 85 + (px1.G % 2) * 85 + (px1.B % 2) * 85;
  108. maskParity1.SetPixel(j, i, Color.FromArgb(maskParityIntensity1, maskParityIntensity1, maskParityIntensity1));
  109.  
  110. int maskParityIntensity2 = (px2.R % 2) * 85 + (px2.G % 2) * 85 + (px2.B % 2) * 85;
  111. maskParity2.SetPixel(j, i, Color.FromArgb(maskParityIntensity2, maskParityIntensity2, maskParityIntensity2));
  112. }
  113. }
  114.  
  115. maskDiff.Save("maskDiff.png");
  116. maskParity1.Save("maskParity_" + inputImageFileName1);
  117. maskParity2.Save("maskParity_" + inputImageFileName2);
  118.  
  119. }
  120. }
  121.  
  122. class Util
  123. {
  124. public static byte[] BitmapToByteArray(Image img)
  125. {
  126. using (MemoryStream ms = new MemoryStream())
  127. {
  128. img.Save(ms, ImageFormat.Png);
  129. return ms.ToArray();
  130. }
  131. }
  132.  
  133. public static Image ByteArrayToBitmap(byte[] bytes)
  134. {
  135. using (MemoryStream ms = new MemoryStream(bytes))
  136. {
  137. return Image.FromStream(ms);
  138. }
  139. }
  140.  
  141. public static byte[] Combine(byte[] left, byte[] right)
  142. {
  143. byte[] combined = new byte[left.Length + right.Length];
  144. Buffer.BlockCopy(left, 0, combined, 0, left.Length);
  145. Buffer.BlockCopy(right, 0, combined, left.Length, right.Length);
  146. return combined;
  147. }
  148.  
  149. public static byte[] RgbComponentsToBytes(Image innocuousImg)
  150. {
  151. Bitmap innocuousBmp = new Bitmap(innocuousImg);
  152. int counter = 0;
  153. byte[] components = new byte[3 * innocuousBmp.Width * innocuousBmp.Height];
  154. for (int y = 0; y < innocuousBmp.Height; y++)
  155. {
  156. for (int x = 0; x < innocuousBmp.Width; x++)
  157. {
  158. Color c = innocuousBmp.GetPixel(x, y);
  159. components[counter++] = c.R;
  160. components[counter++] = c.G;
  161. components[counter++] = c.B;
  162. }
  163. }
  164. return components;
  165. }
  166.  
  167. public static Bitmap ByteArrayToBitmap(byte[] rgbComponents, int width, int hight)
  168. {
  169. Queue<byte> rgbComponentQueue = new Queue<byte>(rgbComponents);
  170. Bitmap bitmap = new Bitmap(width, hight);
  171. for (int y = 0; y < hight; y++)
  172. {
  173. for (int x = 0; x < width; x++)
  174. {
  175. bitmap.SetPixel(x, y, Color.FromArgb(rgbComponentQueue.Dequeue(), rgbComponentQueue.Dequeue(), rgbComponentQueue.Dequeue()));
  176. }
  177. }
  178. return bitmap;
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement