Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bitmap Encryption(General Query)

Status
Not open for further replies.

nikhilparchure

Programmer
Oct 6, 2001
121
0
0
AU
hi ppl ,
This is something that i have noticed and like to
know how it works.
I have heard message can sent using bitmaps.
i.e Messages or simple txt for that sake
can be encryted in the bitmap and further retrieved
Can anybody help me out with this

thnks
Nikhil
 
I remeber a small program called SNOW that did this, I found it on the internet, but I can not rember where. Try searching ENCRYPT SNOW

Paul
 
Steganography - that's the one! Stenography is completely different. I blame Xmas for the brain malfunction...
 
The links Palva listed above will give you some good general information on steganography. Here's two "home grown" solutions you might try before you immerse yourself in the subject. Note that these make no attempt to "encrypt" the data. You will have to write your own routine or use a standard method to encrypt the data before embedding it in the image. Also note that these approaches, as written, will only work on the BMP file format.

Place a DriveListBox, a DirListBox and a FileListBox on a form (these will be used to select the starting image - set the Pattern property on the FileListBox to "*.BMP"). Add two TextBoxes (one will receive a password - name it "txtPassword" - and the other will contain the message you want to hide in the image), two PictureBoxes (to show the image before and after modification) and four CommandButtons. The first button appends the text in Text1 to the BMP file (useful if the message is as large or larger than the original image). The second button mixes the message in the image, storing the size of the message arbitrarily at offset 1079 (the image must be large enough to hold the message). Button 3 and 4 just retrieve the stored messages and places them back in Text1.
[tt]
Private Sub Form_Load()
' Get the first BMP in the Windows folder
' just to make sure Picture1 holds an image...

SampleBMP$ = Dir(Environ$("WINDIR") & "\*.BMP")
If SampleBMP$ <> &quot;&quot; Then
Picture1.Picture = LoadPicture(Environ$(&quot;WINDIR&quot;) & &quot;\&quot; & SampleBMP$)
End If
' Place a string in the password box.
txtPassword.Text = &quot;f3W#1&quot;
Command1.Caption = &quot;Append Data In Text1 To BMP&quot;
Command2.Caption = &quot;Mix Data in Text1 Randomly in the BMP&quot;
Command3.Caption = &quot;Fill Text1 With Data Appended In BMP&quot;
Command4.Caption = &quot;Fill Text1 With Data Mixed In BMP&quot;
Mess$ = &quot;Example of data to store in BMP file.&quot; & vbCrLf
Mess$ = Mess$ & &quot;The data can be of any size or form,&quot; & vbCrLf
Mess$ = Mess$ & &quot;when it is simply appended to the BMP.&quot; & vbCrLf & vbCrLf
Mess$ = Mess$ & &quot;The BMP should be at least&quot; & vbCrLf
Mess$ = Mess$ & &quot;1079 bytes + the length of the message when&quot; & vbCrLf
Mess$ = Mess$ & &quot;embeding the message in the image.&quot;
Text1.Text = Mess$
Dir1 = Drive1
File1.Pattern = &quot;*.BMP&quot;
File1 = Dir1
End Sub

Private Sub Dir1_Change()
File1 = Dir1
End Sub

Private Sub Drive1_Change()
Dir1 = Drive1
End Sub

Private Sub File1_Click()
Fname$ = Dir1.Path
If Right$(Fname$, 1) <> &quot;\&quot; Then
Fname$ = Fname$ & &quot;\&quot;
End If
Fname$ = Fname$ & File1.FileName
Picture1.Picture = LoadPicture(Fname$)
End Sub

Private Sub Command1_Click()
' This will append the contents of Text1 to a BMP file.
' The file will appear to be normal in all respects but
' it will contain an unencrypted message.
' This approach does not degrade image quality
' but it allows inspection of the message by using a HEX editor.
' The message should be encrypted before it is appended to the BMP.

