Jump to content
Nytro

Steganography: Simple Implementation in C#

Recommended Posts

Posted

[h=1]Steganography: Simple Implementation in C#[/h]By Hamzeh soboh, 31 Oct 2013

[h=2]Introduction[/h] Steganography is the art and science of hiding information by embedding messages within others. Steganography works by replacing bits of useless or unused data in regular computer files with bits of different, invisible information. This hidden information can be plain text, cipher text, or even images

[*].

I've implemented two methods that can be helpful to embed/extract a text in/from an image. The first one is embedText that receives the text you want to embed and the Bitmap object of the original image (your image before embedding the text in it), and returns the Bitmap object of the image after embedding the text in it. Then you can export the Bitmap object into an image file. It is optional to encrypt your text before starting the process for extra security. You are free then to send the result image by email or keep it on your flash memory for example.

The second method is extractText that receives the Bitmap object of the processed image (the image that has already been used to embed the text in), and returns the text that has been extracted.

[h=2]Executing the Code[/h]If you download the attached testing project, it allows you to open an image, write your text or import a text file, optionally encrypt your text before starting processing, embed the text in the image, and finally save the result image into an image file. Then you can reopen that application and the image that you've saved, and extract the text from it.

ZN02E.jpg

[h=2]Using the Code[/h] The SteganographyHelper class contains the needed methods to embed/extract a text in/from an image.

CAUTION: Don't save the result image in a lossy format (like JPEG); your data will be lost. Saving it as PNG is pretty good.

    class SteganographyHelper
{
enum State
{
hiding,
filling_with_zeros
};

public static Bitmap embedText(string text, Bitmap bmp)
{
State s = State.hiding;

int charIndex = 0;
int charValue = 0;
long colorUnitIndex = 0;

int zeros = 0;

int R = 0, G = 0, B = 0;

for (int i = 0; i < bmp.Height; i++)
{
for (int j = 0; j < bmp.Width; j++)
{
Color pixel = bmp.GetPixel(j, i);

pixel = Color.FromArgb(pixel.R - pixel.R % 2,
pixel.G - pixel.G % 2, pixel.B - pixel.B % 2);

R = pixel.R; G = pixel.G; B = pixel.B;

for (int n = 0; n < 3; n++)
{
if (colorUnitIndex % 8 == 0)
{
if (zeros == 8)
{
if ((colorUnitIndex - 1) % 3 < 2)
{
bmp.SetPixel(j, i, Color.FromArgb(R, G, );
}

return bmp;
}

if (charIndex >= text.Length)
{
s = State.filling_with_zeros;
}
else
{
charValue = text[charIndex++];
}
}

switch (colorUnitIndex % 3)
{
case 0:
{
if (s == State.hiding)
{
R += charValue % 2;

charValue /= 2;
}
} break;
case 1:
{
if (s == State.hiding)
{
G += charValue % 2;

charValue /= 2;
}
} break;
case 2:
{
if (s == State.hiding)
{
B += charValue % 2;

charValue /= 2;
}

bmp.SetPixel(j, i, Color.FromArgb(R, G, );
} break;
}

colorUnitIndex++;

if (s == State.filling_with_zeros)
{
zeros++;
}
}
}
}

return bmp;
}

public static string extractText(Bitmap bmp)
{
int colorUnitIndex = 0;
int charValue = 0;

string extractedText = String.Empty;

for (int i = 0; i < bmp.Height; i++)
{
for (int j = 0; j < bmp.Width; j++)
{
Color pixel = bmp.GetPixel(j, i);

for (int n = 0; n < 3; n++)
{
switch (colorUnitIndex % 3)
{
case 0:
{
charValue = charValue * 2 + pixel.R % 2;
} break;
case 1:
{
charValue = charValue * 2 + pixel.G % 2;
} break;
case 2:
{
charValue = charValue * 2 + pixel.B % 2;
} break;
}

colorUnitIndex++;

if (colorUnitIndex % 8 == 0)
{
charValue = reverseBits(charValue);

if (charValue == 0)
{
return extractedText;
}

char c = (char)charValue;

extractedText += c.ToString();
}
}
}
}

return extractedText;
}

public static int reverseBits(int n)
{
int result = 0;

for (int i = 0; i < 8; i++)
{
result = result * 2 + n % 2;

n /= 2;
}

return result;
}
}

You can use the following code snippet to save the result image:

        private void saveImageAs()
{
SaveFileDialog save_dialog = new SaveFileDialog();
save_dialog.Filter = "PNG|*.png|Bitmap|*.bmp";

if (save_dialog.ShowDialog() == DialogResult.OK)
{
switch (save_dialog.FilterIndex)
{
case 0:
{
bmp.Save(save_dialog.FileName, ImageFormat.Png);
}break;
case 1:
{
bmp.Save(save_dialog.FileName, ImageFormat.Bmp);
} break;
}
}
}

[h=2]License[/h] This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Sursa: Steganography: Simple Implementation in C# - CodeProject

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...