```       __                                             __
___ / /____ _  _____ ______ _____ ____    ___  ___ / /_
(_-</ __/ -_) |/ / -_) __/ // / _ `/ _ \_ / _ \/ -_) __/
/___/\__/\__/|___/\__/_/  \_, /\_,_/_//_(_)_//_/\__/\__/
/___/
```

### LSB Matching Explained

December 1, 2021

LSB Matching (LSBM) improves upon LSB Replacement (LSBR) by removing (most of) the statistical anomalies which can be detected using statistical analysis. LSBM works in a similar way to LSBR, however instead of replacing the LSBs with the message bits, the LSBs are randomly increased or decreased by one if the message bits and the LSBs in the image bytes do not match.

The rules for LSBM are as follows:

(1) If the message bit and the LSB of the image byte are the same, do nothing.

(2) If the message bit and the LSB of the image byte are not the same, randomly add or subtract one.

Let us explain this with an example. We will use the following 3 x 3 pixel RGB image:

1  2  3  4  5  6  7  8  9
10 ‍11 ‍12 ‍13 ‍14 ‍15 ‍16 ‍17 ‍18
19 ‍20 ‍21 ‍22 ‍23 ‍24 ‍25 ‍26 ‍27

Our example image is an RGB image, so each pixel has three bytes (one byte representing the colour Red, one byte representing the colour Green, and one byte representing the colour Blue), hence why the image is a matrix of 3 x 9 bytes.

The message we want to hide is the character ”Z” (decimal 90) which can be written as follows:

‍01011010

We will use sequential embedding (rather than random embedding, or some other embedding technique) to keep things simple.

The first byte in the cover image is the number 1:

‍00000001

The LSB does not match the first bit in the message, so we randomly add or subtract one. A possible result is as follows:

‍00000010

In the above case, we randomly added one. We could have randomly subtracted one:

‍00000000

As you can see, both addition and subtraction results in the desired LSB value.

The second byte in the cover image is the number 2:

‍00000010

The LSB does not match the second bit in the message, so we randomly add or subtract one. A possible result (addition) is as follows:

‍00000011

The third byte in the cover image is the number 3:

‍00000011

Once again the LSB does not match, so we randomly add or subtract one. A possible result (subtraction) is the following:

‍00000010

When all eight bits in our secret message have been embedded in the cover image, we are left with our desired stego-image.

There are two issues we need to be aware of during LSBM encoding:

(1) If the image byte is 0, only addition is allowed to prevent the byte flipping around to 255.

(2) If the image byte is 255, only subtraction is allowed to prevent the byte flipping around to 0.

If the above two situations are ignored, the stego-image will develop distorted pixels. For example, a pixel byte which should be white (0x00) changes to black (0xFF).

Decoding the hidden message follows the same process as decoding a message hidden using LSBR. This is because both LSBR and LSBM cause the altered LSBs in the image to equal the hidden message.