写隐藏的信息图像文件c# [英] Write hidden messages to image file c#

查看:168
本文介绍了写隐藏的信息图像文件c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找其中描述了如何编写/使用C#读取隐藏的文本转换为图像文件,但不幸的是我嚣教程中找不到这方面有任何好的帖子,所以我决定在这里问这个小把戏<。 / p>

假设我有一个名为test.jpg放在一个图像文件,有没有办法写一个隐藏的文本,如你好词这个文件?我看了一下图像元数据,我想,我应该尝试以某种方式写入该元数据,但我嚣;找不到如何。
如果有人知道如何做到这一点,请帮助我了解如何把它。
谢谢你。


解决方案

这样做的正确的术语是隐写术。

下面的文章状态是隐写术的定义:


  

隐写术是别人中嵌入消息的隐藏信息的艺术和科学。隐写术的工作原理是用不同的,无形的信息比特替换普通的计算机文件无用或不使用的数据位。这种隐藏的信息可以是纯文本,密文,甚至是图像


步骤一步解释隐写上如何工作的:


  • 通过图像的像素回路。在每次迭代中,得到的RGB值在一个单独的整数分隔每个。

  • 对于每个R,G和B的上,进行LSB等于0,这些位将在隐藏的字符被使用。

  • 获取当前字符,并将其转换为整数。然后隐藏其8位在R1,G1,B1,R2,G2,B2,R3,G3,其中该数字指的是象素的号码。在每一个LSB​​这些元素(从R1至G3)的,隐藏的字符的位连续。

  • 当字符的8位进行处理,跳转到下一个字符,直到整个文本处理重复上述过程。

  • 的文本可以按照该文本的长度被隐藏在图象中的一小部分。因此,必须有东西,表明我们在这里达到了文本的末尾。该指标仅仅是连续8个零。这将提取图像中的文字时是必要的。


  

这是不是隐藏更简单。刚刚通过的像素
  图像,直到你找到连续8个零。当你路过,挑
  LSB从每个像素元件(R,G,B)和将其固定到一个空
  值。当这个值的8位完成后,将其转换回
  字符,则该字符添加到您正在寻找的结果文本。


