Welcome Guest!
 VB 6 Helper
 Previous Message All Messages Next Message 
VB Helper Newsletter  Rod Stephens
 Nov 04, 2011 14:58 PST 

Riddle: How can you store two pictures in the same bitmap?

Answer: Steganography.

This week I've posted two extremely cool steganography applications
(each in VB 6 and VB .NET flavors) that hide one picture inside another.
Even though I knew more or less what to expect, I was dazzled by the
result when I used 4 bits to hide one picture inside another. It's
almost like magic.

The programs are also remarkably simple (simpler in .NET than in VB 6).
Download them and take a look.
-----
John Mueller posted a review of my book "Rod Stephens' Visual Basic
Programming 24-Hour Trainer" on his blog at:

    
http://blog.johnmuellerbooks.com/2011/04/14/rod-stephens-visual-basic-programming-24-hour-trainer.aspx


It's a pretty good summary of the book's topics and approach, although
there's one small mistake that most reviewers and readers make. The
word's "24-Hour Trainer" mean the book is available for you to use 24
hours a day not that you should be able to finish the book in 24 hours.
John suggests allowing at least 40 hours to work through the book and
that seems reasonable to me. There are 39 lessons (similar to chapters)
in the book and some could easily take more than an hour if you work
through them thoroughly.

If you're ready to move to .NET and want a basic introduction, take a
look at the book. Or if you know someone else who wants to learn to
program for the first time, suggest that they take a look at it.
-----
Updated Book: Rod Stephens' Visual Basic Programming 24-Hour Trainer
http://www.vb-helper.com/24hourvb.html

In that same book, Frank noticed on page 133 the rather confusing entry
"-3 is -3" in the table. The idea here is that the "-" symbol represents
negation as you probably already know it.

Perhaps a better entry for the table would be:

    -(3) is -3

Hopefully the idea of negation is familiar enough that this didn't
confuse too many people
-----
Now available:
    Start Here! Fundamentals of Microsoft .NET Programming
    http://shop.oreilly.com/product/0790145330536.do

On sale at $7.99 for a limited time at:

    http://shop.oreilly.com/product/0790145330536.do

Also available:
    Start Here! Learn Microsoft Visual C# 2010
    By John Paul Mueller
    http://shop.oreilly.com/product/0790145319210.do

If you buy John's book, you will receive a coupon that lets you get my
book in electronic format for free (because it's supposed to supplement
the other books).
-----
Have a great week and thanks for subscribing!

Rod
RodSte-@vb-helper.com

Twitter feeds:
    VBHelper
    CSharpHelper
==========
==========

    VB6 Contents:
1. New HowTo: Use steganography to hide one picture inside another in
Visual Basic 6
2. New HowTo: Use steganography to hide one large picture inside another
in Visual Basic 6
==========
++++++++++
       <VB6>
++++++++++
==========
1. New HowTo: Use steganography to hide one picture inside another in
Visual Basic 6
http://www.vb-helper.com/howto_steganography_hide_image.html
http://www.vb-helper.com/HowTo/howto_steganography_hide_image.zip

Picture at
http://www.csharphelper.com/howto_steganography_hide_image.png.

