简体   繁体   English

C# 更改最低有效位图像算法

[英]C# Changing Least Significant Bit Image Algorithm

I am trying to hide a string of text into a bitmap using the LSB algorithm, which is replacing the least significant bit of the RGB Values for each pixel.我正在尝试使用 LSB 算法将一串文本隐藏到位图中,该算法正在替换每个像素的 RGB 值的最低有效位。 So far I have loped through the pixels of the image and cleared the LSB value for each pixel.到目前为止,我已经浏览了图像的像素并清除了每个像素的 LSB 值。 The part that I am struggling with is inserting the new LSB values that come from a string.我正在努力解决的部分是插入来自字符串的新 LSB 值。

This is what I have done so far any pointers of where to go next would be helpful这是我到目前为止所做的任何有关下一步去哪里的指示都会有所帮助

string text = txtEncrypt.Text;
//Gets the ascii value of each character from the string
var n = ASCIIEncoding.ASCII.GetBytes(text);

Bitmap myBitmap = new Bitmap(myPictureBox.Image);
byte[] rgbBytes = new byte[0];
int R=0, G=0, B=0;
for (int i = 0; i < myBitmap.Width; i++)
{
    for (int j = 0; j < myBitmap.Height; j++)
    {
        Color pixel = myBitmap.GetPixel(i, j);

        // now, clear the least significant bit (LSB) from each pixel element
        //Therefore Three bits in each pixel spare

        R = pixel.R - pixel.R % 2;
        G = pixel.G - pixel.G % 2;
        B = pixel.B - pixel.B % 2;

        // Need to insert new values
    }
}

Although you can do bit manipulation using "regular" arithmetics (the kind they teach in the first grade) it is more common to use bit manipulation operators to achieve the same goal.尽管您可以使用“常规”算术(他们在一年级教授的那种)进行位操作,但更常见的是使用位操作运算符来实现相同的目标。

For example, writing R = pixel.R & ~1 is a lot more common than subtracting pixel.R % 2 .例如,写R = pixel.R & ~1比减去pixel.R % 2更常见。

You don't need to clear the bit before setting it.您不需要在设置之前清除该位。 To force a bit into 1 use R = pixel.R | 1要强制位为1使用R = pixel.R | 1 R = pixel.R | 1 . R = pixel.R | 1 . To force it into zero use the R = pixel.R & ~1 mentioned above.要强制它为零,请使用上面提到的R = pixel.R & ~1

To iterate bits of the "message stored as a sequence of N` bytes use this check:要迭代stored as a sequence of N` 字节stored as a sequence of的“消息的位” stored as a sequence of使用以下检查:

if (message[pos / 8] & (1 << pos % 8) != 0) {
    // bit at position pos is 1
} else {
    // bit at position pos is 0
}

Bitwise operators make this easy to do:按位运算符使这很容易做到:

set last bit to 1:将最后一位设置为 1:

var newR = pixel.R | 0b00000001

set last bit to 0将最后一位设置为 0

var newR = pixel.R & 0b11111110

how this works: |这是如何工作的: | merges bits like an or operator would per byte.or运算符一样合并位每个字节。 and & merges bits like an and operator would (psuedocode): and & 像and运算符一样合并位(伪代码):

10101000 | 0000001 = 10101001
10101001 & 1111110 = 10101000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM