0

I was trying to find various ways of encryption till I researched some to find steganography and wanted to implement it in C#, but problem was that I didn't know how. After some extensive search, LSD algorithm seemed easy enough for me and I started searching GitHub for people with the same projects, I fell upon this code I understood most easily but the problem is that I cannot for the life of me the usage of bitwise operators here and what significance they may hold to manipulate RGB values of each pixel to hide my bits inside them.

Here's the function which reads the characters and converts them into 8-bit byte and sends those bits to modify 3 pixels at once.

 public Bitmap EncryptImage(string imagePath, string text)
    {
        var image = new Bitmap(imagePath, true);
        text = Preamble + text;
        for (int i = 0; i <= text.Length; i++) {
            byte b = (byte)((i == text.Length) ? 0x00 : BitConverter.GetBytes(text[i])[0]);
            bool b1 = GetBit(b, 1);
            bool b2 = GetBit(b, 2);
            bool b3 = GetBit(b, 3);
            bool b4 = GetBit(b, 4);
            bool b5 = GetBit(b, 5);
            bool b6 = GetBit(b, 6);
            bool b7 = GetBit(b, 7);
            bool b8 = GetBit(b, 8);
            ModifyPixel(image, i * 3, b1, b2, b3);
            ModifyPixel(image, i * 3 + 1, b4, b5, b6);
            ModifyPixel(image, i * 3 + 2, b7, b8, false);
        }

        return image;
    }

I don't understand why i*3 is being sent as an argument and what significance it holds in the ModifyPixel function below:

    private void ModifyPixel(Bitmap image, int i, bool r, bool g, bool b)
    {
        int x = i % image.Width;
        int y = i / image.Width;
        Color color = image.GetPixel(x, y);
        byte red = ChangeBit(color.R, r, 0x01);
        byte green = ChangeBit(color.G, g, 0x01);
        byte blue = ChangeBit(color.B, b, 0x01);
        Color newColor = Color.FromArgb(color.A, red, green, blue);
        image.SetPixel(x, y, newColor);
    }

Furthermore, why not use image.Height for int y? and how is ChangeBit working to manipulate the R, G and B values of each pixel?

    private static byte ChangeBit(byte i, bool bit, byte index)
    {
        if (bit) return (byte)(i | index);
        return (byte)(i & ~index);
    }
  • Is the question, explain why this (not my) code works? – Biscuits Apr 06 '18 at 20:43
  • You're asking 3 different questions. The first two are linked in the sense they're both asking how the arithmetic used works to access pixels in a sequential manner. The last question is different and has been answered before on this site, i.e., how bitwise operators work. – Reti43 Apr 06 '18 at 20:49
  • @Biscuits yes. Can you explain the process using test cases for variables – Tarun Kumar Apr 06 '18 at 20:53
  • http://graphics.stanford.edu/~seander/bithacks.html – Eser Apr 06 '18 at 20:53

0 Answers0