public static string GetDecryptedTextFromImage(Image image) { Bitmap img = new Bitmap(image); int maxLinear = image.Width * image.Height; SeedRNG generator = new SeedRNG((maxLinear + image.Width).GetHashCode(), maxLinear); OutputConsole.Write("Seed generated"); string pass = string.Format("{0}x{1}={2}", image.Width, image.Height, maxLinear); AESEncrypt encrypt = new AESEncrypt(); string text = string.Empty; int value=0; OutputConsole.Write("Processing image..."); do { Point point = LinearIndexToPoint(generator.Next, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); if (value != 0) text += Convert.ToChar(value); } while (value != 0); try { OutputConsole.Write(string.Format("String found: \n{0}", text)); OutputConsole.Write("Decrypting text..."); return encrypt.DecryptString(text, pass); } catch (Exception e) { OutputConsole.Write("Error: Text not found"); Console.WriteLine(e.Message); return null; } }
private static void EncodeMessage(ref Bitmap img, string text) { int maxLinear = img.Width * img.Height; SeedRNG generator = new SeedRNG((maxLinear + img.Width).GetHashCode(), maxLinear); OutputConsole.Write("Seed generated"); string pass = string.Format("{0}x{1}={2}", img.Width, img.Height, maxLinear); AESEncrypt encrypt = new AESEncrypt(); string encrypted = encrypt.EncryptString(text, pass); OutputConsole.Write(string.Format("Text encrypted \n{0}", encrypted)); OutputConsole.Write("Processing image..."); if (encrypted.Length < maxLinear) { for (int i = 0; i < encrypted.Length; i++) { Point point = LinearIndexToPoint(generator.Next, img.Width, img.Height); Color pixel = img.GetPixel(point.X, point.Y); char letter = encrypted[i]; int value = Convert.ToInt32(letter); Color n = EncodePixel(pixel, value); img.SetPixel(point.X, point.Y, n); } //Null value placed at the end to indicate end of text Point pointEnd = LinearIndexToPoint(generator.Next, img.Width, img.Height); Color pixelEnd = img.GetPixel(pointEnd.X, pointEnd.Y); img.SetPixel(pointEnd.X, pointEnd.Y, EncodePixel(pixelEnd, 0)); OutputConsole.Write("Finished embedding encrypted text"); } else { OutputConsole.Write("Error: Image size doesn't support encrypted text size"); img = null; } }
public static string GetDecryptedTextFromImage(Image image) { Bitmap img = new Bitmap(image); int maxLinear = image.Width * image.Height; SeedRNG generator = new SeedRNG((maxLinear + image.Width).GetHashCode(), maxLinear); OutputConsole.Write("Seed generated"); string pass = string.Format("{0}x{1}={2}", image.Width, image.Height, maxLinear); AESEncrypt encrypt = new AESEncrypt(); string text = string.Empty; int value = 0; OutputConsole.Write("Processing image..."); do { Point point = LinearIndexToPoint(generator.Next, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); if (value != 0) { text += Convert.ToChar(value); } } while (value != 0); try { OutputConsole.Write(string.Format("String found: \n{0}", text)); OutputConsole.Write("Decrypting text..."); return(encrypt.DecryptString(text, pass)); } catch (Exception e) { OutputConsole.Write("Error: Text not found"); Console.WriteLine(e.Message); return(null); } }
private static void EncodeMessage(ref Bitmap img, string text) { int maxLinear = img.Width * img.Height; SeedRNG generator = new SeedRNG((maxLinear + img.Width).GetHashCode(), maxLinear); OutputConsole.Write("Seed generated"); string pass = string.Format("{0}x{1}={2}", img.Width, img.Height, maxLinear); AESEncrypt encrypt = new AESEncrypt(); string encrypted = encrypt.EncryptString(text, pass); OutputConsole.Write(string.Format("Text encrypted \n{0}",encrypted)); OutputConsole.Write("Processing image..."); if (encrypted.Length < maxLinear) { for (int i = 0; i < encrypted.Length; i++) { Point point = LinearIndexToPoint(generator.Next, img.Width, img.Height); Color pixel = img.GetPixel(point.X, point.Y); char letter = encrypted[i]; int value = Convert.ToInt32(letter); Color n = EncodePixel(pixel,value); img.SetPixel(point.X, point.Y, n); } //Null value placed at the end to indicate end of text Point pointEnd = LinearIndexToPoint(generator.Next, img.Width, img.Height); Color pixelEnd = img.GetPixel(pointEnd.X, pointEnd.Y); img.SetPixel(pointEnd.X, pointEnd.Y, EncodePixel(pixelEnd, 0)); OutputConsole.Write("Finished embedding encrypted text"); } else { OutputConsole.Write("Error: Image size doesn't support encrypted text size"); img = null; } }
private static void EncodeFileWithColor2(ref Bitmap img, byte[] file, string filename) { Bitmap backup = img.Clone() as Bitmap; int maxLinear = img.Width * img.Height; OutputConsole.Write(string.Format("File size: {0}", FileSizeFormatProvider.GetFileSize(file.Length))); SeedRNG generator = new SeedRNG((maxLinear + img.Width).GetHashCode(), maxLinear, true); OutputConsole.Write("Seed generated"); int extraBytes = 2 + filename.Length + file.Length.ToString().Length; HiddenFile f = new HiddenFile(file, filename); f.cipherFile((maxLinear + img.Width).GetHashCode()); OutputConsole.Write("Ciphering file..."); if (file.Length < maxLinear - extraBytes) { string fileLength = file.Length.ToString(); OutputConsole.Write("Processing image..."); OutputConsole.Write("Writing metadata..."); for (int i = 0; i < fileLength.Length; i++) { Point point = LinearIndexToPoint(generator.NextN, img.Width, img.Height); Color pixel = img.GetPixel(point.X, point.Y); char letter = fileLength[i]; int value = Convert.ToInt32(letter); img.SetPixel(point.X, point.Y, EncodePixel(pixel, value)); } //Insert # to separate file length from filename Point point1 = LinearIndexToPoint(generator.NextN, img.Width, img.Height); Color pixel1 = img.GetPixel(point1.X, point1.Y); int value1 = Convert.ToInt32('#'); img.SetPixel(point1.X, point1.Y, EncodePixel(pixel1, value1)); //Insert filename and finish with null char for (int i = 0; i < filename.Length; i++) { Point point = LinearIndexToPoint(generator.NextN, img.Width, img.Height); Color pixel = img.GetPixel(point.X, point.Y); char letter = filename[i]; int value = Convert.ToInt32(letter); img.SetPixel(point.X, point.Y, EncodePixel(pixel, value)); } point1 = LinearIndexToPoint(generator.NextN, img.Width, img.Height); pixel1 = img.GetPixel(point1.X, point1.Y); value1 = 0; img.SetPixel(point1.X, point1.Y, EncodePixel(pixel1, value1)); OutputConsole.Write("Writing file data..."); //Write file for (int i = 0; i < file.Length; i++) { Point point = LinearIndexToPoint(generator.NextN, img.Width, img.Height); Color pixel = img.GetPixel(point.X, point.Y); int value = f.file[i]; img.SetPixel(point.X, point.Y, EncodePixel(pixel, value)); } OutputConsole.Write("Finished embedding file"); } else { OutputConsole.Write("File size is greater than total pixels in image with extra data, resize or use another image to encrypt this file"); img = null; } }
public static HiddenFile GetFileFromImage2(Image image) { try { Bitmap img = new Bitmap(image); int maxLinear = image.Width * image.Height; SeedRNG generator = new SeedRNG((maxLinear + image.Width).GetHashCode(), maxLinear, true); OutputConsole.Write("Seed generated"); string text = string.Empty; int value = 0; //Read file length OutputConsole.Write("Processing image..."); OutputConsole.Write("Reading metadata..."); do { Point point = LinearIndexToPoint(generator.NextN, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); if (value != '#') text += Convert.ToChar(value); } while (value != '#' && char.IsNumber((char)value)); int filelength = int.Parse(text); OutputConsole.Write(string.Format("Extracted file size: {0} bytes", filelength)); text = string.Empty; value = 0; //Read filename do { Point point = LinearIndexToPoint(generator.NextN, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); if (value != 0) text += Convert.ToChar(value); } while (value != 0); string filename = text; OutputConsole.Write(string.Format("Extracted file name: {0}", filename)); byte[] file = new byte[filelength]; for (int i = 0; i < filelength; i++) { Point point = LinearIndexToPoint(generator.NextN, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); file[i] = (byte)value; } OutputConsole.Write(string.Format("Extracted file content")); HiddenFile f = new HiddenFile(file, filename); f.cipherFile((maxLinear + img.Width).GetHashCode()); OutputConsole.Write("Ciphering file..."); return f; } catch (Exception e) { OutputConsole.Write("Error: File not found"); Console.WriteLine(e.Message); return null; } }
public static HiddenFile GetFileFromImage2(Image image) { try { Bitmap img = new Bitmap(image); int maxLinear = image.Width * image.Height; SeedRNG generator = new SeedRNG((maxLinear + image.Width).GetHashCode(), maxLinear, true); OutputConsole.Write("Seed generated"); string text = string.Empty; int value = 0; //Read file length OutputConsole.Write("Processing image..."); OutputConsole.Write("Reading metadata..."); do { Point point = LinearIndexToPoint(generator.NextN, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); if (value != '#') { text += Convert.ToChar(value); } } while (value != '#' && char.IsNumber((char)value)); int filelength = int.Parse(text); OutputConsole.Write(string.Format("Extracted file size: {0} bytes", filelength)); text = string.Empty; value = 0; //Read filename do { Point point = LinearIndexToPoint(generator.NextN, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); if (value != 0) { text += Convert.ToChar(value); } } while (value != 0); string filename = text; OutputConsole.Write(string.Format("Extracted file name: {0}", filename)); byte[] file = new byte[filelength]; for (int i = 0; i < filelength; i++) { Point point = LinearIndexToPoint(generator.NextN, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); file[i] = (byte)value; } OutputConsole.Write(string.Format("Extracted file content")); HiddenFile f = new HiddenFile(file, filename); f.cipherFile((maxLinear + img.Width).GetHashCode()); OutputConsole.Write("Ciphering file..."); return(f); } catch (Exception e) { OutputConsole.Write("Error: File not found"); Console.WriteLine(e.Message); return(null); } }