Issue with steganography decoding

Status
Not open for further replies.

Pejura27

Member
Joined
May 3, 2023
Messages
16
Programming Experience
3-5
I am working on steganography in C#.

According to the LSB technique, I store information in the RGB and Alpha channels, but while decoding, the codes are decoded, but the information is incorrect.

Color.FromArgb(A, R, G, B)); Can anyone who has worked on this issue help?

Thank u.
 
For future reference, please provide a meaningful title for your thread. The title should give an indication of the content without our having to open it. Your title is as much use as no title at all.
 
As for the question, please provide ALL the relevant information. Color.FromArgb does what it does so it is not inherently the problem. It is your usage of it that will be the issue but we don't know what that usage is. Provide enough code to actually demonstrate the issue, but no more than is necessary. Explain exactly what you expect to see and exactly what you do see.
 
I understand, sorry. I wrote it like this because I need help on the subject

That's fine, but everyone posts here because they need help on a subject. The thread title should indicate what that subject is.
 
hide codes ;
C#:
 class LSB
    {
        static bool end;
        public static Bitmap HideBitmap(String text, Bitmap bmp)  // HideBitmap adlı bir fonksiyon tanımlanır.
                                                                  // Bu fonksiyon, gizli bir metni bir resim dosyasının piksellerine gömerek gizli mesajın resimde saklanmasını sağlar.
                                                                  // Fonksiyon iki parametre alır: bir metin dizesi ve bir bitmap dosyası.
        {
            bool end = false;
            int charValue = 0;
            int zeros = 0;
            int charIndex = 0;
            long pixelIndex = 0;
            int R = 0, G = 0, B = 0, A = 0;
            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color pixel = bmp.GetPixel(x, y);
                    R = pixel.R - pixel.R % 4;
                    G = pixel.G - pixel.G % 4;
                    B = pixel.B - pixel.B % 4;
                    A = pixel.A - pixel.A % 4;
                    for (int n = 0; n < 4; n++)
                    {
                        if (pixelIndex % 3 == 0 && pixelIndex != 0)
                        {
                            if (end && zeros == 4)
                            {
                                break;
                            }
                            if (charIndex >= text.Length)
                            {
                                end = true;
                            }
                            else
                            {
                                charValue = text[charIndex++];
                            }
                        }
                        switch (pixelIndex % 3)
                        {
                            case 0:
                                if (!end)
                                {
                                    R += charValue % 4;
                                    charValue /= 4;
                                }
                                break;
                            case 1:
                                if (!end)
                                {
                                    G += charValue % 4;
                                    charValue /= 4;
                                }
                                break;
                            case 2:
                                if (!end)
                                {
                                    B += charValue % 4;
                                    charValue /= 4;
                                }
                                break;
                        }
                        pixelIndex++;
                        if (end && (pixelIndex - 1) % 3 == 2)
                        {
                            if ((A & 3) != 0)
                            {
                                A -= (A & 3);
                            }
                            else
                            {
                                if (zeros != 4)
                                {
                                    A += 1;
                                    zeros++;
                                }
                            }
                        }
                    }
                    if (end && zeros == 4)
                    {
                        break;
                    }
                    bmp.SetPixel(x, y, Color.FromArgb(A, R, G, B));
                }
            }
            return bmp;

Decoding codes :
C#:
 public static String ExtractBitmap(Bitmap bmp)
        {
            Color pixel;
            bool found = false;
            int zeros = 0;
            string message = "";
            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    pixel = bmp.GetPixel(x, y);
                    int R = pixel.R % 2;
                    int G = pixel.G % 2;
                    int B = pixel.B % 2;
                    int A = pixel.A % 2;
                    int value = R + G * 2 + B * 4;
                    if ((A & 1) != 0)
                    {
                        value += 8;
                    }
                    if (value == 0)
                    {
                        zeros++;
                    }
                    else
                    {
                        message += (char)value;
                        zeros = 0;
                    }
                    if (zeros == 4)
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    break;
                }
            }
            return message;

        }

        private static int ReverseBits(int charValue)
        {
            int result = 0;
            for (int i = 0; i < 8; i++)
            {
                result = result * 2 + charValue % 2;
                charValue /= 2;
            }
            return result;
        }

      
       
    }
}
Using the codes that I have written above, I am hiding information inside a PNG image. However, the decoding process results in incorrect output. There seems to be an issue somewhere but I cannot find it, it's slipping my attention.
the output looks like this
 

Attachments

  • 12399.jpg
    12399.jpg
    7.6 KB · Views: 7
Last edited by a moderator:
Please read post #4 again and ask yourself whether you followed those instructions. I can only assume that you haven't actually debugged your code, so you need to do that for a start. Set a breakpoint and step through the code line by line, examining the state at each step. When the actual state differs from your expectations, that is where the issue is. Based on that, you can investigate why the state is not as expected. If you need more help from us then you can provide all this relevant information to us.
 
