Digital Disappearing Ink: Steganography in C# - ' The sample program Cont' (
Page 4 of 4 )
.">
The next code of interest in the AddMessage class is the StoreBits method. This method stores a string into the bitmap data array. It takes two arguments: the string containing the data and the number of bits that can be stored in each byte.
// This method stores an entire string
// into the bitmap data array. It takes
// two arguments: the string containing
// the data and the number of bits that
// can be stored in each byte. Once again,
// note that the number of bits can actually
// be different than the number of bits
// that were stripped off.
void StoreBits( string strData, int nBits)
{
// Loop through the string.
for (int i = 0; i < strData.Length; i++)
{
// Get the data byte for this character.
byte Data = (byte)strData[i];
// Start our bit test variable at 1;
byte DataTest = 1;
// Loop through the 8 bits of the data byte.
for (int j = 0; j < 8; j++)
{
// Start by assuming the bit is 0.
bool Bit = false;
if ((Data & DataTest) != 0)
{
// If the bit is a 1, set the
// boolean variable to true.
Bit = true;
}
// Call the StoreBit method
// which will do the actual
// storage into the bitmap
// data array.
StoreBit(Bit, nBits);
// Adjust our bit test variable.
DataTest <<= 1;
}
}
}
The last method that is used by the AddMessage class to perform the message hiding is the btnSave_Click method which simply calls the InitBitStoring method, the StoreBits method, and then saves the bitmap data to disk as a BMP file. The btnSave_Click method can be seen below.
// This method stores the message data into the
// bitmap array (by calling the appriate methods)
// and then saves the bitmap data to a BMP file.
private void btnSave_Click(object sender, EventArgs e)
{
// We need to format the five digit string
// that contains the length of the messagwe
// string.
string strSize = String.Format("{0:00000}",
txtMessage.Text.Length);
// Call the method to initialize
// the bit saving variables.
InitBitStoring();
// Store the number of bits. Note that
// this is saved as a single bit so
// that when the file is read back,
// it's always assumed that the initial
// number of bits is in a single bit.
StoreBits(Convert.ToString(m_nBits), 1);
// Store the size string.
StoreBits(strSize, m_nBits);
// Store the message data.
StoreBits(txtMessage.Text, m_nBits);
try
{
// Create a bitmap object from
// the bitmap data.
Bitmap objBitmap =
BitmapHelper.BitmapFromBitmapData(m_BitmapData);
// Save the bitmap to disk.
objBitmap.Save(txtFilename.Text, ImageFormat.Bmp);
// Show the user that the process is complete.
MessageBox.Show("Done!");
}
catch (Exception ex)
{
// Alert user to the error.
MessageBox.Show(ex.Message.ToString());
}
}
ADVERTISEMENT
To read a bitmap from a BMP file and extract a message, the program simply reverses the process. This code can be seen in the ReadMessage class. The Figure below shows this Windows Form in use.
Conclusion
Steganography is a really intriguing subject. It's not that hard to get started, and I encourage you to try out the demo program.