private static void EncodeFileWithColorLinear(ref Bitmap img, byte[] file, string filename) { Bitmap backup = img.Clone() as Bitmap; int maxLinear = img.Width * img.Height; int c = 0; OutputConsole.Write(string.Format("File size: {0}", FileSizeFormatProvider.GetFileSize(file.Length))); 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(c, 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)); c++; } //Insert # to separate file length from filename Point point1 = LinearIndexToPoint(c, 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)); c++; //Insert filename and finish with null char for (int i = 0; i < filename.Length; i++) { Point point = LinearIndexToPoint(c, 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)); c++; } point1 = LinearIndexToPoint(c, img.Width, img.Height); pixel1 = img.GetPixel(point1.X, point1.Y); value1 = 0; img.SetPixel(point1.X, point1.Y, EncodePixel(pixel1, value1)); c++; //Write file OutputConsole.Write("Writing file data..."); for (int i = 0; i < file.Length; i++) { Point point = LinearIndexToPoint(c, 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)); c++; } 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 DecryptFile2(byte[] wav) { try { WavAudio audio = new WavAudio(wav); string text = string.Empty; SeedURNG generator = new SeedURNG(audio.totalSamples, audio.totalSamples, true); OutputConsole.Write("Seed generated"); OutputConsole.Write("Processing wav file..."); OutputConsole.Write("Reading metadata..."); uint value = 0; do { value = 0; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; value = value | ((sampleValue & 1) << x); } if (value != '#') text += Convert.ToChar(value); } while (value != '#' && char.IsNumber((char)value)); int filesize = int.Parse(text); OutputConsole.Write(string.Format("Extracted file size: {0} bytes", filesize)); text = string.Empty; do { value = 0; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; value = value | ((sampleValue & 1) << x); } 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[filesize]; for (int i = 0; i < filesize; i++) { value = 0; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; value = value | ((sampleValue & 1) << x); } file[i] = (byte)value; } OutputConsole.Write(string.Format("Extracted file content")); HiddenFile f = new HiddenFile(file, filename); OutputConsole.Write("Ciphering file..."); f.cipherFile((int)audio.totalSamples); return f; } catch (Exception e) { return null; } }
public static HiddenFile GetFileFromImageLinear(Image image) { try { Bitmap img = new Bitmap(image); int maxLinear = image.Width * image.Height; int c = 0; string text = string.Empty; int value = 0; OutputConsole.Write("Processing image..."); OutputConsole.Write("Reading metadata..."); //Read file length do { Point point = LinearIndexToPoint(c, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); c++; 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(c, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); c++; 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(c, image.Width, image.Height); Color pixel = img.GetPixel(point.X, point.Y); value = DecodePixel(pixel); file[i] = (byte)value; c++; } 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 byte[] EncryptFileLinear(byte[] wav, byte[] file, string filename) { WavAudio audio = new WavAudio(wav); uint value = 0; int extraBytes = 2 + filename.Length + file.Length.ToString().Length; HiddenFile f = new HiddenFile(file, filename); OutputConsole.Write(string.Format("File size: {0} bytes", file.Length)); f.cipherFile((int)audio.totalSamples); if (file.Length <= Math.Floor((double)(audio.totalSamples / 8)) - extraBytes) { uint n = 0; OutputConsole.Write("Ciphering file"); OutputConsole.Write("Processing wav file..."); OutputConsole.Write("Writing metadata..."); //Write file size for (int i = 0; i < file.Length.ToString().Length; i++) { value = file.Length.ToString()[i]; for (int x = 0; x < 8; x++) { uint sample = n; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; n++; } } value = '#'; for (int x = 0; x < 8; x++) { uint sample = n; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; n++; } //Write file name for (int i = 0; i < filename.Length; i++) { value = filename[i]; for (int x = 0; x < 8; x++) { uint sample = n; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; n++; } } value = 0; for (int x = 0; x < 8; x++) { uint sample = n; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; n++; } //Write file content OutputConsole.Write("Writing file data..."); for (int i = 0; i < file.Length; i++) { value = f.file[i]; for (int x = 0; x < 8; x++) { uint sample = n; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; n++; } } } else { OutputConsole.Write("Error"); return null; } OutputConsole.Write("Finished embedding file"); OutputConsole.Write(string.Format("Used {0} samples", (file.Length + extraBytes) * 8)); audio.Save(); return audio.data; }
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); } }
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 DecryptFile2(byte[] wav) { try { WavAudio audio = new WavAudio(wav); string text = string.Empty; SeedURNG generator = new SeedURNG(audio.totalSamples, audio.totalSamples, true); OutputConsole.Write("Seed generated"); OutputConsole.Write("Processing wav file..."); OutputConsole.Write("Reading metadata..."); uint value = 0; do { value = 0; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; value = value | ((sampleValue & 1) << x); } if (value != '#') { text += Convert.ToChar(value); } } while (value != '#' && char.IsNumber((char)value)); int filesize = int.Parse(text); OutputConsole.Write(string.Format("Extracted file size: {0} bytes", filesize)); text = string.Empty; do { value = 0; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; value = value | ((sampleValue & 1) << x); } 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[filesize]; for (int i = 0; i < filesize; i++) { value = 0; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; value = value | ((sampleValue & 1) << x); } file[i] = (byte)value; } OutputConsole.Write(string.Format("Extracted file content")); HiddenFile f = new HiddenFile(file, filename); OutputConsole.Write("Ciphering file..."); f.cipherFile((int)audio.totalSamples); return(f); } catch (Exception e) { return(null); } }
public static byte[] EncryptFile2(byte[] wav, byte[] file, string filename) { WavAudio audio = new WavAudio(wav); uint value = 0; int extraBytes = 2 + filename.Length + file.Length.ToString().Length; HiddenFile f = new HiddenFile(file, filename); OutputConsole.Write(string.Format("File size: {0} bytes", file.Length)); f.cipherFile((int)audio.totalSamples); if (file.Length <= Math.Floor((double)(audio.totalSamples / 8)) - extraBytes) { SeedURNG generator = new SeedURNG(audio.totalSamples, audio.totalSamples, true); OutputConsole.Write("Seed generated"); OutputConsole.Write("Ciphering file"); OutputConsole.Write("Processing wav file..."); OutputConsole.Write("Writing metadata..."); //Write file size for (int i = 0; i < file.Length.ToString().Length; i++) { value = file.Length.ToString()[i]; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; } } value = '#'; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; } //Write file name for (int i = 0; i < filename.Length; i++) { value = filename[i]; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; } } value = 0; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; } //Write file content OutputConsole.Write("Writing file data..."); for (int i = 0; i < file.Length; i++) { value = f.file[i]; for (int x = 0; x < 8; x++) { uint sample = generator.NextN; uint sampleValue = audio.samples[sample]; sampleValue = (sampleValue & 0xFFFFFFFE) | ((value >> x) & 1); audio.samples[sample] = sampleValue; } } } else { OutputConsole.Write("Error"); return(null); } OutputConsole.Write("Finished embedding file"); OutputConsole.Write(string.Format("Used {0} samples", (file.Length + extraBytes) * 8)); audio.Save(); return(audio.data); }