Click here to Skip to main content
12,752,634 members (40,600 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I have encoded a message in the lsb of each pixel. How would i retrieve it? When i read the LSB of each pixel do input each bit into a byte array or a zero/one into a char array? Unsure what encoding function to call then? I should use a foreach loop and Encoding.UTF8.GetString(Message)but all the ones and zeros from the char array into a single string?

I have just modified my code. The message i pull form the image i put into a
stringbuilder object consist of ones and zeros. After the indexed for loops, one can see i have tried various methods but the same value is returned, any suggestions
private void button2_Click(object sender, EventArgs e)
        {
            int c = sb.Length,x,y,z=0;
            char[] Message= new char[c];
            StringBuilder RetreivedMessage = new StringBuilder();
            for (x = 0, y = 0; x < image1.Width && z < c; x++)
            {
                for (y = 0; y < image1.Height && z < c; y++)
                {
                    Color pixelColor = image1.GetPixel(x, y);
                    string binary1 = Convert.ToString(pixelColor.R, 2);
                    //byte NewRed, NewGreen, NewBlue;
                    if (binary1[binary1.Length -1] == '0')
                    {
                        RetreivedMessage.Append('0');
                        z++;
                        if (z == c)
                        {
                            break;
                        }

                    }
                    else
                    {
                        RetreivedMessage.Append('1');
                        z++;
                        if (z == c)
                        {
                            break;
                        }

                    }
                    binary1 = Convert.ToString(pixelColor.G, 2);
                    //byte NewRed, NewGreen, NewBlue;
                    if (binary1[binary1.Length - 1] == '0')
                    {
                        RetreivedMessage.Append('0');
                        z++;
                        if (z == c)
                        {
                            break;
                        }

                    }
                    else
                    {
                        RetreivedMessage.Append('1');
                        z++;
                        if (z == c)
                        {
                            break;
                        }

                    }
                    binary1 = Convert.ToString(pixelColor.B, 2);
                    //byte NewRed, NewGreen, NewBlue;
                    if (binary1[binary1.Length - 1] == '0')
                    {
                        RetreivedMessage.Append('0');
                        z++;
                        if (z == c)
                        {
                            break;
                        }

                    }
                    else
                    {
                        RetreivedMessage.Append('1');
                        z++;
                        if (z == c)
                        {
                            break;
                        }

                    }
                    //string binary1 = Convert.ToString(pixelColor.R, 2);
                    //char last1 = binary1[binary1.Length - 1];
                }
            }
            string FinalRetreivedMessage  = RetreivedMessage.ToString();
            //Byte[] buf = Encoding.Unicode.GetBytes(RetreivedMessage.ToString());
            Byte[] buf = Encoding.Unicode.GetBytes(RetreivedMessage.ToString());
            string result = System.Text.Encoding.Unicode.GetString(buf);
            //String result = Encoding.Unicode.GetString(buf);
            StringBuilder r2 = new StringBuilder();
            //foreach (Byte b in Encoding.Unicode.GetBytes(FinalRetreivedMessage))
           // {
           //    r2.Append(Convert.ToString());
            //}
        }
Posted 18-Nov-12 1:22am
v3
Comments
Christian Graus 18-Nov-12 15:53pm
   
You reverse the process you used to put it in there, surely ?
Darith1974 21-Nov-12 5:07am
   
string a ="te";
StringBuilder r2 = new StringBuilder();
foreach (Byte b in Encoding.Unicode.GetBytes(a))
{
r2.Append(Convert.ToString(b,2));
}

Thanks your reply has substance in that i used the above code to create the initial string so i should look at this code to solve my challenge
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

string FinalRetreivedMessage  = RetreivedMessage.ToString();
            int count = FinalRetreivedMessage.Length / 8;
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            UTF8Encoding enc = new UTF8Encoding();
            var StringBytes = new byte[count];
            for (int i = 0; i < count; i++)
                StringBytes[i] = Convert.ToByte(FinalRetreivedMessage.Substring(i * 8, 8), 2);
                //StringBytes[i] = encoding.GetBytes(FinalRetreivedMessage.Substring(i * 8, 8), 2);
            string FinalResult = enc.GetString(StringBytes);
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

string FinalRetreivedMessage  = RetreivedMessage.ToString();
            int count = FinalRetreivedMessage.Length / 8;
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            UTF8Encoding enc = new UTF8Encoding();
            var StringBytes = new byte[count];
            for (int i = 0; i < count; i++)
                StringBytes[i] = Convert.ToByte(FinalRetreivedMessage.Substring(i * 8, 8), 2);
                //StringBytes[i] = encoding.GetBytes(FinalRetreivedMessage.Substring(i * 8, 8), 2);
            string FinalResult = enc.GetString(StringBytes);
  Permalink  
Comments
Gbolahan OIladele Taiwo 10-May-16 0:16am
   
Hello Hamzeh Soboh. I downloaded your code on steganography (c#) to guide me to development mine for my project yet i'm trying to run it. How do i run your code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100