He is deleting messages because you posted no value messages, as well as non-English messages.

Anyway, just scanning your code what jumps out right away for me is that it looks like you are encoding 2 bits at a time, but decoding only 1 bit at a time.

Also, it looks like you are under the mistaken impression that C# characters are 8 bits. C# characters are actually 16 bits.

Any which way, follow @jmcilhinney advice: Use the debugger. Step through your code. During the encoding phase, actually write down notes using pen and paper of what values are being written out. During the decoding phase, compare the values being extracted with your notes.
 
İngilizce olmayan mesajların yanı sıra hiçbir değerli mesaj göndermediğiniz için mesajları siliyor.

Her neyse, sadece kodunuzu taramak benim için hemen ortaya çıkan şey, bir seferde 2 bit kodluyormuşsunuz, ancak bir seferde yalnızca 1 bitin kodunu çözüyormuşsunuz gibi görünüyor.

Ayrıca, C# karakterlerinin 8 bit olduğu yanılgısına kapılmışsınız gibi görünüyor. C# karakterleri aslında 16 bittir.

Hangi şekilde olursa olsun, @jmcilhinney tavsiyesine uyun: Hata ayıklayıcıyı kullanın. Kodunuzu adım adım ilerletin. Kodlama aşamasında, kalem ve kağıt kullanarak hangi değerlerin yazıldığına dair notlar alın . Kod çözme aşamasında, çıkarılan değerleri notlarınızla karşılaştırın .

I came here on the advice that this forum would help with C# and it seems that no one wants to try to help. I have already reviewed the code step by step and posted here because I couldn't solve the problem. There is something I'm missing and I can't figure it out. Of course I know how to review code step by step, I'm not ignorant enough to write here without doing that. Please approach with the intention to help. The advice to examine the codes step by step is not a solution. !
 
I came here on the advice that this forum would help with C# and it seems that no one wants to try to help.
If we didn't want to help then we would have just ignored your question. We are trying to help but we're not magic. We need information with which to work and you need to provide the relevant information.
I have already reviewed the code step by step and posted here because I couldn't solve the problem.
You don't have to solve the problem but, if you have debugged the code properly, as you claim, then you should have additional information to provide to us, like EXACTLY where and how the behaviour of the code differs from your expectations. You should know exactly what you expect the code to do at each step. If you don't then that's part of the problem. If you've debugged then you know exactly what actually happens. Why are you refusing to tell us where and how the two don't match? If you want us to help you, you have to help us do so.
Of course I know how to review code step by step, I'm not ignorant enough to write here without doing that.
How would we know that? Plenty of people are and we don't know anything about you. All we know is what you tell us. If you give no indication that you have debugged your code then why should we assume that you have. If someone actually has debugged their code, I would assume that they tell us what they found, so not telling us what you found implies to me that you haven't debugged your code.
Please approach with the intention to help.
Why do you think we're here at all, volunteering our time for strangers? Maybe it's just a bit arrogant to assume that we're not in the first place. If we aren't giving you the help you want, it's because you haven't provided enough information. You have the power to remedy that.
The advice to examine the codes step by step is not a solution. !
It wasn't meant to be a solution. It was meant to be a step to finding the solution. if you want to find the solution, take the step. Help us help you. If you're just going to post a big wad of code - even then only after prompting - and expect us to take it from there, you're likely to be disappointed. Help us to help you or don't. It's you who'll be missing out if you don't.
 
I have already reviewed the code step by step

There is a difference between "reviewing" and "debugging". We are asking you to do "debugging".
 
There is a difference between "reviewing" and "debugging". We are asking you to do "debugging".

Indeed. If you don't know how to debug then you should stop what you're doing and learn now because it's a vital skill for all developers. Believe me, that advice is helping. As I mentioned earlier, you should set a breakpoint so that execution halts at a relevant point. You can then step through the execution of the code one line at a time. At each step, you can check the values of all your variables and evaluate any other relevant expressions. That way, you can see the actual state of the app all along the way. As I said before, as soon as the actual state doesn't match the expected state, you have found an issue. If you can't work out the solution for yourself, you can tell us what the two states were. We need that information in order to help you. That's why we're asking for it: because we want to help but are currently unable to.
 
@Skydiver @jmcilhinney There is no problem with the hiding code in the coding processes I transmitted. So when hiding a word in a PNG image, it hides it successfully, including the alpha channel. However, when decoding, it decodes the hidden word incorrectly. For example, it decodes the word "trial" in the style of "@€~~```". And there is a problem with the decoding code in my investigations. I don't know if it is a problem in the first loop or the second loop. I made some changes in the if else section, but I didn't get any results. Since steganography is a situation that operates with pixels, I can't find out if it can't capture the last pixel in the decoding process or if there is another problem. What is your advice?
 
Status
Not open for further replies.

Latest posts

Back
Top Bottom