.:: Bitmap Pixel Data Embedding ::.
Store secret messages inside the pixels of a BMP or PNG file
This program allows the user to store text data inside of the pixels of an image. The text data is converted to a binary number string. Then it is stored in the pixels by setting the red color channel to either an even value or an odd value. Even represents a binary 1 and odd represents a binary 0. To convert an odd to even or even to odd, a 1 is added to the red channel in a pixel.
Here is a PNG file with an encoded message...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace CodeRed { class Program { static bool IsEven(int val) { bool ret = false; if (((double)val / 2) == Math.Round((double)val / 2)) { ret = true; } return ret; } static string BinToLetters(string bin) { string ret = ""; for (int i = 0; i < bin.Length; i += 8) { string byt = bin.Substring(i, 8); int result = 0; bool bit1 = false; bool bit2 = false; bool bit3 = false; bool bit4 = false; bool bit5 = false; bool bit6 = false; bool bit7 = false; bool bit8 = false; if (byt.Substring(0, 1) == "1") { bit1 = true; } if (byt.Substring(0, 1) == "0") { bit1 = false; } if (byt.Substring(1, 1) == "1") { bit2 = true; } if (byt.Substring(1, 1) == "0") { bit2 = false; } if (byt.Substring(2, 1) == "1") { bit3 = true; } if (byt.Substring(2, 1) == "0") { bit3 = false; } if (byt.Substring(3, 1) == "1") { bit4 = true; } if (byt.Substring(3, 1) == "0") { bit4 = false; } if (byt.Substring(4, 1) == "1") { bit5 = true; } if (byt.Substring(4, 1) == "0") { bit5 = false; } if (byt.Substring(5, 1) == "1") { bit6 = true; } if (byt.Substring(5, 1) == "0") { bit6 = false; } if (byt.Substring(6, 1) == "1") { bit7 = true; } if (byt.Substring(6, 1) == "0") { bit7 = false; } if (byt.Substring(7, 1) == "1") { bit8 = true; } if (byt.Substring(7, 1) == "0") { bit8 = false; } if (bit1 == true) { result += (int)Math.Pow(2, 7); } if (bit2 == true) { result += (int)Math.Pow(2, 6); } if (bit3 == true) { result += (int)Math.Pow(2, 5); } if (bit4 == true) { result += (int)Math.Pow(2, 4); } if (bit5 == true) { result += (int)Math.Pow(2, 3); } if (bit6 == true) { result += (int)Math.Pow(2, 2); } if (bit7 == true) { result += (int)Math.Pow(2, 1); } if (bit8 == true) { result += (int)Math.Pow(2, 0); } ret += (char)result; } return ret; } static string LettersToBin(string str) { string result = ""; foreach (char chr in str) { result += Convert.ToString(chr, 2).PadLeft(8, '0'); } return result.Trim(); } static void Main(string[] args) { while (true) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("(c) 2016 - ROSTISLAV PERSION - BMP/PNG IMAGE EMBEDDER"); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.Write("STORE [S] or DECODE [D] or QUIT [Q]: "); string inpt = Console.ReadLine().Trim().ToUpper(); if (inpt == "Q") { Environment.Exit(0); } if (inpt == "S") { // USER INPUT Console.WriteLine(); Console.Write("SOURCE BMP/PNG FILE: "); string openfile = Console.ReadLine().Trim(); Console.Write("DESTINATION BMP/PNG FILE: "); string openfile2 = Console.ReadLine().Trim(); // LOAD BITMAP Bitmap bmp1 = new Bitmap(openfile); // USER INPUT Console.Write("MESSAGE TO STORE (MAXIMUM CHARACTERS: " + (Math.Floor((double)(bmp1.Width * bmp1.Height) / 8) - 1).ToString() + "): "); string message = Console.ReadLine().Trim(); string bin = LettersToBin(message + "|"); // WILL IT FIT?? if (((bmp1.Width * bmp1.Height) / 8) < (message.Length + 1)) { Console.WriteLine("ERROR: TOO MUCH DATA."); Console.ReadLine(); break; } // PREPARE IMAGE for (int y = 0; y < bmp1.Height; y++) { for (int x = 0; x < bmp1.Width; x++) { Color col1 = bmp1.GetPixel(x, y); int rr = col1.R; int gg = col1.G; int bb = col1.B; if (rr == 255) { rr = 254; } Color col2 = Color.FromArgb(rr, gg, bb); bmp1.SetPixel(x, y, col2); } } int cnt = -1; for (int y = 0; y < bmp1.Height; y++) { for (int x = 0; x < bmp1.Width; x++) { cnt++; Color col = bmp1.GetPixel(x, y); Color newcol = Color.White; if (cnt < bin.Length) { if (bin.Substring(cnt, 1) == "0") // odd { if (IsEven(col.R)) { newcol = col; newcol = Color.FromArgb(col.R + 1, col.G, col.B); } else { newcol = col; } } if (bin.Substring(cnt, 1) == "1") // even { if (IsEven(col.R)) { newcol = col; } else { newcol = col; newcol = Color.FromArgb(col.R + 1, col.G, col.B); } } bmp1.SetPixel(x, y, newcol); } } } bmp1.Save(openfile2); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(); Console.WriteLine("OUTPUT FILE: " + openfile2 + "\n"); Console.WriteLine("TEXT MESSAGE: " + message + "\n"); Console.WriteLine("BINARY MESSAGE: " + bin + "\n"); Console.WriteLine("IMAGE DATA HAS BEEN ENCODED." + "\n"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); Console.WriteLine("PRESS ENTER TO GOT TO MAIN MENU..."); Console.ReadLine(); } if (inpt == "D") { // USER INPUT Console.WriteLine(); Console.Write("DECODE BMP/PNG FILE: "); string openfile = Console.ReadLine().Trim(); // LOAD BITMAP Bitmap bmp1 = new Bitmap(openfile); // FILE SIZE Console.Write("NUMBER OF CHARACTERS TO GET (MAX: " + Math.Floor((double)(bmp1.Width * bmp1.Height) / 8).ToString() + "): "); int numchars = Convert.ToInt32(Console.ReadLine().Trim()); Console.WriteLine(); // WILL IT FIT?? if (numchars > Math.Floor((double)(bmp1.Width * bmp1.Height) / 8)) { Console.WriteLine("ERROR: NUMBER OF CGHARACTERS IS MORE THAN MAXIMUM."); Console.ReadLine(); break; } string bin = ""; int cnt = 0; for (int y = 0; y < bmp1.Height; y++) { for (int x = 0; x < bmp1.Width; x++) { cnt++; if (cnt <= (numchars * 8)) { Color col = bmp1.GetPixel(x, y); if (IsEven(col.R)) { bin += "1"; } else { bin += "0"; } } } } Console.ForegroundColor = ConsoleColor.Yellow; string msgout = BinToLetters(bin) + "|"; msgout = msgout.Substring(0, msgout.IndexOf("|")); Console.WriteLine("MESSAGE: " + msgout); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); Console.WriteLine("PRESS ENTER TO GO TO MAIN MENU..."); Console.ReadLine(); } } } } } |