将隐藏的消息写入图像文件c#

我正在寻找一个教程,其中描述了如何使用C#将隐藏文本写入/读取到图像文件中,但遗憾的是我发现了关于此的任何好post,所以我决定在这里询问这个小技巧。

假设我有一个名为test.jpg的图像文件,有没有办法在这个文件中写一个像“Hello word”这样的隐藏文本? 我已经阅读了有关图像元数据的内容,我想我应该尝试以某种方式编写这个元数据,但我不知道如何找到。 如果有人知道该怎么做,请帮助我理解如何做到这一点。 谢谢。

对此的正确用语是“隐写术”。

以下文章阐述了隐写术的定义:

隐写术是通过在其他人中嵌入信息来隐藏信息的艺术和科学。 隐写术的工作原理是将常规计算机文件中无用或未使用的数据替换为不同的不可见信息。 隐藏的信息可以是纯文本,密文,甚至是图像

关于隐写术如何工作的逐步解释:

  • 循环遍历图像的像素。 在每次迭代中,获取RGB值,每个RGB值以单独的整数分隔。
  • 对于R,G和B中的每一个,使LSB等于0.这些位将用于隐藏字符。
  • 获取当前字符并将其转换为整数。 然后隐藏其在R1,G1,B1,R2,G2,B2,R3,G3中的8位,其中数字指的是像素的数量。 在这些元素的每个LSB中(从R1到G3),连续隐藏字符的位。
  • 处理8位字符时,跳转到下一个字符,然后重复该过程直到处理完整个文本。
  • 根据文本的长度,文本可以隐藏在图像的一小部分中。 所以,必须有一些东西表明我们到达文本的最后。 指标只是连续8个零。 从图像中提取文本时需要这样做。

它比隐藏更简单。 只需通过图像的像素,直到找到8个连续的零。 在传递过程中,从每个像素元素(R,G,B)中选取LSB并将其附加到空值中。 完成此值的8位后,将其转换回字符,然后将该字符添加到您要查找的结果文本中。

一个示例源代码是::

class SteganographyHelper { public enum State { Hiding, Filling_With_Zeros }; public static Bitmap embedText(string text, Bitmap bmp) { // initially, we'll be hiding characters in the image State state = State.Hiding; // holds the index of the character that is being hidden int charIndex = 0; // holds the value of the character converted to integer int charValue = 0; // holds the index of the color element (R or G or B) that is currently being processed long pixelElementIndex = 0; // holds the number of trailing zeros that have been added when finishing the process int zeros = 0; // hold pixel elements int R = 0, G = 0, B = 0; // pass through the rows for (int i = 0; i < bmp.Height; i++) { // pass through each row for (int j = 0; j < bmp.Width; j++) { // holds the pixel that is currently being processed Color pixel = bmp.GetPixel(j, i); // now, clear the least significant bit (LSB) from each pixel element R = pixel.R - pixel.R % 2; G = pixel.G - pixel.G % 2; B = pixel.B - pixel.B % 2; // for each pixel, pass through its elements (RGB) for (int n = 0; n < 3; n++) { // check if new 8 bits has been processed if (pixelElementIndex % 8 == 0) { // check if the whole process has finished // we can say that it's finished when 8 zeros are added if (state == State.Filling_With_Zeros && zeros == 8) { // apply the last pixel on the image // even if only a part of its elements have been affected if ((pixelElementIndex - 1) % 3 < 2) { bmp.SetPixel(j, i, Color.FromArgb(R, G, B)); } // return the bitmap with the text hidden in return bmp; } // check if all characters has been hidden if (charIndex >= text.Length) { // start adding zeros to mark the end of the text state = State.Filling_With_Zeros; } else { // move to the next character and process again charValue = text[charIndex++]; } } // check which pixel element has the turn to hide a bit in its LSB switch (pixelElementIndex % 3) { case 0: { if (state == State.Hiding) { // the rightmost bit in the character will be (charValue % 2) // to put this value instead of the LSB of the pixel element // just add it to it // recall that the LSB of the pixel element had been cleared // before this operation R += charValue % 2; // removes the added rightmost bit of the character // such that next time we can reach the next one charValue /= 2; } } break; case 1: { if (state == State.Hiding) { G += charValue % 2; charValue /= 2; } } break; case 2: { if (state == State.Hiding) { B += charValue % 2; charValue /= 2; } bmp.SetPixel(j, i, Color.FromArgb(R, G, B)); } break; } pixelElementIndex++; if (state == State.Filling_With_Zeros) { // increment the value of zeros until it is 8 zeros++; } } } } return bmp; } public static string extractText(Bitmap bmp) { int colorUnitIndex = 0; int charValue = 0; // holds the text that will be extracted from the image string extractedText = String.Empty; // pass through the rows for (int i = 0; i < bmp.Height; i++) { // pass through each row for (int j = 0; j < bmp.Width; j++) { Color pixel = bmp.GetPixel(j, i); // for each pixel, pass through its elements (RGB) for (int n = 0; n < 3; n++) { switch (colorUnitIndex % 3) { case 0: { // get the LSB from the pixel element (will be pixel.R % 2) // then add one bit to the right of the current character // this can be done by (charValue = charValue * 2) // replace the added bit (which value is by default 0) with // the LSB of the pixel element, simply by addition charValue = charValue * 2 + pixel.R % 2; } break; case 1: { charValue = charValue * 2 + pixel.G % 2; } break; case 2: { charValue = charValue * 2 + pixel.B % 2; } break; } colorUnitIndex++; // if 8 bits has been added, // then add the current character to the result text if (colorUnitIndex % 8 == 0) { // reverse? of course, since each time the process occurs // on the right (for simplicity) charValue = reverseBits(charValue); // can only be 0 if it is the stop character (the 8 zeros) if (charValue == 0) { return extractedText; } // convert the character value from int to char char c = (char)charValue; // add the current character to the result text extractedText += c.ToString(); } } } } return extractedText; } public static int reverseBits(int n) { int result = 0; for (int i = 0; i < 8; i++) { result = result * 2 + n % 2; n /= 2; } return result; } } 

来源文章: http : //www.codeproject.com/Tips/635715/Steganography-Simple-Implementation-in-Csharp

你也可以参考这个.. 图像隐写术应用C#