簡體   English   中英

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

[英]C# Changing Least Significant Bit Image Algorithm

我正在嘗試使用 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
    }
}

盡管您可以使用“常規”算術(他們在一年級教授的那種)進行位操作,但更常見的是使用位操作運算符來實現相同的目標。

例如,寫R = pixel.R & ~1比減去pixel.R % 2更常見。

您不需要在設置之前清除該位。 要強制位為1使用R = pixel.R | 1 R = pixel.R | 1 . 要強制它為零,請使用上面提到的R = pixel.R & ~1

要迭代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
}

按位運算符使這很容易做到:

將最后一位設置為 1:

var newR = pixel.R | 0b00000001

將最后一位設置為 0

var newR = pixel.R & 0b11111110

這是如何工作的: | or運算符一樣合並位每個字節。 and & 像and運算符一樣合並位(偽代碼):

10101000 | 0000001 = 10101001
10101001 & 1111110 = 10101000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM