Steganography

13. June 2010

I was reading about steganography this week and decided to have a play about. Here is the code I wrote:

 my code

Maybe with these three methods you can get it out, method 1:

// Decrypts a bitmap into a text file
private static void DecryptFile()
{
    // Get the bitmap
    FileInfo image = GetFileInfo("Enter path to encrypted bitmap:", true);
    Bitmap bmp;

    try
    {
        bmp = (Bitmap)Bitmap.FromFile(image.FullName);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine("Aborting");
        return;
    }

    int x = 0;
    int y = 0;

    StringBuilder stringBuilder = new StringBuilder();

    // Get the 3 characters from each pixel
    while (y < bmp.Height)
    {
        Color color = bmp.GetPixel(x, y);

        stringBuilder.Append(ColorToString(color));

        x++;

        if (x >= bmp.Width)
        {
            x = 0;
            y++;
        }
    }

    // Save decrypted text to file
    FileInfo saveFile = GetFileInfo("Enter path to save output:", false);

    try
    {
        using (StreamWriter stream = saveFile.AppendText())
        {
            stream.Write(stringBuilder.ToString());
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine("Aborting");
        return;
    }

    Console.WriteLine("Saved to " + saveFile.FullName);
}

 

Nice and simple, method 2:

// Convert a color to 3 char string
private static string ColorToString(Color color)
{
    return ASCIIEncoding.ASCII.GetString(new byte[] { color.R, color.G, color.B });
}

 

I’m not sure if the recursive calls are the best way to do this but it works well, method 3:

// Gets a FileInfo object by path input from the user
private static FileInfo GetFileInfo(string message, bool mustExist)
{
    Console.WriteLine(message);

    string filepath = Console.ReadLine();

    if (string.IsNullOrEmpty(filepath))
    {
        return GetFileInfo(message, mustExist);
    }

    FileInfo fileInfo;

    try
    {
        fileInfo = new FileInfo(filepath);
    }
    catch (Exception e)
    {
        // invalid file
        Console.WriteLine(e.Message);
        return GetFileInfo(message, mustExist);
    }

    if (!mustExist || fileInfo.Exists)
    {
        return fileInfo;
    }

    Console.WriteLine("File does not exist");

    return GetFileInfo(message, mustExist);
}

 

It’s not quite obscure enough yet but I will be doing some more reading into this and try to do more. For anyone interested the wikipedia page is a good place to start and has some interesting examples.

.NET , , ,