2

我正在尝试使用 LSB 算法将一串文本隐藏到位图中,该算法正在替换每个像素的 RGB 值的最低有效位。到目前为止,我已经遍历了图像的像素并清除了每个像素的 LSB 值。我正在努力的部分是插入来自字符串的新 LSB 值。

这就是我到目前为止所做的任何关于下一步去哪里的指示都会有所帮助

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

2 回答 2

1

尽管您可以使用“常规”算术(他们在一年级时教授的那种)进行位操作,但更常见的是使用位操作运算符来实现相同的目标。

例如,写作R = pixel.R & ~1比减法更常见pixel.R % 2

您无需在设置之前清除该位。1用力一点R = pixel.R | 1。要将其强制为零,请使用R = pixel.R & ~1上述方法。

要迭代“消息stored as a sequence ofN”字节的位,请使用以下检查:

if (message[pos / 8] & (1 << pos % 8) != 0) {
    // bit at position pos is 1
} else {
    // bit at position pos is 0
}
于 2018-02-08T12:00:23.273 回答
0

位运算符使这很容易做到:

将最后一位设置为 1:

var newR = pixel.R | 0b00000001

将最后一位设置为 0

var newR = pixel.R & 0b11111110

这是如何工作的:| or像运算符一样按字节合并位。和 & 像运算符一样合并位and(伪代码):

10101000 | 0000001 = 10101001
10101001 & 1111110 = 10101000
于 2018-02-08T11:59:30.080 回答