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
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)
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 bitindex As Integer
bitindex = 0
For Each c As Char In plainText
Dim x, y As Byte
x = AscW(c)
Dim i As Integer
i = 7
For i = 7 To 0 Step -1
y = x >> i
y = y Mod 2
Dim bit As Integer
bit = bitMapData(bitindex)
bitMapData(bitindex) = bitMapData(bitindex) - (bitMapData(bitindex) Mod 2)
bitMapData(bitindex) = bitMapData(bitindex) + y
bit = bitMapData(bitindex)
If (((bitindex + 2) Mod 4) = 0) Then
bitindex = bitindex + 2
Else
bitindex = bitindex + 1
End If
Next
Next
For i = 0 To 7 Step +1
bitMapData(bitindex) = bitMapData(bitindex) - bitMapData(bitindex) Mod 2
If (((bitindex + 2) Mod 4) = 0) Then
bitindex = bitindex + 2
Else
bitindex = bitindex + 1
End If
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)
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
Next
If charvalue = 0 Then
Return PlainText
End If
charvalue = reverseBits(charvalue)
Dim c As Char
c = Convert.ToChar(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