3

Hi guys I am working on a basic steganography project at my university. I manipulate my bitmap but somewhere there is a bug. I put a breakpoint just to check the values before .save() method and pixels are looking fine but when I save it and open the new image some of the pixels are +-1 bit instate of what was expected. Here is the code:

public class EncryptController
{
    public void EncryptMessage(Bitmap oldImage,string imagePath, DataSteg dataSteg)
    {
        Bitmap newImage = new Bitmap(oldImage.Width, oldImage.Height);
        ImageFormat imageFormat = ImageCheck.GetFormat(oldImage);
        string data = dataSteg.EncryptData();
        int keyLength = data.Length;
        int bitmapPos = 0;

        for (int i = 0, n = oldImage.Size.Width; i < n; i++)
        {
            for (int j = 0, z = oldImage.Size.Height; j < z; j++)
            {   //data = "010010110110000101101100011000110110100001101111"
                if ((Convert.ToInt32(data[bitmapPos].ToString()) != CheckPixelVale(newImage.GetPixel(i, j)))&&keyLength>0)
                {
                    Color newColor = ChangeMaxRate(oldImage.GetPixel(i, j));
                    newImage.SetPixel(i, j, newColor);
                }
                else
                {
                    Color newColor = oldImage.GetPixel(i, j);
                    newImage.SetPixel(i, j, newColor);
                }

                if (bitmapPos < data.Length-1)
                {
                    keyLength--;
                    bitmapPos++;
                }
            }
        }  

        string pt = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\fl\roseEnc.jpg";
        oldImage.Dispose();
        newImage.Save(pt, imageFormat);
    }

    private Color ChangeMaxRate(Color color)
    {
        int red = color.R;
        int blue = color.B;
        int green = color.G;

        if (red + blue + green < 255 * 3)
        {
            if ((red >= blue && red >= green) && red<255)
            {
                red++;
            }
            else if ((blue >= red && blue >= green) & blue < 255)
            {
                blue++;
            }
            else if ((green >= blue && green >= red) && green < 255)
            {
                green++;
            }
        }
        else
        {
            red--;
        }

        return Color.FromArgb(red, green, blue);
    }

    private int CheckPixelVale(Color color)
    {
        return (color.R + color.B + color.G) % 2;
    }
}
4
  • 1
    "...all of the pixels are -1 bit instate of what was expected..." - huh?
    – user585968
    Commented Mar 12, 2018 at 5:57
  • Yes I would like the sum to be odd if the current data[index] is "1" and even if it's "0" so I check the sum of RGB and if there is deference add 1 to the maximum of RGB so it the sum goes from odd to even and backward. It works but there is deference when I save it. Commented Mar 12, 2018 at 6:25
  • 2
    Using jpg makes it very very hard. With such a simplistic approach you need to go for png, as Rich suggests!
    – TaW
    Commented Mar 12, 2018 at 9:14
  • Isn't "-1" just "FFFFFFFF", aka, fully opaque white?
    – Nyerguds
    Commented Mar 12, 2018 at 9:18

1 Answer 1

5

I suspect the problem may be the filetype that you're using. It might be a deception, but the filename in the code is JPG, which is a 'lossy' format. The image will look the same to a human, but will may not contain the exact bits that you saved. Try using a PNG image format.

You would have better separation of concerns by passing in the "string data", rather than "DataSteg k". This would bring it closer to being a MCVE. No need to pass the first parameter by ref. I'd suggest that its a bad design for this method to dispose of a parameter that it has been given. Also, unless I've missed something string t is unused.

1
  • Tnx I fix it. JPG format was the problem. I want to manipulate the data in this DataSteg that's why I use it. And yes t was just for testing. xaxa :) Commented Mar 12, 2018 at 18:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.