The example "Hide a message inside a picture (steganography)"
(http://www.vb-helper.com/howto_steganography.html) hides message bits
in the least-significant color bits of randomly selected points within
an image. The idea is that small changes to the least significant bits
of a color won't be noticeable. For example, if a pixel's red color
component is 254 instead of 255, no one will notice.

In fact, you can make a whole lot more changes without seriously
altering an image.

To see why, notice that the most significant bit in an 8-bit binary
value contributes fully half of the total value. If you change that bit,
you change the value by 128, half of the possible number of values 256.

The next most significant bit accounts for half of the value that you
can create with the remaining 7 bits. Changing that bit alters the value
by 64, half of the number of possible values with 7 bits 128.

Together the two most significant bits account for 3/4 of the total
value.

If you continue this line of reasoning, the 3 most significant bits
account for roughly 7/8 of the total, the 4 most significant bits
account for 15/16 of the total, and so forth. Even if you take away 4 of
a pixel's 8 bits of red, green, and blue color information, the
resulting color is pretty close to the original color. Instead of
storing only 1 bit of information in pixels scattered around the image,
you can store several bits in every red, green, and blue color value
throughout the image.

This example does just that. It takes away the least significant pixels
from one image and uses them to store the most significant pixels of a
second hidden image. The hidden image's values are stored in the result
image's least significant bits so they don't add greatly to the
resulting combined image.

For example, suppose you want to use 3 bits to hide one image inside
another and consider the red component of a particular pixel. Suppose
the visible image's red component for that pixel in binary is 10110101
and the hidden image's red component for that pixel is 01010011. To use
3 bits to hide the second value inside the value, we remove the 3
*least* significant bits of the first value and replace them with the
*most* significant bits of the second value. In this example, 10110101 +
01010011 = 10110010.

To see that the change is small, note that the original pixel's value
was 10110101 = 181 and the final value is 10110010 = 178. We stored 3
bits from the hidden value but only changed the original value by a
small amount.

To recover the hidden image, you extract the combined image's 3 least
significant bits and use them for the hidden image's most significant
bits. In this example, 10110010 gives the original image's value as
10110000 and the hidden image's value is 01000000. These values are
slightly different from the original values but they're close enough to
be useful.

The following code shows how the example program hides one image inside
another.

' Hide pic_hidden inside pic_visible and place the result in pic_result.
Public Sub HideImage(ByVal pic_visible As PictureBox, ByVal pic_hidden
As PictureBox, ByVal pic_combined As PictureBox, ByVal hidden_bits As
Integer)
Dim visible_mask As Integer
Dim hidden_mask As Integer
Dim shift As Integer
Dim x As Integer
Dim y As Integer
Dim r_visible As Byte
Dim g_visible As Byte
Dim b_visible As Byte
Dim r_hidden As Byte
Dim g_hidden As Byte
Dim b_hidden As Byte
Dim r_combined As Byte
Dim g_combined As Byte
Dim b_combined As Byte

    shift = 2 ^ (8 - hidden_bits)
    visible_mask = &HFF * 2 ^ hidden_bits
    hidden_mask = &HFF \ shift
    For x = 0 To pic_visible.ScaleWidth - 1
        For y = 0 To pic_visible.ScaleHeight - 1
            UnRGB pic_visible.Point(x, y), r_visible, g_visible,
b_visible
            UnRGB pic_hidden.Point(x, y), r_hidden, g_hidden, b_hidden
            r_combined = (r_visible And visible_mask) + ((r_hidden \
shift) And hidden_mask)
            g_combined = (g_visible And visible_mask) + ((g_hidden \
shift) And hidden_mask)
            b_combined = (b_visible And visible_mask) + ((b_hidden \
shift) And hidden_mask)
            pic_combined.PSet (x, y), RGB(r_combined, g_combined,
b_combined)
        Next y
    Next x
End Sub

The code first makes bit masks to extract the bits from the visible and
hidden images. It creates a new bitmap to store the result and then
loops over the images' pixels. For each pixel, the code gets the color
component values for the visible and hidden pixels. It uses the masks to
clear the unwanted bits from each value and then combines them, shifting
the hidden value's bits so they move into the least significant bit
positions.

The following code shows how the program recovers an image hidden inside
another image.

' Recover a hidden image.
Public Sub RecoverImage(ByVal pic_combined As PictureBox, ByVal
pic_recovered As PictureBox, ByVal hidden_bits As Integer)
Dim shift As Integer
Dim hidden_mask As Integer
Dim x As Integer
Dim y As Integer
Dim r_combined As Byte
Dim g_combined As Byte
Dim b_combined As Byte
Dim r_recovered As Byte
Dim g_recovered As Byte
Dim b_recovered As Byte
    
    shift = 2 ^ (8 - hidden_bits)
    hidden_mask = &HFF \ shift
    For x = 0 To pic_combined.ScaleWidth - 1
        For y = 0 To pic_combined.ScaleHeight - 1
            UnRGB pic_combined.Point(x, y), r_combined, g_combined,
b_combined
            r_recovered = (r_combined And hidden_mask) * shift
            g_recovered = (g_combined And hidden_mask) * shift
            b_recovered = (b_combined And hidden_mask) * shift
            pic_recovered.PSet (x, y), RGB(r_recovered, g_recovered,
b_recovered)
        Next y
    Next x
End Sub

This code loops over the combined image's pixels. It pulls the least
significant bits out of each pixel's color components and shifts them so
they become the most significant bits in the recovered image's pixels.

The following code shows how the program uses these methods.

' Hide and then recover the image.
Private Sub cmdGo_Click()
Dim num_bits As Integer

    MousePointer = vbHourglass

    ' Hide the image.
    num_bits = Val(txtNumBits.Text)
    HideImage picVisible, picHidden, picCombined, num_bits

    ' Recover the hidden image.
    RecoverImage picCombined, picRecovered, num_bits

    MousePointer = vbDefault
End Sub

This code gets the number of bits to shift from the user's selection. It
then calls the HideImage method to hide the image in the picHidden
PictureBox inside the image displayed by the picVisible PictureBox and
displays the result.

Next the code calls RecoverImage to extract the hidden image and
displays the result.

If you look closely at the picture shown here, you'll see that the
combined image looks very much like the original image and the recovered
hidden image looks very much like its original value. By using 4 bits,
you get a pretty remarkable result. If you experiment with other numbers
of bits such as 1 or 7, you'll see lots of degradation in one image or
the other.

This method works well for the photographs used here. It might not work
as well for large geometric shapes of constant colors. For example, if
you hid a series of vertical stripes inside an image containing a large
rectangle, you might see the stripes showing through.
==========
2. New HowTo: Use steganography to hide one large picture inside another
in Visual Basic 6
http://www.vb-helper.com/howto_steganography_hide_image2.html
http://www.vb-helper.com/HowTo/howto_steganography_hide_image2.zip

Picture at http://www.vb-helper.com/howto_steganography_hide_image2.png.

The example "Use steganography to hide one picture inside another in
Visual Basic 6
(http://www.vb-helper.com/howto_steganography_hide_image.html) explains
how you can hide one image inside another. This example uses the same
tools to hide a large image inside another large image so you can get a
better view of the results. Switch back and forth between the images to
see how much they changed.

The images used here are 600 x 700 pixels = 420,000 pixels so they
contain 3 * 420,000 = 1,260,000 color component values. If you use 4
bits to store hidden information, which seems to be about the most you
can reasonably get away with without noticeably altering the visible
image, then you can store 4 * 1,260,000 bits or 630,000 bytes of hidden
information.
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Twitter feeds:
    VBHelper
    CSharpHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.