Results 1 to 24 of 24

Thread: *** Creating my own binary file thingo.... ***

  1. #1

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641

    Unhappy *** Creating my own binary file thingo.... ***

    Hi All!

    I am only really a newbie with file handling. I can open a file, and read text from it, but thats about it.

    What I'm looking to do is make my own file, with a header, and the data. The header will contain some sort of encrypted password string, and the data will contain a normal file (*.jpg, *.mpg).

    If anyone could give me some sort of an idea on how to do this, or a demonstration, please reply, as I really want to learn how to do this.

    Thanks in advance,
    Last edited by tim_l_012; Oct 7th, 2002 at 10:45 PM.
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  2. #2
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Take a look at User Define Types. Some examples of a similar sort are already available in earlier threads

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  3. #3

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    I think i might have found something, but could you guys post some links or some source for me to check out??

    Thanks in advance,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  4. #4
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Sure, 2 Secs...............

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  5. #5
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    VB Code:
    1. 'Needs one PictureBox, one Command Button
    2. Private Type myBITMAP
    3.     Data() As Byte
    4.     Category As String * 4
    5.     Comments As String * 50
    6. End Type
    7.  
    8. Private Sub Command1_Click()
    9. Dim newPic As myBITMAP
    10.     'Read in a standard Bitmap
    11.     Open App.Path & "\Clouds.bmp" For Binary As #1
    12.         ReDim newPic.Data(LOF(1))
    13.         Get #1, , newPic.Data
    14.     Close #1
    15.     newPic.Category = "WPPR"
    16.     newPic.Comments = "Source: WIN98"
    17.    
    18.     'Write our Bitmap
    19.     Open App.Path & "\NewPic.bmp" For Binary As #1
    20.         Put #1, , newPic
    21.     Close #1
    22.     MsgBox "done"
    23.    
    24.     'Read the file back
    25.  
    26.     Open App.Path & "\NewPic.bmp" For Binary As #1
    27.     'Leave the string values and take the rest
    28.         ReDim newPic.Data(LOF(1) - 108)
    29.         Get #1, , newPic
    30.     Close #1
    31.     'Write to the picture to temporary file for
    32.     'displaying in a picture box
    33.     Open App.Path & "\tmpFile.bmp" For Binary As #1
    34.         Put #1, , newPic.Data
    35.     Close
    36.     'Load into a PictureBox
    37.     Picture1.Picture = LoadPicture(App.Path & "\tmpFile.bmp")
    38.     Picture1.Refresh
    39.     'Delete the temporary file
    40.     Kill App.Path & "\tmpFile.bmp"
    41.  
    42.     MsgBox "The Picture in the PictureBox is of Category " _
    43.     & newPic.Category & vbCrLf & vbCrLf & newPic.Comments
    44. End Sub

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  6. #6

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    THANKS HEAPS!!

    That was exactly what I was looking for. Now I just need to set it up for handling MPEG and AVI Files... then it will be complete!

    Thanks for your help!

    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  7. #7
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Just be careful of the file sizes. As you will have to write to disk and read them back for proper rendering. 10 MB or more and there will be noticable disk activity

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  8. #8
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    UR most welcome

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  9. #9

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    How can I encrypt just the data??

    so that I will still be able to read the comments, but not the data

    Thanks in advance,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  10. #10

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    ok, i found this:

    Code:
    Sub EncryptFile(ByVal sName As String)
        Dim b() As Byte
        Dim nb() As Byte
        n = FileLen(sName)
        ReDim b(n - 1)
        ReDim nb(n - 1)
        
        Open sName For Binary Access Read As #1
        Get #1, , b()
        Close #1
        
        Kill sName
        
        For i = LBound(b) To UBound(b)
            nb(i) = b(i) Xor 5
        Next i
    
        Open sName For Binary Access Write As #1
        Put #1, , nb()
        Close #1
    End Sub
    how can I make it encrypt the binary data??

    Thanks in advance,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  11. #11
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Since the data is in a Byte Array, you can encrypt it as you please. For Example, reverse the bytes.
    VB Code:
    1. .........
    2. Open App.Path & "\Clouds.bmp" For Binary As #1
    3.         ReDim newPic.Data(LOF(1))
    4.         Get #1, , newPic.Data
    5.     Close #1
    6. 'Here we encrypt the Bitmap, by reversing the bytes
    7. Dim encData() As Byte, j As Long
    8. 'Create a new Byte Array to hold it
    9. ReDim encData(UBound(newPic.Data))
    10.     For i = UBound(newPic.Data) To LBound(newPic.Data) Step -1
    11.         encData(j) = newPic.Data(i)
    12.         j = j + 1
    13.     Next i
    14.  
    15. 'Over write the file with its reverse
    16.     For i = LBound(encData) to UBound(encData)
    17.         newPic.Data(i) = encData(i)
    18.     Next i

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  12. #12

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    I would like to use a password, not just reversing it..

    I changed the 'comments' string to a 'strPassword' string. is there any way I could use that in there somewhere???

    Thanks in advance,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  13. #13
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Originally posted by tim_l_012

    how can I make it encrypt the binary data??

    Thanks in advance, [/B]
    The above code is for Binary data. b and nb are Byte Arrays

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  14. #14
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Am I right in understanding that you want to encrypt the data using whatever password that is stored for that file.

    But in that case the password itself would be exposed, would it not?

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  15. #15
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    A good way to do encryption is random seeds using xor, a look-up-table, and interpolating characters.

    That way, if the smallest change occurs, the entire file is corrupt.

  16. #16

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    ok I'll go through this properly

    In my program, I want to be able to add passwords to certain files. Then, I give them my own file type, so they open with my program.

    The password is passed by from the user, then encrypted using my encryption routine, and then placed in the file, along with the data.

    the password is only encrypted using XOR, and I didn't know how to encrypt the binary stuff, so..

    here you guys are to help me!

    hope that helped, reply ASAP

    Thanks in advance,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  17. #17

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    hey digital,

    thats a sweet idea!

    how would I incorporate that?

    Thanks in advance,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  18. #18
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    get it.......some time please..........

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  19. #19
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    If you're not going to encrypt the data you should encrypt the password heavily, then distribute the bytes of it throughout the file. rendering the file corrupt, without password.

  20. #20

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    ok, thanks for all your ideas, I have devised my solution, and have started on the new routines!

    Thanks all who participated!
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  21. #21
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    UR Welcome

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  22. #22
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Well.

    You define the seed to create psudeo random numbers using randomize -1
    randomize seed

    then rnd will generate the same seed of random numbers

    then you do

    chr$(rnd*256 xor asc(chr))


    you have to store the seed as well, there are 64k seeds. I would suggest using base 256, and storing seeds in a 2 byte format.

    -32k to 32k

    where asc(chr1)*256+asc(chr2) = seed - 32k

  23. #23

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    ok, for all who dont understand, I want to store an encrypted password in the header. When WMP or similar tries to open it, they wont understand it because the bytes will be reversed.

    to un-reverse them, my program opens the file, encrypts the users password, and checks it against the encrypted password stored in the file. If it is the same, the bytes are un-reversed..

    can anybody tell me why this wont work??
    (btw, you will need a form with 5 command buttons, and a Windows Media Player control, called 'MP'

    Thanks in advance,

    Code:
    Option Explicit
    
    Private Type myFILE
        
        Data() As Byte
        Category As String * 4
        Word As String * 50
        
    End Type
    
    Dim strExtension As String
    Dim strNewFilename As String
    
    Private Function OnlyExtension(FileName As String) As String
        Dim lngPos As Long
        
        FileName = StrReverse(FileName)
        
        lngPos = InStr(FileName, ".")
        FileName = StrReverse(FileName)
        
        OnlyExtension = Right(FileName, lngPos - 1)
        
        
    End Function
    
    
    Private Function NoExtension(FileName As String) As String
        Dim lngPos As Long
        
        lngPos = InStrRev(FileName, ".")
        NoExtension = Left(FileName, lngPos - 1)
    
    End Function
    
    Public Sub PutPassWord(ByVal strFileName As String, ByVal strCategory As String, ByVal strPassword As String)
    
    Dim newpic As myFILE
    Dim I As Long
    Dim encData() As Byte, j As Long
    
        Debug.Print strFileName
        
        Open strFileName For Binary As #1
            ReDim newpic.Data(LOF(1))
            Get #1, , newpic.Data
        Close #1
        
        newpic.Category = strCategory
        newpic.Word = strPassword
        
        '====================
        'ENCRYPT THA DATA!!
    
    'Create a new Byte Array to hold it
    ReDim encData(UBound(newpic.Data))
        For I = UBound(newpic.Data) To LBound(newpic.Data) Step -1
            encData(j) = newpic.Data(I)
            j = j + 1
        Next I
    
    'Over write the file with its reverse
        For I = LBound(encData) To UBound(encData)
            newpic.Data(I) = encData(I)
        Next I
    
            
        
        '====================
            
        strExtension = OnlyExtension(strFileName)
        strNewFilename = NoExtension(strFileName)
            
        strNewFilename = strNewFilename & ".p" & strExtension
        
        If Dir(strNewFilename, vbNormal) <> "" Then Kill strNewFilename
        
        Debug.Print "Final: " & strNewFilename
        
        Open strNewFilename For Binary As #1
            Put #1, , newpic
        Close #1
    
    End Sub
    Public Sub GetMovie(ByVal strFileName As String, ByVal strPassword As String)
    
    Dim I As Long
    Dim encData() As Byte, j As Long
    Dim newpic As myFILE
    
        Open strFileName For Binary As #1
            ReDim newpic.Data(LOF(1) - 108)
            Get #1, , newpic
        Close #1
    
        If RTrim(newpic.Word) = strPassword Then
    
        '====== DECRYPT IT
        
        'Create a new Byte Array to hold it
        ReDim encData(UBound(newpic.Data))
        For I = UBound(newpic.Data) To LBound(newpic.Data) Step -1
            encData(j) = newpic.Data(I)
            j = j + 1
        Next I
    
        'Over write the file with its reverse
        For I = LBound(encData) To UBound(encData)
            newpic.Data(I) = encData(I)
        Next I
        
        '==================
    
        Open strFileName For Binary As #1
            Put #1, , newpic.Data
        Close #1
        
        MP.FileName = strFileName
        
        Me.Visible = True
        MP.Play
        
        End If
        
    End Sub
    Public Sub RemovePass(ByVal strFileName As String, ByVal strPassword As String)
    Dim tmpString As String
    Dim newpic As myFILE
    
        Open strFileName For Binary As #1
            ReDim newpic.Data(LOF(1) - 108)
            Get #1, , newpic
        Close #1
    
    tmpString = NoExtension(strFileName) & "." & Right(OnlyExtension(strFileName), Len(OnlyExtension(strFileName)) - 1)
    
        If newpic.Word = strPassword Then
            Open tmpString For Binary As #1
                Put #1, , newpic.Data
            Close
        Else
            MsgBox "Password Incorrect. Password cannot be removed without Authorization.", vbOKOnly + vbCritical, "Remove Failed:"
        End If
        
    End Sub
    
    Private Sub cmdtest_Click()
    
    'Me.Show
    
    If MP.FileName = "" Then
        MP.FileName = App.Path & "\clock.avi"
    Else
        MP.FileName = ""
    End If
    
    End Sub
    
    Private Sub Command1_Click()
    
    PutPassWord App.Path & "\clock.avi", "PAVI", "testing"
    
    End Sub
    
    Private Sub Command2_Click()
    
    RemovePass App.Path & "\clock.avi", "testing"
    
    
    End Sub
    
    Private Sub Command3_Click()
    
    GetMovie App.Path & "\clock.avi", "testing"
    
    End Sub
    
    Private Sub Command4_Click()
    
    If MP.FileName = "" Then
        MP.FileName = App.Path & "\clock.avi"
    Else
        MP.FileName = ""
    End If
    
    End Sub
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  24. #24

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    *BUMP*

    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width