Steganography Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Steganography Help

  1. #1
    Join Date
    Jun 2008
    Posts
    1

    Steganography Help

    Hello all,
    I am fairly new to VB.Net but have been using vb5/6 for a while. I am after some help with the following

    Code:
       Public Sub PicText(ByVal cmd As String, ByVal picpath As String)
    
            Dim PicBuffer As New System.IO.FileInfo(picpath)
            Dim Ready As Boolean = True
            Dim PicFileStream As System.IO.FileStream
            Try
                PicFileStream = PicBuffer.OpenRead
            Catch ex As Exception
    
            End Try
            If Ready = True Then
                Dim PicBytes As Long = PicFileStream.Length
                Dim PicExt As String = PicBuffer.Extension
                PicBuffer = Nothing
    
                Dim PicByteArray(PicBytes) As Byte
                PicFileStream.Read(PicByteArray, 0, PicBytes)
                PicFileStream.Close()
                PicFileStream.Dispose()
                Dim SentinelString() As Byte = {73, 116, 83, 116, 97, 114, 116, 115, 72, 101, 114, 101}
                If cmd = "Write" Then
                    Dim PlainText As String = frmInfo.txtImageInfo.Text
                    Dim PlainTextByteArray(PlainText.Length) As Byte
                    For i As Integer = 0 To (PlainText.Length - 1)
                        PlainTextByteArray(i) = CByte(AscW(PlainText.Chars(i)))
                        Application.DoEvents()
                    Next
                    Dim PicAndText(PicBytes + PlainText.Length + SentinelString.Length) As Byte
                    For t As Long = 0 To (PicBytes - 1)
                        PicAndText(t) = PicByteArray(t)
                    Next
                    Dim count As Integer = 0
                    For r As Long = PicBytes To (PicBytes + (SentinelString.Length) - 1)
                        PicAndText(r) = SentinelString(count)
                        count += 1
                    Next
                    count = 0
                    For q As Long = (PicBytes + SentinelString.Length) To (PicBytes + SentinelString.Length + PlainText.Length - 1)
                        PicAndText(q) = PlainTextByteArray(count)
                        count += 1
                    Next
    
    
                    My.Computer.FileSystem.WriteAllBytes(picpath, PicAndText, False)
                    PlainText = Nothing
                    PicAndText = Nothing
                    PlainTextByteArray = Nothing
                    PicByteArray = Nothing
    
    
                ElseIf cmd = "Read" Then
                    Dim OutterSearch, InnerSearch, StopSearch As Boolean
                    OutterSearch = True
                    InnerSearch = True
                    StopSearch = False
                    Dim count As Long = 0
                    Dim leftCounter As Long
                    Dim rightCounter As Integer
                    leftCounter = 0
                    rightCounter = 0
                    Do While (count < (PicBytes - SentinelString.Length) And StopSearch = False)
                        If (PicByteArray(count) = SentinelString(0)) Then
                            leftCounter = count + 1
                            rightCounter = 1
                            InnerSearch = True
                            Do While (InnerSearch = True) And (rightCounter < SentinelString.Length) _
                            And (leftCounter < PicByteArray.Length)
                                If (PicByteArray(leftCounter) = SentinelString(rightCounter)) Then
                                    rightCounter += 1
                                    leftCounter += 1
                                    If (rightCounter = (SentinelString.Length - 1)) Then
                                        StopSearch = True
                                    End If
                                Else
                                    InnerSearch = False
                                    count += 1
                                End If
                            Loop
                        Else
                            count += 1
                        End If
                    Loop
                    If StopSearch = True Then
                        'leftCounter contains the starting string that is being retrieved
                        Do While (leftCounter < PicBytes)
                            'Bytes need to be converted to an integer 
                            'then to an unicode character which will be the plaintext
                            frmInfo.txtImageInfo.AppendText(ChrW(CInt(PicByteArray(leftCounter))))
                            leftCounter += 1
                        Loop
                        PicByteArray = Nothing
                        PicBytes = Nothing
                    End If
    
    
                ElseIf cmd = "Delete" Then
                    Dim OutterSearch, InnerSearch, StopSearch As Boolean
                    OutterSearch = True
                    InnerSearch = True
                    StopSearch = False
                    Dim count As Long = 0
                    Dim leftCounter As Long
                    Dim rightCounter As Integer
                    leftCounter = 0
                    rightCounter = 0
                    Do While (count < (PicBytes - SentinelString.Length) And StopSearch = False)
                        If (PicByteArray(count) = SentinelString(0)) Then
                            leftCounter = count + 1
                            rightCounter = 1
                            InnerSearch = True
                            Do While (InnerSearch = True) And (rightCounter < SentinelString.Length) _
                            And (leftCounter < PicByteArray.Length)
                                If (PicByteArray(leftCounter) = SentinelString(rightCounter)) Then
                                    rightCounter += 1
                                    leftCounter += 1
                                    If (rightCounter = (SentinelString.Length - 1)) Then
                                        StopSearch = True
                                    End If
                                Else
                                    InnerSearch = False
                                    count += 1
                                End If
                            Loop
                        Else
                            count += 1
                        End If
                    Loop
    
                    If StopSearch = True Then
                        Dim newfile(count) As Byte
                        For i As Long = 0 To count - 1
                            newfile(i) = PicByteArray(i)
                        Next
                        My.Computer.FileSystem.WriteAllBytes(picpath, newfile, False)
                    End If
                End If
            End If
    
        End Sub
    I am trying to place text into an image, beable to delete/modify the text and then write it to THE SAME filename. I can get this to work to a different filename with no troubles but not the same. I get a "file being used by another process" error. I know my prog is the only thing accessing the file. Any suggestions???

  2. #2
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    I don't get the error. I made some insignificant changes to your code (select case, error handling, Longs to Integers, different form reference). I'm using VS2008

    Code:
    Public Sub PicText(ByVal cmd As String, ByVal picpath As String)
    
            Dim PicBuffer As New System.IO.FileInfo(picpath)
            Dim Ready As Boolean = True
            Dim PicFileStream As System.IO.FileStream = PicBuffer.OpenRead
    
            If Ready = True Then
                Dim PicBytes As Integer = CType(PicFileStream.Length, Integer)
                Dim PicExt As String = PicBuffer.Extension
                PicBuffer = Nothing
    
                Dim PicByteArray(PicBytes) As Byte
                PicFileStream.Read(PicByteArray, 0, PicBytes)
                PicFileStream.Close()
                PicFileStream.Dispose()
                Dim SentinelString() As Byte = {73, 116, 83, 116, 97, 114, 116, 115, 72, 101, 114, 101}
                Select Case cmd
                    Case "Write"
                        Try
                            Dim PlainText As String = Me.txtImageInfo.Text
                            Dim PlainTextByteArray(PlainText.Length) As Byte
                            For i As Integer = 0 To (PlainText.Length - 1)
                                PlainTextByteArray(i) = CByte(AscW(PlainText.Chars(i)))
                                Application.DoEvents()
                            Next
                            Dim PicAndText(PicBytes + PlainText.Length + SentinelString.Length) As Byte
                            For t As Integer = 0 To (PicBytes - 1)
                                PicAndText(t) = PicByteArray(t)
                            Next
                            Dim count As Integer = 0
                            For r As Integer = PicBytes To (PicBytes + (SentinelString.Length) - 1)
                                PicAndText(r) = SentinelString(count)
                                count += 1
                            Next
                            count = 0
                            For q As Integer = (PicBytes + SentinelString.Length) To (PicBytes + SentinelString.Length + PlainText.Length - 1)
                                PicAndText(q) = PlainTextByteArray(count)
                                count += 1
                            Next
                            My.Computer.FileSystem.WriteAllBytes(picpath, PicAndText, False)
                            PlainText = Nothing
                            PicAndText = Nothing
                            PlainTextByteArray = Nothing
    
                        Catch ex As Exception
                            MessageBox.Show(ex.ToString, "PicText " & cmd & " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End Try
                        PicByteArray = Nothing
    
                    Case "Read"
                        Try
                            Dim OutterSearch, InnerSearch, StopSearch As Boolean
                            OutterSearch = True
                            InnerSearch = True
                            StopSearch = False
                            Dim count As Integer = 0
                            Dim leftCounter As Integer
                            Dim rightCounter As Integer
                            leftCounter = 0
                            rightCounter = 0
                            Do While (count < (PicBytes - SentinelString.Length) And StopSearch = False)
                                If (PicByteArray(count) = SentinelString(0)) Then
                                    leftCounter = count + 1
                                    rightCounter = 1
                                    InnerSearch = True
                                    Do While (InnerSearch = True) And (rightCounter < SentinelString.Length) _
                                    And (leftCounter < PicByteArray.Length)
                                        If (PicByteArray(leftCounter) = SentinelString(rightCounter)) Then
                                            rightCounter += 1
                                            leftCounter += 1
                                            If (rightCounter = (SentinelString.Length - 1)) Then
                                                StopSearch = True
                                            End If
                                        Else
                                            InnerSearch = False
                                            count += 1
                                        End If
                                    Loop
                                Else
                                    count += 1
                                End If
                            Loop
                            If StopSearch = True Then
                                'leftCounter contains the starting string that is being retrieved
                                Do While (leftCounter < PicBytes)
                                    'Bytes need to be converted to an integer 
                                    'then to an unicode character which will be the plaintext
                                    Me.txtImageInfo.AppendText(ChrW(CInt(PicByteArray(leftCounter))))
                                    leftCounter += 1
                                Loop
                                PicByteArray = Nothing
                                PicBytes = Nothing
                            End If
                        Catch ex As Exception
                            MessageBox.Show(ex.ToString, "PicText " & cmd & " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End Try
    
                    Case "Delete"
                        Try
                            Dim OutterSearch, InnerSearch, StopSearch As Boolean
                            OutterSearch = True
                            InnerSearch = True
                            StopSearch = False
                            Dim count As Integer = 0
                            Dim leftCounter As Integer
                            Dim rightCounter As Integer
                            leftCounter = 0
                            rightCounter = 0
                            Do While (count < (PicBytes - SentinelString.Length) And StopSearch = False)
                                If (PicByteArray(count) = SentinelString(0)) Then
                                    leftCounter = count + 1
                                    rightCounter = 1
                                    InnerSearch = True
                                    Do While (InnerSearch = True) And (rightCounter < SentinelString.Length) _
                                    And (leftCounter < PicByteArray.Length)
                                        If (PicByteArray(leftCounter) = SentinelString(rightCounter)) Then
                                            rightCounter += 1
                                            leftCounter += 1
                                            If (rightCounter = (SentinelString.Length - 1)) Then
                                                StopSearch = True
                                            End If
                                        Else
                                            InnerSearch = False
                                            count += 1
                                        End If
                                    Loop
                                Else
                                    count += 1
                                End If
                            Loop
    
                            If StopSearch = True Then
                                Dim newfile(count) As Byte
                                For i As Integer = 0 To count - 1
                                    newfile(i) = PicByteArray(i)
                                Next
                                My.Computer.FileSystem.WriteAllBytes(picpath, newfile, False)
                            End If
    
                        Catch ex As Exception
                            MessageBox.Show(ex.ToString, "PicText " & cmd & " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End Try
                End Select
    
            End If
    
        End Sub
    How are you calling your Sub?

    ...joe

  3. #3
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,262
    This is only a guess, but because it looks like VB still has your file locked up, at least for write, that it may be your FileInfo object. You closed and disposed of the ReadOnly file stream, but not the FileInfo object.

    Dim PicBuffer As New System.IO.FileInfo(picpath) '<-- accessing your file

    Since WriteAllBytes want's to overwrite the file, it may not be able to until all objects referencing it are closed or at least no longer referencing that file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center