将长度为 n 的密文与图像的第二个 LSB 进行异或

XORing a ciphertext of length n with the 2nd LSB of an image

我有一个长度为 n 的密文,我想将此密文的每一位与图像中字节的第二个 LSB 进行异或,并将结果放入图像相同字节的 LSB。

我喜欢继续这个操作,直到密文结束

我将图像读入字节数组,如下所示:

     string cipherText = "some cipher text";

     string address = this.picBoxOriginalImg.ImageLocation;
     byte[] array = File.ReadAllBytes(address);

     //this line gives me the list of all bits separated by a comma(just for visualization of the results for myself)
     this.txtBoxTestByte.Text = string.Join(",", array.Select(b => Convert.ToString(b,2).ToUpper()));

但是从现在开始,我无法想出一个解决方案,将 cipherText 的位与图像字节的第二个 LSB 进行异或,并将结果替换为同一字节的 LSB。

任何帮助或代码片段将不胜感激。

这就是你想要的。底部有说明。

static Bitmap Embed(string fname, string message)
{
    Bitmap img = new Bitmap(fname);
    byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message);

    int nbytes = msgBytes.Length;
    int ibit = 7;
    int ibyte = 0;
    byte currentByte = msgBytes[ibyte];

    for (int i = 0; i < img.Height; i++)
    {
        for (int j = 0; j < img.Width; j++)
        {
            Color pixel = img.GetPixel(i, j);
            int lsb = (msgBytes[ibyte] >> ibit) & 1;
            Color newpixel = Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B ^ (lsb << 1));
            img.SetPixel(i, j, newpixel);
            ibit--;
            if (ibit == -1)
            {
                ibit = 7;
                ibyte++;
                if (ibyte == nbytes)
                {
                    return img;
                }
                currentByte = msgBytes[ibyte];
            }
        }
    }
    return img;
}

ibyte-ibit 组合迭代您的字节,对于每个字节,它从最高有效位到最低有效位提取位。

位提取是使用 bitwise SHIFT and AND operations 完成的。简而言之,右移将感兴趣的位带到最低有效位置,并且 AND 操作屏蔽掉所有其他位。根据您感兴趣的位的值,这会留下 0 或 1。这就是int lsb = msgBytes[ibyte] >> ibit) & 1.

的意思

类似地,对于嵌入,左移将您的位带到第二个 lsb 位置,因此您要么得到 00000000,要么得到 00000010.与 0 returns 异或相同的东西,这样你就不必担心其他 7 位的值是什么。您可以继续将上面的内容与一个像素进行异或运算。 (pixel.B ^ (lsb << 1).

总结了所有这些

我选择在蓝色通道中嵌入信息,但可以随意更改。请记住,如果您在 alpha 通道中进行嵌入,则需要将图像保存为支持透明的格式,例如PNG。同样,由于您处理的是直接像素修改,因此无法将图像保存为有损格式,例如jpeg,因为压缩会修改一些像素,可能会破坏你的信息。