Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I'm so sorry for not providing a proper description. This is my first post here.
This is a project that embeds text inside a bitmap. I'm editing the Least Significant bits of the RGB components skipping every fourth component just in case the image isn't 24 bits. I debugged the code many times and I guess the embed function works well. I'm unable to recover the message while decrypting the image. Can anyone please go through the disEmbed() function and help me ! Thanks



VB
Module SteganoHelper
    Public Function embed(ByVal workImg As Bitmap, ByVal plainText As String) As Bitmap

        Dim rect As New Rectangle(0, 0, workImg.Width, workImg.Height)
        Dim workImgData As System.Drawing.Imaging.BitmapData
        workImgData = workImg.LockBits(rect, Imaging.ImageLockMode.ReadWrite, workImg.PixelFormat)
        'rect = Nothing

        Dim ptr As IntPtr
        ptr = workImgData.Scan0
        Dim tempsize As Integer
        tempsize = Math.Abs(workImgData.Stride * workImg.Height)
        Dim bitMapData As Byte()
        ReDim bitMapData(tempsize)
        System.Runtime.InteropServices.Marshal.Copy(ptr, bitMapData, 0, bitMapData.Length)
        'Dim R, G, B, A As Byte
        Dim bitindex As Integer
        ' bitindex = 65
        bitindex = 0
      

        For Each c As Char In plainText             'iterating through each character from the text
            Dim x, y As Byte
            x = AscW(c)
            'converting the character to Byte
            Dim i As Integer
            i = 7
            For i = 7 To 0 Step -1
                y = x >> i                                      'Read individual bits from character right to left
                y = y Mod 2
                ' y = x Mod 2
                ' x = x / 2
                'by shifting the required bits to rightmost position and then getting the first digit

                Dim bit As Integer
                bit = bitMapData(bitindex)

                bitMapData(bitindex) = bitMapData(bitindex) - (bitMapData(bitindex) Mod 2)  'LSB of pixel set to 0..
                bitMapData(bitindex) = bitMapData(bitindex) + y   ' one bit of character added to the color component

                bit = bitMapData(bitindex)
                If (((bitindex + 2) Mod 4) = 0) Then
                    bitindex = bitindex + 2                         'ignore every 4th value in case the image supports transparent component RGB'A'RGB'A'RGB'A' 
                Else
                    bitindex = bitindex + 1
                End If
                'bitindex = bitindex + 1
            Next
        Next
        For i = 0 To 7 Step +1                                       'Adding a termination character
            bitMapData(bitindex) = bitMapData(bitindex) - bitMapData(bitindex) Mod 2   'LSB of pixel set to 0

            If (((bitindex + 2) Mod 4) = 0) Then
                bitindex = bitindex + 2
            Else
                bitindex = bitindex + 1
            End If
            ' bitindex = bitindex + 1
            i = i + 1
        Next
        System.Runtime.InteropServices.Marshal.Copy(bitMapData, 0, ptr, bitMapData.Length)

        workImg.UnlockBits(workImgData)
        Return workImg

    End Function
    Public Function disEmbed(ByVal workImg As Bitmap) As String
        Dim PlainText As String
        PlainText = ""
        Dim bitindex As Integer
        bitindex = 0
        Dim rect As New Rectangle(0, 0, workImg.Width, workImg.Height)
        Dim workImgData As System.Drawing.Imaging.BitmapData
        workImgData = workImg.LockBits(rect, Imaging.ImageLockMode.ReadWrite, workImg.PixelFormat)
        'rect = Nothing

        Dim ptr As IntPtr
        ptr = workImgData.Scan0
        Dim tempsize As Integer
        tempsize = Math.Abs(workImgData.Stride * workImg.Height)
        Dim bitMapData As Byte()
        ReDim bitMapData(tempsize)
        System.Runtime.InteropServices.Marshal.Copy(ptr, bitMapData, 0, bitMapData.Length)
        Dim charvalue As Integer

        While vbTrue
            charvalue = 0
            For i = 0 To 7 Step +1
                charvalue = charvalue * 2 + bitMapData(bitindex) Mod 2
                
                If (((bitindex + 2) Mod 4) = 0) Then
                    bitindex = bitindex + 2
                Else
                    bitindex = bitindex + 1
                End If
                'bitindex = bitindex + 1
            Next
            If charvalue = 0 Then
                Return PlainText
            End If
            charvalue = reverseBits(charvalue)
            Dim c As Char
           
            c = Convert.ToChar(charvalue)
            ' c = Chr(charvalue)
            
            PlainText = PlainText + c
        End While

        Return PlainText
    End Function
    Public Function reverseBits(ByVal n As Integer) As Integer

        Dim result As Integer
        result = 0

        For i = 0 To 7 Step +1

            result = result * 2 + n Mod 2

            n /= 2


        Next
        Return result
    End Function
End Module
Posted
Updated 8-Mar-15 9:27am
v3
Comments
Sergey Alexandrovich Kryukov 8-Mar-15 13:18pm    
What, without any explanation of your goals and this code sample? Please use "Improve question".
I'll add proper formatting to your code, please look at the HTML to see how to do it in future.
—SA
A4Abhiraj 8-Mar-15 15:28pm    
Hey. I'm so sorry. This was my first post here. I've updated the question. Please help me . Thanks already .
Richard MacCutchan 8-Mar-15 13:25pm    
Check what?
A4Abhiraj 8-Mar-15 15:30pm    
Sorry for the delayed response. This being my first post, was made in a haste. I've updated the description. I have a project to submit and I've worked a bit on embedding text inside the least significant bytes of any Image. I'm not getting the expected results. Please help.
Thanks in advance :)
Richard MacCutchan 9-Mar-15 4:19am    
I'm not getting the expected results.
You need to explain (in your question) exactly what results you expect, and what you see. We do not know your code, and most people do not have the time to work through it all and try to make it work. We need your help in order to help you.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900