/// <summary> /// Embed content /// </summary> /// <param name="sender">caused the event</param> /// <param name="e">event params</param> private void embedButton_Click(object sender, EventArgs e) { // var init Boolean success = false; // show an error if nothing to embed if (this.destFile.Text.Trim() == String.Empty) { MessageBox.Show("You must supply a destination file in which to embed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if ((ContentType)Enum.Parse(typeof(ContentType), this.embedContentType.SelectedItem.ToString()) == ContentType.Text) { if (this.embedText.Text.Trim() == String.Empty) { MessageBox.Show("You must supply text to embed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { EmbeddingContent content = new EmbeddingContent(this.embedText.Text); EmbeddingPlane planes = (EmbeddingPlane)Enum.Parse(typeof(EmbeddingPlane), this.embedPlane.SelectedItem.ToString()); byte bitDepth = Convert.ToByte(this.embedBitDepth.SelectedItem.ToString()); using (Bitmap img = SteganographyMethods.Embed(this.sourceFile.Text.Trim(), planes, bitDepth, content)) { ImageFormat format = (this.destFileType.SelectedItem.ToString() == "Bmp") ? ImageFormat.Bmp : ImageFormat.Png; success = MainWindow.SaveImgToFile(img, this.destFile.Text.Trim(), format); } } } else if ((ContentType)Enum.Parse(typeof(ContentType), this.embedContentType.SelectedItem.ToString()) == ContentType.File) { if (this.embedFile.Text.Trim() == String.Empty) { MessageBox.Show("You must supply a file to embed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { EmbeddingContent content = new EmbeddingContent(new FileInfo(this.embedFile.Text.Trim())); EmbeddingPlane planes = (EmbeddingPlane)Enum.Parse(typeof(EmbeddingPlane), this.embedPlane.SelectedItem.ToString()); byte bitDepth = Convert.ToByte(this.embedBitDepth.SelectedItem.ToString()); using (Bitmap img = SteganographyMethods.Embed(this.sourceFile.Text.Trim(), planes, bitDepth, content)) { ImageFormat format = (this.destFileType.SelectedItem.ToString() == "Bmp") ? ImageFormat.Bmp : ImageFormat.Png; success = MainWindow.SaveImgToFile(img, this.destFile.Text.Trim(), format); } } } // notify if the operation is successful if (success == true) { MessageBox.Show("The operation was completed", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public void TestEmbeddingContent() { // build embedding content String header = "File|4|smallfile.txt|abcd"; List <byte> headerAndContent = new List <byte>(Encoding.ASCII.GetBytes(header)); EmbeddingContent ec = new EmbeddingContent(headerAndContent.ToArray()); // check Assert.AreEqual(ec.Header.TypeOfContent, ContentType.File); Assert.AreEqual(ec.Header.Filename, "smallfile.txt"); Assert.AreEqual(ec.Header.SizeOfContent, 4); Assert.AreEqual(Encoding.ASCII.GetString(ec.Content), "abcd"); // build embedding content header = "Text|5||abcd1"; headerAndContent = new List <byte>(Encoding.ASCII.GetBytes(header)); ec = new EmbeddingContent(headerAndContent.ToArray()); // check Assert.AreEqual(ec.Header.TypeOfContent, ContentType.Text); Assert.AreEqual(ec.Header.Filename, String.Empty); Assert.AreEqual(ec.Header.SizeOfContent, 5); Assert.AreEqual(Encoding.ASCII.GetString(ec.Content), "abcd1"); // build embedding content ERROR try { ec = new EmbeddingContent((byte[])null); Assert.IsTrue(false); } catch (SteganographyException) { Assert.IsTrue(true); } // build embedding content ERROR try { ec = new EmbeddingContent(new FileInfo("notafile.txt")); Assert.IsTrue(false); } catch (SteganographyException) { Assert.IsTrue(true); } }
public void TestBMPTextEmbedDepth2RGBtoBMPandPNG() { // build content const String TEST_MESSAGE = "this is a test message to check to make sure it works!"; EmbeddingContent ec = new EmbeddingContent(TEST_MESSAGE); EmbeddingContent recovered = null; // embed using (Bitmap img = SteganographyMethods.Embed("test.bmp", EmbeddingPlane.RGB, 2, ec)) { Assert.IsNotNull(img); using (Bitmap png = new Bitmap(img)) { png.Save("testout.bmp", System.Drawing.Imaging.ImageFormat.Bmp); png.Save("testout.png", System.Drawing.Imaging.ImageFormat.Png); } } // extract bmp recovered = SteganographyMethods.Extract("testout.bmp", EmbeddingPlane.RGB, 2); // test Assert.IsNotNull(recovered); String recoveredMessage = Encoding.ASCII.GetString(recovered.Content); Assert.AreEqual(recoveredMessage, TEST_MESSAGE); Assert.AreEqual(recovered.Header.TypeOfContent, ContentType.Text); // extract png recovered = null; recovered = SteganographyMethods.Extract("testout.png", EmbeddingPlane.RGB, 2); // test Assert.IsNotNull(recovered); recoveredMessage = Encoding.ASCII.GetString(recovered.Content); Assert.AreEqual(recoveredMessage, TEST_MESSAGE); Assert.AreEqual(recovered.Header.TypeOfContent, ContentType.Text); // cleanup File.Delete("testout.bmp"); File.Delete("testout.png"); }
/// <summary> /// Extract content /// </summary> /// <param name="sender">caused the event</param> /// <param name="e">event params</param> private void extractButton_Click(object sender, EventArgs e) { // clear the extracted text box this.extractedText.Text = String.Empty; // extract! EmbeddingPlane planes = (EmbeddingPlane)Enum.Parse(typeof(EmbeddingPlane), this.extractPlane.SelectedItem.ToString()); byte bitDepth = Convert.ToByte(this.extractBitDepth.SelectedItem.ToString()); EmbeddingContent content = SteganographyMethods.Extract(this.sourceFile.Text.Trim(), planes, bitDepth); if (content == null) { // nothing was extracted, show an error message box MessageBox.Show("An error occurred while extracting", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { if (content.Header.TypeOfContent == ContentType.Text) { // text was extracted, put in extracted text box this.extractedText.Text = Encoding.ASCII.GetString(content.Content); } else { // write to file String newFilename = String.Format("extracted_{0}", content.Header.Filename); String newPathAndFilename = Path.Combine(Path.GetDirectoryName(this.sourceFile.Text), newFilename); File.WriteAllBytes(newPathAndFilename, content.Content); // extraction mesage String extractionMessage = String.Format("Successfully extracted file: {0}", newFilename); // notify that a file was extracted this.extractedText.Text = extractionMessage; MessageBox.Show(extractionMessage, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
public void TestPNGRandGandBDepth1() { // build content const String TEST_MESSAGE = "this is a test message to check to make sure it works!"; EmbeddingContent ec = new EmbeddingContent(TEST_MESSAGE); EmbeddingContent recovered = null; // embed R using (Bitmap img = SteganographyMethods.Embed("test.bmp", EmbeddingPlane.R, 1, ec)) { Assert.IsNotNull(img); using (Bitmap png = new Bitmap(img)) { png.Save("testout.png", System.Drawing.Imaging.ImageFormat.Png); } } // extract R recovered = SteganographyMethods.Extract("testout.png", EmbeddingPlane.R, 1); Assert.IsNotNull(recovered); // test R String recoveredMessage = Encoding.ASCII.GetString(recovered.Content); Assert.AreEqual(recoveredMessage, TEST_MESSAGE); Assert.AreEqual(recovered.Header.TypeOfContent, ContentType.Text); // cleanup R recovered = null; File.Delete("testout.png"); // embed G using (Bitmap img = SteganographyMethods.Embed("test.bmp", EmbeddingPlane.G, 1, ec)) { Assert.IsNotNull(img); using (Bitmap png = new Bitmap(img)) { png.Save("testout.png", System.Drawing.Imaging.ImageFormat.Png); } } // extract G recovered = SteganographyMethods.Extract("testout.png", EmbeddingPlane.G, 1); Assert.IsNotNull(recovered); // test G recoveredMessage = Encoding.ASCII.GetString(recovered.Content); Assert.AreEqual(recoveredMessage, TEST_MESSAGE); Assert.AreEqual(recovered.Header.TypeOfContent, ContentType.Text); // cleanup G recovered = null; File.Delete("testout.png"); // embed B using (Bitmap img = SteganographyMethods.Embed("test.bmp", EmbeddingPlane.B, 1, ec)) { Assert.IsNotNull(img); using (Bitmap png = new Bitmap(img)) { png.Save("testout.png", System.Drawing.Imaging.ImageFormat.Png); } } // extract B recovered = SteganographyMethods.Extract("testout.png", EmbeddingPlane.B, 1); Assert.IsNotNull(recovered); // test B recoveredMessage = Encoding.ASCII.GetString(recovered.Content); Assert.AreEqual(recoveredMessage, TEST_MESSAGE); Assert.AreEqual(recovered.Header.TypeOfContent, ContentType.Text); // test wrong extraction from G is null (should be B) using (Bitmap img = new Bitmap("testout.png")) { recovered = SteganographyMethods.Extract(img, EmbeddingPlane.G, 2); } Assert.IsNull(recovered); // cleanup B File.Delete("testout.png"); }
public void TestBMPFileEmbedDepth2RGBtoBMPandPNG() { // var init EmbeddingContent ec = new EmbeddingContent(new FileInfo("embedfile.jpg")); EmbeddingContent recovered = null; int height; int width; // get info aboud the embed file using (Bitmap embedfile = new Bitmap("embedfile.jpg")) { height = embedfile.Height; width = embedfile.Width; } // embed using (Bitmap img = SteganographyMethods.Embed("test.bmp", EmbeddingPlane.RGB, 2, ec)) { Assert.IsNotNull(img); using (Bitmap png = new Bitmap(img)) { png.Save("testout.bmp", System.Drawing.Imaging.ImageFormat.Bmp); png.Save("testout.png", System.Drawing.Imaging.ImageFormat.Png); } } // extract file from BMP, get info recovered = SteganographyMethods.Extract("testout.bmp", EmbeddingPlane.RGB, 2); Assert.IsNotNull(recovered); File.WriteAllBytes("recovered.jpg", recovered.Content); // test Assert.AreEqual(recovered.Header.TypeOfContent, ContentType.File); Assert.AreEqual(recovered.Content.Length, (new FileInfo("recovered.jpg")).Length); using (Bitmap embedfile = new Bitmap("recovered.jpg")) { Assert.AreEqual(height, embedfile.Height); Assert.AreEqual(width, embedfile.Width); } // cleanup recovered = null; File.Delete("recovered.jpg"); // extract from PNG recovered = SteganographyMethods.Extract("testout.png", EmbeddingPlane.RGB, 2); Assert.IsNotNull(recovered); File.WriteAllBytes("recovered.jpg", recovered.Content); // test Assert.AreEqual(recovered.Header.TypeOfContent, ContentType.File); Assert.AreEqual(recovered.Content.Length, (new FileInfo("recovered.jpg")).Length); using (Bitmap embedfile = new Bitmap("recovered.jpg")) { Assert.AreEqual(height, embedfile.Height); Assert.AreEqual(width, embedfile.Width); } // cleanup File.Delete("recovered.jpg"); File.Delete("testout.bmp"); File.Delete("testout.png"); }