' Save the bitmap in Picture1 to a file.
' We'll use &quot;MyStego.bmp&quot; throughout the examples.

SavePicture Picture1.Picture, App.Path & &quot;\MyStego.bmp&quot;
DoEvents
Fname$ = App.Path & &quot;\&quot; & &quot;MyStego.bmp&quot;
' Get the physical file size.
TotalSize& = FileLen(Fname$)
ff = FreeFile
Open Fname$ For Binary As #ff
' Get the original file size.
Get #ff, 3, ImageSize&
If TotalSize& > ImageSize& Then
' File already contains data.
x = MsgBox(&quot;Write over existing data?&quot;, vbYesNo)
If x <> vbYes Then
Close
Exit Sub
End If
End If
Pu$ = Text1.Text
' Append the contents of Text1 to the file.
Put #ff, ImageSize& + 1, Pu$
Close #ff
DoEvents
' Load the image into Picture2 to see what it looks like.
' (It looks just like the original.)

Picture2.Picture = LoadPicture(App.Path & &quot;\MyStego.bmp&quot;)
End Sub

Private Sub Command2_Click()
' This will mix the contents of Text1 &quot;randomly&quot; in a BMP file.
' This approach degrades image quality, placing dots throughout
' the picture. It doesn't allow inspection of the message
' with HEX editor because the characters in the message are
' &quot;randomly&quot; distributed over the image. Still, the message should be
' encrypted before it is mixed with the BMP.

' Save the bitmap in Picture1 to a file.

SavePicture Picture1.Picture, App.Path & &quot;\MyStego.bmp&quot;
DoEvents

' Create a randomizing seed from the password.
PWD$ = txtPassword.Text
For P = 1 To Len(PWD$)
RNDseed& = RNDseed& + (Asc(Mid$(PWD$, P, 1)) * P)
Next
a = Rnd(-1)
Randomize RNDseed&

Gin$ = Text1.Text
StartByte& = 1079
f2 = FreeFile
Open App.Path & &quot;\MyStego.bmp&quot; For Binary As #f2
Gout$ = String$(LOF(f2) - StartByte&, 0)
Get #f2, StartByte&, Gout$

TextLen& = Len(Gin$)
' The length of the text message
' will be stored at offset 1075.

Put #f2, 1075, TextLen&
Close #f2
Temp$ = String$(Len(Gout$), 255)
TempLen& = Len(Gout$)
For Re = 1 To TextLen&
RandPos& = NextRandLoc&(Temp$)
' Write the next character in the message
' at a &quot;random&quot; location in the image.

Mid$(Gout$, RandPos&, 1) = Chr$(Asc(Mid$(Gin$, Re, 1)))
' Mark that location as having been written once.
Mid$(Temp$, RandPos&, 1) = Chr$(1)
Next

f2 = FreeFile
Open App.Path & &quot;\MyStego.bmp&quot; For Binary As #f2
Put #f2, StartByte&, Gout$
Close #f2
DoEvents
' Load the image into Picture2 to see what it looks like.
' (It looks just like the original but it is flecked with spots.)

Picture2.Picture = LoadPicture(App.Path & &quot;\MyStego.bmp&quot;)
End Sub

Private Sub Command3_Click()
Text1.Text = &quot;&quot;
MsgBox &quot;Text1 cleared. Click Ok to get encrypted text.&quot;

PWD$ = txtPassword.Text
For P = 1 To Len(PWD$)
RNDseed& = RNDseed& + (Asc(Mid$(PWD$, P, 1)) * P)
Next
a = Rnd(-1)
Randomize RNDseed&