这是示例源$ C ​​$ C将是:

 类SteganographyHelper
{
    公共枚举状态
    {
        隐藏,
        Filling_With_Zeros
    };    公共静态位图embedText(字符串文本,BMP位图)
    {
        //开始,我们将在图像中藏匿的字符
        状态的状态= State.Hiding;        //认为是被隐藏的字符的索引
        INT的charIndex = 0;        //保持字符的值转换成整数
        INT的charValue = 0;        //认为是当前正在处理的色彩元素的索引(R或G或B)的
        长pixelElementIndex = 0;        //拥有的处理结束时已添加尾随零的数目
        INT零= 0;        //保持像素元件
        INT R = 0,G = 0,B = 0;        //通过行
        的for(int i = 0; I&LT; bmp.Height;我++)
        {
            //通过每一行
            对于(INT J = 0; J&LT; bmp.Width; J ++)
            {
                //认为当前正在处理的像素
                彩色像素= bmp.GetPixel(J,I);                //现在,清除从每个象素元件的至少显著位(LSB)
                R = pixel.R - pixel.R%2;
                G = pixel.G - pixel.G%2;
                B = pixel.B - pixel.B%2;                //对于每个像素,通过它的元素(RGB)的
                为(中间体n = 0时; N 3;; N ++)
                {
                    //检查是否新的8位已被处理
                    如果(pixelElementIndex%8 == 0)
                    {
                        //检查的整个过程已经完成
                        //我们可以说,当加入8零它的完成
                        如果(状态== State.Filling_With_Zeros&放大器;&安培;零== 8)
                        {
                            //申请的最后一个像素的图像上
                            //即使仅其元件的一部分受到影响
                            如果((pixelElementIndex - 1)%3 2)
                            {
                                bmp.SetPixel(J,I,Color.FromArgb(R,G,B));
                            }                            //使用隐藏在文本返回位图
                            返回BMP;
                        }                        //检查是否所有字符已隐藏
                        如果(charIndex处&GT; = text.Length)
                        {
                            //启动加零来标记文本的末尾
                            状态= State.Filling_With_Zeros;
                        }
                        其他
                        {
                            //再次移动到下一个字符和过程
                            的charValue =文本[charIndex处++];
                        }
                    }                    //检查哪个像素元素具有转弯躲了一下它的LSB
                    开关(pixelElementIndex%3)
                    {
                        情况下0:
                            {
                                如果(状态== State.Hiding)
                                {
                                    //在字符的最右边的位将是(的charValue%2)
                                    //把这个值,而不是LSB的像素单元的
                                    //只是把它添加到它
                                    //记得,LSB像素元素已被清理
                                    //此操作之前
                                    R + =的charValue%2;                                    //删除字符的附加最右边的位
                                    //这样下一次我们能到达下一个
                                    的charValue / = 2;
                                }
                            }打破;
                        情况1:
                            {
                                如果(状态== State.Hiding)
                                {
                                    G + =的charValue%2;                                    的charValue / = 2;
                                }
                            }打破;
                        案例2:
                            {
                                如果(状态== State.Hiding)
                                {
                                    B + =的charValue%2;                                    的charValue / = 2;
                                }                                bmp.SetPixel(J,I,Color.FromArgb(R,G,B));
                            }打破;
                    }                    pixelElementIndex ++;                    如果(状态== State.Filling_With_Zeros)
                    {
                        //递增零的值,直到它是8
                        零++;
                    }
                }
            }
        }        返回BMP;
    }    公共静态字符串extractText(BMP位图)
    {
        INT colorUnitIndex = 0;
        INT的charValue = 0;        //认为会从图像中提取文本
        字符串extractedText =的String.Empty;        //通过行
        的for(int i = 0; I&LT; bmp.Height;我++)
        {
            //通过每一行
            对于(INT J = 0; J&LT; bmp.Width; J ++)
            {
                彩色像素= bmp.GetPixel(J,I);                //对于每个像素,通过它的元素(RGB)的
                为(中间体n = 0时; N 3;; N ++)
                {
                    开关(colorUnitIndex%3)
                    {
                        情况下0:
                            {
                                //取得LSB的从像素元件(将pixel.R%2)
                                //然后一个位添加到当前字符右侧
                                //这可以通过进行(的charValue =的charValue * 2)
                                //替换加位(其值默认为0)与
                                // LSB的所述像素元件的,简单地通过加入
                                的charValue =的charValue * 2 + pixel.R%2;
                            }打破;
                        情况1:
                            {
                                的charValue =的charValue * 2 + pixel.G%2;
                            }打破;
                        案例2:
                            {
                                的charValue =的charValue * 2 + pixel.B%2;
                            }打破;
                    }                    colorUnitIndex ++;                    //如果8位已被添加,
                    //然后当前字符添加到结果文本
                    如果(colorUnitIndex%8 == 0)
                    {
                        //反转?当然,每次由于过程中发生
                        //在右侧(为简单起见)
                        的charValue = reverseBits(的charValue);                        //仅可以是0,如果它是停止字符(8个零)
                        如果(的charValue == 0)
                        {
                            返回extractedText;
                        }                        //从int转换成字符值为char
                        焦C =(焦炭)的charValue;                        //当前字符添加到结果文本
                        extractedText + = c.ToString();
                    }
                }
            }
        }        返回extractedText;
    }    公共静态INT reverseBits(INT N)
    {
        INT结果为0;        的for(int i = 0; I&LT; 8;我++)
        {
            结果=结果* 2 + N%2;            N / = 2;
        }        返回结果;
    }
}

来源的文章:http://www.$c$cproject.com/Tips/635715/Steganography-Simple-Implementation-in-Csharp

I was looking for a tutorial where is described how to write/read hidden text into image file using C#, but unfortunately I din;t find any good posts about this, so I decided to ask here about this small trick.

Suppose I have an image file called test.jpg , is there any way to write an hidden text like "Hello word " to this file ? I have read about image metadata , and I suppose that I should try to write somehow to this metadata , but I din;t find how. If anybody know how to do it , please help me to understand how to it. Thanks.

解决方案

The correct term for this is "Steganography".

The following article state's the definition of Steganography:

Steganography is the art and science of hiding information by embedding messages within others. Steganography works by replacing bits of useless or unused data in regular computer files with bits of different, invisible information. This hidden information can be plain text, cipher text, or even images

Step by step explanation on how Steganography works:

  • Loop through the pixels of the image. In each iteration, get the RGB values separated each in a separate integer.
  • For each of R, G, and B, make the LSB equals to 0. These bits will be used in hiding characters.
  • Get the current character and convert it to integer. Then hide its 8 bits in R1, G1, B1, R2, G2, B2, R3, G3, where the numbers refer to the numbers of the pixels. In each LSB of these elements (from R1 to G3), hide the bits of the character consecutively.
  • When the 8 bits of the character are processed, jump to the next character, and repeat the process until the whole text is processed.
  • The text can be hidden in a small part of the image according to the length of that text. So, there must be something to indicate that here we reached the end of the text. The indicator is simply 8 consecutive zeros. This will be needed when extracting the text from the image.

It's more simple than hiding. Just pass through the pixels of the image until you find 8 consecutive zeros. As you are passing, pick the LSB from each pixel element (R, G, B) and attach it into an empty value. When the 8 bits of this value are done, convert it back to character, then add that character to the result text you are seeking.

An example source code would be::

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;
    }
} 

Source article: http://www.codeproject.com/Tips/635715/Steganography-Simple-Implementation-in-Csharp

这篇关于写隐藏的信息图像文件c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