Fname$ = App.Path & &quot;\&quot; & &quot;MyStego.bmp&quot;
' Get the physical file size.
TotalSize& = FileLen(Fname$)
ff = FreeFile
Open Fname$ For Binary As #ff
' Get the original file size.
Get #ff, 3, ImageSize&
If TotalSize& <= ImageSize& Then
MsgBox &quot;Picture doesn't contain data.&quot;
Close
Exit Sub
End If
Ge$ = String$(TotalSize& - ImageSize&, 32)
' Retrieve the previously stored data.
Get #ff, ImageSize& + 1, Ge$
Close #ff
Text1.Text = Ge$
End Sub

Private Sub Command4_Click()
Text1.Text = &quot;&quot;
MsgBox &quot;Text1 cleared. Click Ok to get encrypted text.&quot;
' Create a random seed from the password
' in txtPassword.

PWD$ = txtPassword.Text
For P = 1 To Len(PWD$)
RNDseed& = RNDseed& + (Asc(Mid$(PWD$, P, 1)) * P)
Next
a = Rnd(-1)
Randomize RNDseed&
StartByte& = 1079
f2 = FreeFile
Open App.Path & &quot;\MyStego.bmp&quot; For Binary As #f2
' The length of the text message was stored at
' the 1075th offset. Get the value.

Get #f2, 1075, TextLen&
Gout$ = String$(LOF(f2) - StartByte&, 0)
Get #f2, StartByte&, Gout$
Close #f2
Temp$ = String$(Len(Gout$), 255)
ClearText$ = &quot;&quot;
For Re = 1 To TextLen&
RandPos& = NextRandLoc&(Temp$)
' Get the text from the various &quot;random&quot; locations
' where it was stored in the BMP.

ClearText$ = ClearText$ & Mid$(Gout$, RandPos&, 1)
' Mark that location in Temp$ as having been read once.
Mid$(Temp$, RandPos&, 1) = Chr$(1)
Next
Text1.Text = ClearText$
End Sub

Private Function NextRandLoc&(Temp$)
' This just helps to distribute the message
' &quot;randomly&quot; though a string of the right size by finding
' the next unused offset.

Written = False
TempLen& = Len(Temp$)
Do While Not Written
' Try to find an unused random location
' five times. If that fails, use the
' next available location.

For Rep& = 1 To 5 'try 5 times
RandPos& = Fix(TempLen& * Rnd + 1)
If Mid$(Temp$, RandPos&, 1) = Chr$(255) Then
Written = True
Exit For
End If
Next
If Written = True Then Exit Do
RandPos& = InStr(RandPos&, Temp$, Chr$(255))
If RandPos& < 1 Then
RandPos& = InStr(1, Temp$, Chr$(255))
If RandPos& > 0 Then
Written = True
Exit Do
End If
End If
Loop
Mid$(Temp$, RandPos&, 1) = Chr$(1)
NextRandLoc& = RandPos&
End Function
[/tt]
=============================================================
Have fun.


VCA.gif
 
Well guys thank u all for such wonderfull help
Will work on u'r solutions
and get back to u a.s.a.p
take care
Wish U
Merry Christmas and Happy New Year

Nikhil
 
Alt,
Nice code. You are a poet. I have one prob. maybe you can shed some light on. I have created the exe with your code (with some minor alterations). I use the encryption portion of your code. Sometimes when I encrypt the message throught the bit map. I can retrieve the message and sometimes I cannot. Can you tell me what I am doing wrong? I notice it more on larger messages could that be the prob?

Wait patiently for your response

Scoty ::) &quot;Learn from others' mistakes. You could not live long enough to make them all yourself.&quot;
-- Hyman George Rickover (1900-86),
 
What are the symptoms? You might be trying to store text that simply won't fit in the bitmap.

In Sub Command2_Click check to see if Text1 will successfully fit in the BMP.[tt]

TextLen& = Len(Gin$)
If TextLen& > Len(Gout$) Then
Close #f2
MsgBox &quot;The message is too large to store in the image.&quot;
Exit Sub
End If
[/tt]
Let me know. I haven't had any problems with the code... but then, I haven't tested it under a wide range of conditions.

VCA.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top