PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] What is the fastest and simplest file encryption?-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: [RESOLVED] What is the fastest and simplest file encryption?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Resolved [RESOLVED] What is the fastest and simplest file encryption?

    I am looking for the fastest file encryption? Does anyone know how to take the content of a binary file - reverse and save it?
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  2. #2
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: What is the fastest and simplest file encryption?

    vb Code:
    1. Dim f As long
    2. Dim d() As Byte
    3. Dim i As Long
    4. Dim filein As String
    5. Dim fileout As String
    6.  
    7. f = FreeFile
    8. Open filein For Binary As #f
    9.     ReDim d(LOF(f) - 1)
    10.     Get #f, , d
    11. Close #f
    12. f = FreeFile
    13. Open fileout For Binary As #f
    14.     For i = UBound(d) To 0 Step -1
    15.         Put #f, , d(i)
    16.     Next i
    17. Close #f
    18. Erase d

  3. #3
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    You would do better reading 256kb (or another number) blocks of the file from the end backwards and writing a new file. It would be faster and of course you would do the same to revert it to normal. I would recommend that you put a header on the file so that you know something about what you did to the file. In that way you could tell if it was already reversed...

  4. #4
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,585

    Re: What is the fastest and simplest file encryption?

    as long as the file is not TOO large, you could load the entire thing into a byte array and XOR every byte by a certain value. Xoring it by the same value later would decrypt it. However setting file encryption to true would prevent anyone except an administrator on the computer you created it on from reading the file.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Thanks jeroen79. Can that handle any file size?

    Quote Originally Posted by randem
    You would do better reading 256kb (or another number) blocks of the file from the end backwards and writing a new file. It would be faster and of course you would do the same to revert it to normal.
    Can you provide a sample like jeroen79 or provide a link that shows a sample?

    Quote Originally Posted by randem
    I would recommend that you put a header on the file so that you know something about what you did to the file. In that way you could tell if it was already reversed...
    I plan to do this because otherwise, I might end up reversing twice.

    Quote Originally Posted by Lord Orwell
    as long as the file is not TOO large, you could load the entire thing into a byte array and XOR every byte by a certain value. Xoring it by the same value later would decrypt it.
    Can you provide a sample like jeroen79 or provide a link that shows a sample?

    Quote Originally Posted by Lord Orwell
    However setting file encryption to true would prevent anyone except an administrator on the computer you created it on from reading the file.
    I don't understand? Please elaborate.

    Thank You so far.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    Here ya go
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim FNumIn As Integer
    Dim FNumOut As Integer
    Dim i As Long
    Dim sFilenameIn As String
    Dim sFilenameOut As String
    Dim FileBlocks As Long
    Dim bBuf() As Byte
    Dim lOffsetIn As Long
    Const bufsze = 256
    Dim Quit As Boolean
    Dim Remainder As Long
    
    
        sFilenameIn = App.Path & "\DataIn.dat"
        sFilenameOut = App.Path & "\DataOut.dat"
        
        FNumIn = FreeFile
        Open sFilenameIn For Binary Access Read Shared As #FNumIn
        lOffsetIn = LOF(FNumIn) + 1
        
        FNumOut = FreeFile
        Open sFilenameOut For Binary Access Write As #FNumOut
            
        ReDim bBuf(bufsze)
        
        Do While Not Quit
        
            lOffsetIn = lOffsetIn - bufsze
            
            If lOffsetIn < 0 Then
                Remainder = bufsze - lOffsetIn
                lOffsetIn = 1
                Quit = True
                ReDim bBuf(Remainder)
            End If
            
    '        Seek FNumIn, lOffsetIn
            
            Get FNumIn, lOffsetIn, bBuf
            Put FNumOut, , bBuf
            
        Loop
        
        Close #FNumIn
        Close #FNumOut
    End Sub

  7. #7
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: What is the fastest and simplest file encryption?

    Here you can use this, I made this awhile back for my LAN Messenger I made, to encrypt and decrypt the packets sent to and from the client and server. I have used it for serveral other things as well, such as file encryption.

    vb Code:
    1. Public Function Encrypt(ByVal Plain As String, _
    2.                         sEncKey As String) As String
    3.  
    4.     Dim LenLetter As Integer, itempnum As Integer, i As Integer
    5.     Dim Letter As String, KeyNum As String, encstr As String, temp As String, _
    6.         temp2 As String, itempstr As String, encrypted2 As String
    7.     Dim Math As Long
    8.    
    9.     On Error GoTo oops
    10.    
    11.     If sEncKey = "" Then sEncKey = "Jaquio"
    12.     'Sets the Encryption Key if one is not set
    13.     ReDim encKEY(1 To Len(sEncKey))
    14.    
    15.     'starts the values for the Encryption Key
    16.        
    17.     For i = 1 To Len(sEncKey$)
    18.      KeyNum = Mid$(sEncKey$, i, 1) 'gets the letter at index i
    19.      encKEY(i) = Asc(KeyNum) 'sets the the Array value
    20.                              'to ASC number for the letter
    21.  
    22.            'This is the first letter so just hold the value
    23.         If i = 1 Then Math = encKEY(i): GoTo nextone
    24.  
    25.         'compares the value to the previous value and then
    26.         'either adds/subtracts the value to the Math total
    27.        If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) <= _
    28.            encKEY(i - 1) Then Math = Math - encKEY(i)
    29.  
    30.         If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) <= _
    31.            encKEY(i - 1) Then Math = Math - encKEY(i)
    32.         If i >= 2 And encKEY(i) >= Math And encKEY(i) >= _
    33.            encKEY(i - 1) Then Math = Math + encKEY(i)
    34.         If i >= 2 And encKEY(i) < Math And encKEY(i) _
    35.           >= encKEY(i - 1) Then Math = Math + encKEY(i)
    36. nextone:
    37.     Next i
    38.    
    39.    
    40.     For i = 1 To Len(Plain) 'Now for the String to be encrypted
    41.         Letter = Mid$(Plain, i, 1) 'sets Letter to
    42.                                    'the letter at index i
    43.         LenLetter = Asc(Letter) + Math 'Now it adds the Asc
    44.                                        'value of Letter to Math
    45.  
    46. 'checks and corrects the format then adds a space to separate them frm each other
    47.         If LenLetter >= 100 Then encstr = _
    48.              encstr & Asc(Letter) + Math & " "
    49.  
    50.          'checks and corrects the format then adds a space
    51.         'to separate them frm each other
    52.         If LenLetter <= 99 Then encstr$ = encstr & "0" & _
    53.           Asc(Letter) + Math & " "
    54.     Next i
    55.  
    56.  
    57.     'This is part of what i'm doing to convert the encrypted
    58.     'numbers to Letters so it sort of encrypts the
    59.     'encrypted message.
    60.     temp$ = encstr 'hold the encrypted data
    61.     temp$ = TrimSpaces(temp) 'get rid of the spaces
    62.     itempnum% = Mid(temp, 1, 2) 'grab the first 2 numbers
    63.     temp2$ = Chr(itempnum% + 100) 'Now add 100 so it
    64.                                    'will be a valid char
    65.  
    66.     'If its a 2 digit number hold it and continue
    67.     If Len(itempnum%) >= 2 Then itempstr$ = Str(itempnum%)
    68.  
    69.    'If the number is a single digit then add a '0' to the front
    70.    'then hold it
    71.     If Len(itempnum%) = 1 Then itempstr$ = "0" & _
    72.         TrimSpaces(Str(itempnum%))
    73.    
    74.     encrypted2$ = temp2 'set the encrypted message
    75.    
    76.     For i = 3 To Len(temp) Step 2
    77.         itempnum% = Mid(temp, i, 2) 'grab the next 2 numbers
    78.  
    79.       ' add 100 so it will be a valid char
    80.         temp2$ = Chr(itempnum% + 100)
    81.  
    82.       'if its the last number we only want to hold it we
    83.        'don't want to add a '0' even if its a single digit
    84.         If i = Len(temp) Then itempstr$ = _
    85.          Str(itempnum%): GoTo itsdone
    86.  
    87. 'If its a 2 digit number hold it and continue
    88.         If Len(itempnum%) = 2 Then itempstr$ = _
    89.             Str(itempnum%)
    90.  
    91.         'If the number is a single digit then add a '0'
    92.         'to the front then hold it
    93.         If Len(TrimSpaces(Str(itempnum))) = 1 Then _
    94.       itempstr$ = "0" & TrimSpaces(Str(itempnum%))
    95.  
    96.         'Now check to see if a - number was created
    97.         'if so cause an error message
    98.         If Left(TrimSpaces(Str(itempnum)), 1) = "-" Then _
    99.           Err.Raise 20000, , "Unexpected Error"
    100.            
    101.  
    102. itsdone:
    103.            'Set The Encrypted message
    104.         encrypted2$ = encrypted2 & temp2$
    105.     Next i
    106.  
    107.  
    108.     'Encrypt = encstr 'Returns the First Encrypted String
    109.     Encrypt = encrypted2 'Returns the Second Encrypted String
    110.     Exit Function 'We are outta Here
    111. oops:
    112.     Debug.Print "Error description", Err.Description
    113.     Debug.Print "Error source:", Err.Source
    114.     Debug.Print "Error Number:", Err.Number
    115. End Function
    116.  
    117. Public Function Decrypt(ByVal Encrypted As String, _
    118.                         sEncKey As String) As String
    119.  
    120.     Dim NewEncrypted As String, Letter As String, KeyNum As String, EncNum As String
    121.     Dim encbuffer As Long, Math As Long
    122.     Dim strDecrypted As String, Kdecrypt As String, lastTemp As String
    123.     Dim LenTemp As Integer, itempnum As Integer, i As Integer
    124.     Dim temp As String, temp2 As String, itempstr As String
    125.    
    126.     On Error GoTo oops
    127.  
    128.     If sEncKey = "" Then sEncKey = "Jaquio"
    129.  
    130.     ReDim encKEY(1 To Len(sEncKey))
    131.    
    132.     'Convert The Key For Decryption
    133.     For i = 1 To Len(sEncKey$)
    134.         KeyNum = Mid$(sEncKey$, i, 1) 'Get Letter i% in the Key
    135.         encKEY(i) = Asc(KeyNum) 'Convert Letter i to Asc value
    136.  
    137. 'if it the first letter just hold it
    138.        If i = 1 Then Math = encKEY(i): GoTo nextone
    139.        If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) _
    140.                <= encKEY(i - 1) Then Math = Math - encKEY(i)
    141.                'compares the value to the previous value and
    142.                'then either adds/subtracts the value to the
    143.                'Math total
    144.         If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) _
    145.               <= encKEY(i - 1) Then Math = Math - encKEY(i)
    146.         If i >= 2 And encKEY(i) >= Math And encKEY(i) _
    147.               >= encKEY(i - 1) Then Math = Math + encKEY(i)
    148.         If i >= 2 And encKEY(i) < Math And encKEY(i) _
    149.               >= encKEY(i - 1) Then Math = Math + encKEY(i)
    150. nextone:
    151.     Next i
    152.    
    153.    
    154.     'This is part of what i'm doing to convert the encrypted
    155.     'numbers to  Letters so it sort of encrypts the encrypted
    156.     'message.
    157.     temp$ = Encrypted 'hold the encrypted data
    158.  
    159.     For i = 1 To Len(temp)
    160.         itempstr = TrimSpaces(Str(Asc(Mid(temp, i, 1)) - _
    161.            100)) 'grab the next 2 numbers
    162.            'If its a 2 digit number hold it and continue
    163.         If Len(itempstr$) = 2 Then itempstr$ = itempstr$
    164.           If i = Len(temp) - 2 Then LenTemp% = _
    165.                Len(Mid(temp2, Len(temp2) - 3))
    166.           If i = Len(temp) Then itempstr$ = _
    167.               TrimSpaces(itempstr$): GoTo itsdone
    168.           'If the number is a single digit then add a '0' to the
    169.           'front then hold it
    170.         If Len(TrimSpaces(itempstr$)) = 1 Then _
    171.              itempstr$ = "0" & TrimSpaces(itempstr$)
    172.         'Now check to see if a - number was created if so
    173.         'cause an error message
    174.         If Left(TrimSpaces(itempstr$), 1) = "-" Then _
    175.              Err.Raise 20000, , "Unexpected Error"
    176.            
    177.  
    178. itsdone:
    179.         temp2$ = temp2$ & itempstr 'hold the first decryption
    180.     Next i
    181.    
    182.    
    183.     Encrypted = TrimSpaces(temp2$) 'set the encrypted data
    184.  
    185.  
    186.     For i = 1 To Len(Encrypted) Step 3
    187.         'Format the encrypted string for the second decryption
    188.         NewEncrypted = NewEncrypted & _
    189.             Mid(Encrypted, CLng(i), 3) & " "
    190.     Next i
    191.  
    192. ' Hold the last set of numbers to check it its the correct format
    193.     lastTemp$ = TrimSpaces(Mid(NewEncrypted, _
    194.          Len(NewEncrypted$) - 3))
    195.          
    196.          If Len(lastTemp$) = 2 Then
    197. ' If it = 2 then its not the Correct format and we need to fix it
    198.         lastTemp$ = Mid(NewEncrypted, _
    199.            Len(NewEncrypted$) - 1) 'Holds Last Number so a '0'
    200.                                     'Can be added between them
    201. 'set it to the new format
    202.         Encrypted = Mid(NewEncrypted, 1, _
    203.            Len(NewEncrypted) - 2) & "0" & lastTemp$
    204. Else
    205.         Encrypted$ = NewEncrypted$ 'set the new format
    206.  
    207.     End If
    208.     'The Actual Decryption
    209.     For i = 1 To Len(Encrypted)
    210.         Letter = Mid$(Encrypted, i, 1) 'Hold Letter at index i
    211.         EncNum = EncNum & Letter 'Hold the letters
    212.         If Letter = " " Then 'we have a letter to decrypt
    213.             encbuffer = CLng(Mid(EncNum, 1, _
    214.               Len(EncNum) - 1)) 'Convert it to long and
    215.                                  'get the number minus the " "
    216.             strDecrypted$ = strDecrypted & Chr(encbuffer - _
    217.                Math) 'Store the decrypted string
    218.             EncNum = "" 'clear if it is a space so we can get
    219.                         'the next set of numbers
    220.         End If
    221.     Next i
    222.  
    223.     Decrypt = strDecrypted
    224.  
    225.     Exit Function
    226. oops:
    227.     Debug.Print "Error description", Err.Description
    228.     Debug.Print "Error source:", Err.Source
    229.     Debug.Print "Error Number:", Err.Number
    230.     Err.Raise 20001, , "You have entered the wrong encryption string"
    231.  
    232. End Function
    233.  
    234. Private Function TrimSpaces(strString As String) As String
    235.     Dim lngpos As Long
    236.     Do While InStr(1&, strString$, " ")
    237.         DoEvents
    238.          lngpos& = InStr(1&, strString$, " ")
    239.          strString$ = Left$(strString$, (lngpos& - 1&)) & _
    240.             Right$(strString$, Len(strString$) - _
    241.                (lngpos& + Len(" ") - 1&))
    242.     Loop
    243.      TrimSpaces$ = strString$
    244. End Function

    It is a fairly weak encryption method but it works for me and that is all I really needed.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Thanks for doing the follow-up randem.

    Thanks Jaquio, I will check it out.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Jaquio,

    I tried to copy you code over to a vb module but it was all jumbled around. Not sure what happen but it was not like copying randem's code. Can you see if you can change the tag to [code]? Also, if you can explain the functions and sub procedures, that would be great.

    Thank You
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    BTW... randem, can your code handle any file size and what is the best way to put a header into the file? I am thinking of using a string. Thanks
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  11. #11
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    Yes, it can handle any size file and you would write out the header immediately before writing out the first block of the output file.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Thanks for your reply.

    So, I would write the header as a string write? Also, somehow, the code that you provided is not producing an output file.

    BTW... I would write out the header like this right?

    Code:
        Open sFilenameOut For Binary Access Write As #FNumOut
        Print #FNumOut, "Header"
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  13. #13
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    No, You would set up a predefined type definition (fixed size) that consisted of the data you want to have in the header.

    The code I gave is tested and does output a file... You do realize that if you don't supply an input file there can be no output????

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Quote Originally Posted by randem
    No, You would set up a predefined type definition (fixed size) that consisted of the data you want to have in the header.
    Hate to ask you to do this, but would you be kind enough to supply the example to the above?

    Quote Originally Posted by randem
    The code I gave is tested and does output a file... You do realize that if you don't supply an input file there can be no output????
    This is strange. I did supply an input file. I will test it again.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    Post what you have...

  16. #16
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    BTW: You do have to do some of the work...

  17. #17
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: What is the fastest and simplest file encryption?

    Here it is in Code tags

    Code:
    Public Function Encrypt(ByVal Plain As String, _
                            sEncKey As String) As String
      
        Dim LenLetter As Integer, itempnum As Integer, i As Integer
        Dim Letter As String, KeyNum As String, encstr As String, temp As String, _
            temp2 As String, itempstr As String, encrypted2 As String
        Dim Math As Long
        
        On Error GoTo oops
        
        If sEncKey = "" Then sEncKey = "Jaquio"
        'Sets the Encryption Key if one is not set
        ReDim encKEY(1 To Len(sEncKey))
        
        'starts the values for the Encryption Key
            
        For i = 1 To Len(sEncKey$)
         KeyNum = Mid$(sEncKey$, i, 1) 'gets the letter at index i
         encKEY(i) = Asc(KeyNum) 'sets the the Array value
                                 'to ASC number for the letter
    
               'This is the first letter so just hold the value
            If i = 1 Then Math = encKEY(i): GoTo nextone
    
            'compares the value to the previous value and then
            'either adds/subtracts the value to the Math total
           If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) <= _
               encKEY(i - 1) Then Math = Math - encKEY(i)
    
            If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) <= _
               encKEY(i - 1) Then Math = Math - encKEY(i)
            If i >= 2 And encKEY(i) >= Math And encKEY(i) >= _
               encKEY(i - 1) Then Math = Math + encKEY(i)
            If i >= 2 And encKEY(i) < Math And encKEY(i) _
              >= encKEY(i - 1) Then Math = Math + encKEY(i)
    nextone:
        Next i
        
        
        For i = 1 To Len(Plain) 'Now for the String to be encrypted
            Letter = Mid$(Plain, i, 1) 'sets Letter to
                                       'the letter at index i
            LenLetter = Asc(Letter) + Math 'Now it adds the Asc
                                           'value of Letter to Math
    
    'checks and corrects the format then adds a space to separate them frm each other
            If LenLetter >= 100 Then encstr = _
                 encstr & Asc(Letter) + Math & " "
    
             'checks and corrects the format then adds a space
            'to separate them frm each other
            If LenLetter <= 99 Then encstr$ = encstr & "0" & _
              Asc(Letter) + Math & " "
        Next i
    
    
        'This is part of what i'm doing to convert the encrypted
        'numbers to Letters so it sort of encrypts the
        'encrypted message.
        temp$ = encstr 'hold the encrypted data
        temp$ = TrimSpaces(temp) 'get rid of the spaces
        itempnum% = Mid(temp, 1, 2) 'grab the first 2 numbers
        temp2$ = Chr(itempnum% + 100) 'Now add 100 so it
                                       'will be a valid char
    
        'If its a 2 digit number hold it and continue
        If Len(itempnum%) >= 2 Then itempstr$ = Str(itempnum%)
     
       'If the number is a single digit then add a '0' to the front
       'then hold it
        If Len(itempnum%) = 1 Then itempstr$ = "0" & _
            TrimSpaces(Str(itempnum%))
        
        encrypted2$ = temp2 'set the encrypted message
        
        For i = 3 To Len(temp) Step 2
            itempnum% = Mid(temp, i, 2) 'grab the next 2 numbers
      
          ' add 100 so it will be a valid char
            temp2$ = Chr(itempnum% + 100)
    
          'if its the last number we only want to hold it we
           'don't want to add a '0' even if its a single digit
            If i = Len(temp) Then itempstr$ = _
             Str(itempnum%): GoTo itsdone
    
    'If its a 2 digit number hold it and continue
            If Len(itempnum%) = 2 Then itempstr$ = _
                Str(itempnum%)
    
            'If the number is a single digit then add a '0'
            'to the front then hold it
            If Len(TrimSpaces(Str(itempnum))) = 1 Then _
          itempstr$ = "0" & TrimSpaces(Str(itempnum%))
    
            'Now check to see if a - number was created
            'if so cause an error message
            If Left(TrimSpaces(Str(itempnum)), 1) = "-" Then _
              Err.Raise 20000, , "Unexpected Error"
               
    
    itsdone:
               'Set The Encrypted message
            encrypted2$ = encrypted2 & temp2$
        Next i
    
    
        'Encrypt = encstr 'Returns the First Encrypted String
        Encrypt = encrypted2 'Returns the Second Encrypted String
        Exit Function 'We are outta Here
    oops:
        Debug.Print "Error description", Err.Description
        Debug.Print "Error source:", Err.Source
        Debug.Print "Error Number:", Err.Number
    End Function
    
    Public Function Decrypt(ByVal Encrypted As String, _
                            sEncKey As String) As String
    
        Dim NewEncrypted As String, Letter As String, KeyNum As String, EncNum As String
        Dim encbuffer As Long, Math As Long
        Dim strDecrypted As String, Kdecrypt As String, lastTemp As String
        Dim LenTemp As Integer, itempnum As Integer, i As Integer
        Dim temp As String, temp2 As String, itempstr As String
        
        On Error GoTo oops
    
        If sEncKey = "" Then sEncKey = "Jaquio"
    
        ReDim encKEY(1 To Len(sEncKey))
        
        'Convert The Key For Decryption
        For i = 1 To Len(sEncKey$)
            KeyNum = Mid$(sEncKey$, i, 1) 'Get Letter i% in the Key
            encKEY(i) = Asc(KeyNum) 'Convert Letter i to Asc value
     
    'if it the first letter just hold it
           If i = 1 Then Math = encKEY(i): GoTo nextone
           If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) _
                   <= encKEY(i - 1) Then Math = Math - encKEY(i)
                   'compares the value to the previous value and
                   'then either adds/subtracts the value to the
                   'Math total
            If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) _
                  <= encKEY(i - 1) Then Math = Math - encKEY(i)
            If i >= 2 And encKEY(i) >= Math And encKEY(i) _
                  >= encKEY(i - 1) Then Math = Math + encKEY(i)
            If i >= 2 And encKEY(i) < Math And encKEY(i) _
                  >= encKEY(i - 1) Then Math = Math + encKEY(i)
    nextone:
        Next i
        
        
        'This is part of what i'm doing to convert the encrypted
        'numbers to  Letters so it sort of encrypts the encrypted
        'message.
        temp$ = Encrypted 'hold the encrypted data
    
        For i = 1 To Len(temp)
            itempstr = TrimSpaces(Str(Asc(Mid(temp, i, 1)) - _
               100)) 'grab the next 2 numbers
               'If its a 2 digit number hold it and continue
            If Len(itempstr$) = 2 Then itempstr$ = itempstr$
              If i = Len(temp) - 2 Then LenTemp% = _
                   Len(Mid(temp2, Len(temp2) - 3))
              If i = Len(temp) Then itempstr$ = _
                  TrimSpaces(itempstr$): GoTo itsdone
              'If the number is a single digit then add a '0' to the
              'front then hold it
            If Len(TrimSpaces(itempstr$)) = 1 Then _
                 itempstr$ = "0" & TrimSpaces(itempstr$)
            'Now check to see if a - number was created if so
            'cause an error message
            If Left(TrimSpaces(itempstr$), 1) = "-" Then _
                 Err.Raise 20000, , "Unexpected Error"
               
    
    itsdone:
            temp2$ = temp2$ & itempstr 'hold the first decryption
        Next i
        
        
        Encrypted = TrimSpaces(temp2$) 'set the encrypted data
    
    
        For i = 1 To Len(Encrypted) Step 3
            'Format the encrypted string for the second decryption
            NewEncrypted = NewEncrypted & _
                Mid(Encrypted, CLng(i), 3) & " "
        Next i
    
    ' Hold the last set of numbers to check it its the correct format
        lastTemp$ = TrimSpaces(Mid(NewEncrypted, _
             Len(NewEncrypted$) - 3))
             
             If Len(lastTemp$) = 2 Then
    ' If it = 2 then its not the Correct format and we need to fix it
            lastTemp$ = Mid(NewEncrypted, _
               Len(NewEncrypted$) - 1) 'Holds Last Number so a '0'
                                        'Can be added between them
    'set it to the new format
            Encrypted = Mid(NewEncrypted, 1, _
               Len(NewEncrypted) - 2) & "0" & lastTemp$
    Else
            Encrypted$ = NewEncrypted$ 'set the new format
    
        End If
        'The Actual Decryption
        For i = 1 To Len(Encrypted)
            Letter = Mid$(Encrypted, i, 1) 'Hold Letter at index i
            EncNum = EncNum & Letter 'Hold the letters
            If Letter = " " Then 'we have a letter to decrypt
                encbuffer = CLng(Mid(EncNum, 1, _
                  Len(EncNum) - 1)) 'Convert it to long and
                                     'get the number minus the " "
                strDecrypted$ = strDecrypted & Chr(encbuffer - _
                   Math) 'Store the decrypted string
                EncNum = "" 'clear if it is a space so we can get
                            'the next set of numbers
            End If
        Next i
    
        Decrypt = strDecrypted
    
        Exit Function
    oops:
        Debug.Print "Error description", Err.Description
        Debug.Print "Error source:", Err.Source
        Debug.Print "Error Number:", Err.Number
        Err.Raise 20001, , "You have entered the wrong encryption string"
    
    End Function
    
    Private Function TrimSpaces(strString As String) As String
        Dim lngpos As Long
        Do While InStr(1&, strString$, " ")
            DoEvents
             lngpos& = InStr(1&, strString$, " ")
             strString$ = Left$(strString$, (lngpos& - 1&)) & _
                Right$(strString$, Len(strString$) - _
                   (lngpos& + Len(" ") - 1&))
        Loop
         TrimSpaces$ = strString$
    End Function
    Hopefully it copys okay, there isn't much to explain that isn't already it is commented so hopefully it does the job if you don't understand anything about it just ask.

    To Encrypt:

    Encrypt("Data to be encrypted","An encryption key of your choice(can be anything)")

    To Decrypt:

    Decrypt("Data to be decrypted","Your encryption key you chose.")

    Hopefully it works okay for you. If you need it used in an example just let me know.

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Quote Originally Posted by randem
    BTW: You do have to do some of the work...
    Not that I don't want to do the work, just don't understand how to get to the destination.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    OK, this is what I have so far.

    Code:
    Option Explicit
    
    Private Type FileHeader
        intBufferSize As Integer
        lngRemainder As Long
    End Type
    
    Private Sub Main()
        Call ReverseIt
        Call UnReverseIt
    End Sub
    
    Private Sub ReverseIt()
    Dim FNumIn As Integer
    Dim FNumOut As Integer
    Dim sFilenameIn As String
    Dim sFilenameOut As String
    Dim bBuf() As Byte
    Dim lOffsetIn As Long
    Const bufsze As Integer = 256
    Dim Quit As Boolean
    Dim Remainder As Long
    
    Dim HDR As FileHeader
    HDR.intBufferSize = bufsze
    'HDR.lngRemainder = Remainder
    
        sFilenameIn = App.Path & "\Test.vbs"
        sFilenameOut = Left(sFilenameIn, InStrRev(sFilenameIn, ".")) & "txt"
        If Dir(sFilenameOut) <> "" Then Kill sFilenameOut
        
        'Open the input file as binary
        FNumIn = FreeFile
        Open sFilenameIn For Binary Access Read Shared As #FNumIn
        lOffsetIn = LOF(FNumIn) + 1 'Get the total length of the file
        
        'Open the output file as binary
        FNumOut = FreeFile
        Open sFilenameOut For Binary Access Write As #FNumOut
    '    Put FNumOut, , HDR
            
        ReDim bBuf(bufsze)
        Do While Not Quit
            'Subtract the remaining total length from the buffer size to get the last positioning
            lOffsetIn = lOffsetIn - bufsze
            
            'Get the remaining
            If lOffsetIn < 0 Then
                Remainder = bufsze - lOffsetIn
                lOffsetIn = 1
                Quit = True
                ReDim bBuf(Remainder)
            End If
            
    '        Seek FNumIn, lOffsetIn
            
            'Get the binary from the position
            Get FNumIn, lOffsetIn, bBuf
            
            'Output it backout with the end of the input file
            Put FNumOut, , bBuf
        Loop
        
        Close #FNumIn
        Close #FNumOut
    End Sub
    
    Private Sub UnReverseIt()
    Dim FNumIn As Integer
    Dim FNumOut As Integer
    Dim sFilenameIn As String
    Dim sFilenameOut As String
    Dim bBuf() As Byte
    Dim lOffsetIn As Long
    Const bufsze As Integer = 256
    Dim Quit As Boolean
    Dim Remainder As Long
    
    Dim HDR As FileHeader
    HDR.intBufferSize = bufsze
    'HDR.lngRemainder = Remainder
    
        sFilenameIn = App.Path & "\Test.txt"
        sFilenameOut = App.Path & "\Final.txt"
        If Dir(sFilenameOut) <> "" Then Kill sFilenameOut
        
        'Open the input file as binary
        FNumIn = FreeFile
        Open sFilenameIn For Binary Access Read Shared As #FNumIn
        lOffsetIn = LOF(FNumIn) + 1 'Get the total length of the file
        
        'Open the output file as binary
        FNumOut = FreeFile
        Open sFilenameOut For Binary Access Write As #FNumOut
    '    Put FNumOut, , HDR
            
        ReDim bBuf(bufsze)
        Do While Not Quit
            'Subtract the remaining total length from the buffer size to get the last positioning
            lOffsetIn = lOffsetIn - bufsze
            
            'Get the remaining
            If lOffsetIn < 0 Then
                Remainder = bufsze - lOffsetIn
                lOffsetIn = 1
                Quit = True
                ReDim bBuf(Remainder)
            End If
            
    '        Seek FNumIn, lOffsetIn
            
            'Get the binary from the position
            Get FNumIn, lOffsetIn, bBuf
            
            'Output it backout with the end of the input file
            Put FNumOut, , bBuf
        Loop
        
        Close #FNumIn
        Close #FNumOut
    End Sub
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    I hope you don't mind me changing your code around randem. I was having trouble but once I subtracted a 1 during the redim from the array, it worked fine. I also took off the +1 for the filesize. Anyway, if you can give me an example of a header to keep the remainder and the buffer size, I would appreciate it.

    Thanks

    Code:
    Private Sub Main()
        Call EncryptMe(App.Path & "\Test01.txt", App.Path & "\Test02.txt")
        Call EncryptMe(App.Path & "\Test02.txt", App.Path & "\Test03.txt")
    End Sub
    
    Private Sub EncryptMe(strSource As String, _
                            strTarget As String, _
                            Optional intBufferSize As Integer = 2)
                            
        If Dir(strTarget) <> "" Then Kill strTarget
                            
        Dim intFreeFileIn As Integer
        intFreeFileIn = FreeFile
        Open strSource For Binary Access Read Shared As #intFreeFileIn
        Dim lngFileSize As Long
        lngFileSize = LOF(intFreeFileIn)
        
        Dim intFreeFileOut As Integer
        intFreeFileOut = FreeFile
        Open strTarget For Binary Access Write As #intFreeFileOut
        Dim byteArr() As Byte
        ReDim byteArr(intBufferSize - 1)     'Subtract 1 because of 0 base array
        
        Dim lngPos As Long
        lngPos = lngFileSize
        Do
            lngPos = lngPos - intBufferSize
            If lngPos <= 0 Then Exit Do
            Get #intFreeFileIn, lngPos + 1, byteArr
            Put #intFreeFileOut, , byteArr
        Loop
        ReDim byteArr((intBufferSize + lngPos) - 1)
        Get #intFreeFileIn, 1, byteArr
        Put #intFreeFileOut, , byteArr
        
        Close #intFreeFileIn
        Close #intFreeFileOut
        
        Erase byteArr
    End Sub
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  21. #21
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    No, I don't mind at all. That is what you are supposed to do to make the code usable for your situation. I however don't understand what you mean about the header. You have the header defined. All you need to do is to write the header out then continue to write the file out after that. When you get to the end and have all your information for the header you back up to the beginning of the file and rewrite the header that will contain all the information about the file.

    You do need to store the block size for the file in your header also.

    Ex.
    Code:
    Private Type FileHeader
        BlockSize As Long
        Remainder As Long
        VerificationInfo(30) As String    ' Must use fixed sizes for your header
    End Type
    When you reverse it you read the header info. check to see if it is a valid header then use the information in the header to read the file to reverse it.
    Last edited by randem; Nov 5th, 2007 at 09:32 PM.

  22. #22
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,585

    Re: What is the fastest and simplest file encryption?

    Can you provide a sample like jeroen79 or provide a link that shows a sample?
    i don't program in vb6 any more, but based on memory, this code will probably work, but it is untested.
    Code:
     dim a$, workchar$, filelen&, workbyte as byte, Password as byte, cl as long
      password = 15 'set this to a value between 1 and 255 
                  'obviously, zero is allowed but it won't change anything
    open "filetoencrypt" for binary as #1
      FileLen = len(1)
      A$ = space(filelen) 'make a string the length of the file
      get #1, 1, a$ 'read entire file into a$
      for cl = 1 to len(a$) 'set up a loop the length of the file
          WorkChar = mid(a$, cl, 1) 'set workchar to a character from file
          WorkByte = asc(workchar) 'convert it to a byte
          workbyte = workbyte XOR Password 'encrypt the byte
          workchar = chr(workbyte) 'covert it back to a string
          mid(a$, cl, 1) = workchar 'update the file in memory, one char at a time
      next cl
      put #1, 1, a$ 'rewrite file
    close #1
    i would test this on a file copy before using it on anything important. You could also (assuming i didn't screw this up any) write to a 2nd file instead of overwriting the original.
    I don't understand? Please elaborate.
    if you have your folder's encryption enabled, then any other user won't be able to view the contents of the folder. And if they try to copy your files to another computer and view them, even administrators won't be able to because they will not have the correct certificate assigned to any of the users. Encryption is based on an unique string generated when an OS is installed which means if you reinstall your OS without backing up your certificates the files will be lost totally!. Hope that's clear.
    Thank You so far.[/QUOTE]

  23. #23

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Quote Originally Posted by randem
    You have the header defined. All you need to do is to write the header out then continue to write the file out after that. When you get to the end and have all your information for the header you back up to the beginning of the file and rewrite the header that will contain all the information about the file.

    You do need to store the block size for the file in your header also.

    When you reverse it you read the header info. check to see if it is a valid header then use the information in the header to read the file to reverse it.
    I know how to write the header out. However, the concept is what I am still trying to understand. I donít have all of the file info until I cycle to the end. How do I write it out there?

    Also, I am a bit confused how to read the header to get info and does this change the LOF of the file?


    Thanks Lord Orwell. I am going to look at your stuff right after I get this working.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  24. #24
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,585

    Re: What is the fastest and simplest file encryption?

    adding a header to the file will make the output file larger, but won't change the length of the original. However you must make sure you know how long you made the header because you will need to strip it off when you switch it back. It wouldn't actually harm anything to leave it on there, but your program would add a new one every time you re-encrypted it and the file would get bigger every time.

  25. #25

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Any samples to help me get started Lord?
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  26. #26

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    OK, so far, I got this much. I know how to write out and read in. I do have a question on how does the read in process knows the length of the header to read in. Also, after I read the header in, how do I read the rest of the data in?

    Code:
    Option Explicit
    
    Private Type tHeader
        MaxRecord As Long
    End Type
    
    Private Sub Main()
        Call Write(ActiveWorkbook.Path & "\Test.txt")
        Call Read(ActiveWorkbook.Path & "\Test.txt")
    End Sub
    
    Private Function Write(strFilename As String) As Long
        Dim udtHeader As tHeader
        udtHeader.MaxRecord = 5555
    
        Dim intFreeFile As Integer
        intFreeFile = FreeFile
        Open strFilename For Output As #intFreeFile
        Close #intFreeFile ' Close the file before opening in Binary mode
        
        intFreeFile = FreeFile
        Open strFilename For Binary Lock Read Write As #intFreeFile
        
        Put #intFreeFile, 1, udtHeader
        Put #intFreeFile, , "Test Data"
        
        Close #intFreeFile
    End Function
    
    Private Function Read(strFilename As String) As Long
        Dim udtHeader As tHeader
        
        Dim intFreeFile As Integer
        intFreeFile = FreeFile
        Open strFilename For Binary Lock Read Write As #intFreeFile
        
        Get #intFreeFile, 1, udtHeader
        MsgBox udtHeader.MaxRecord
        
        Close #intFreeFile
    End Function
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  27. #27
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    If you have a header you already know the length as per my example
    Code:
    Private Type FileHeader
        BlockSize As Long
        Remainder As Long
        VerificationInfo(30) As String    ' Must use fixed sizes for your header
    End Type
    That is the length...

  28. #28
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    Example

    Code:
    Dim FH as FileHeader
    Dim FHL as long
    
       FHL = LenB(FH)
    Now if you just set a buffer of size FHL then read into that buffer your next read will be the actual start of the file...

  29. #29
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,355

    Re: What is the fastest and simplest file encryption?

    Quote Originally Posted by Liquid Metal
    Does anyone know how to take the content of a binary file - reverse and save it?
    Code:
    Dim MyData As String
    
    Private Sub Form_Load()
    Open "OldFile" For Binary As #1
    MyData = Space$(LOF(1))
    Get #1, , MyData
    Open "NewFile" For Binary As #2
    For I = Len(MyData) To 1 Step -1
        Put #2, , Mid$(MyData, I, 1)
    Next
    Close
    End Sub
    This code builds a second file with the data in reverse order.
    Doctor Ed

  30. #30
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,585

    Re: What is the fastest and simplest file encryption?

    yes it does but it also writes it one byte at a time and this is incredibly slow. It would take about a minute to switch around a megabyte. You should do the byte reversal in memory into a 2nd string before re-writing it.
    Code:
    string2 = string1 'sets the size of string 2
    for cl = 1 to len(string1)
       mid(string2, cl, 1) = mid(string1, len(string1) - cl + 1, 1)
    next cl

  31. #31
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,355

    Re: What is the fastest and simplest file encryption?

    Quote Originally Posted by Lord Orwell
    yes it does but it also writes it one byte at a time and this is incredibly slow. It would take about a minute to switch around a megabyte. You should do the byte reversal in memory into a 2nd string before re-writing it.
    OK, I see what you mean. That takes more string space, but what the heck, memory is cheap these days. Here's my adjusted code for big files:
    Code:
    Dim MyOldData As String, MyNewData As String
    
    Private Sub Form_Load()
    Open "OldFile" For Binary As #1
    MyOldData = Space$(LOF(1))
    Get #1, , MyOldData
    For I = Len(MyOldData) To 1 Step -1
        MyNewData = MyNewData + Mid$(MyOldData, I, 1)
    Next
    Open "NewFile" For Binary As #2
    Put #2, , MyNewData
    Close
    End Sub
    Doctor Ed

  32. #32

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Quote Originally Posted by randem
    If you have a header you already know the length as per my example
    Code:
    Private Type FileHeader
        BlockSize As Long
        Remainder As Long
        VerificationInfo(30) As String    ' Must use fixed sizes for your header
    End Type
    That is the length...
    I did some research and I thought they recommend not to use the fix size data type when working with binary files. You do make sense because if you don't declare the string a fix size, how does it know the size when reading back right?

    Regardless, I am able to read back in the header but still having trouble reading the rest of the data.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  33. #33
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    Why are you having trouble. You have all the information from the header. After reading the header and getting the information. Redim the buffer to the blocksize from the header then read the file. The very next read will be the start of the file...
    Last edited by randem; Nov 6th, 2007 at 07:01 PM.

  34. #34
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,585

    Re: What is the fastest and simplest file encryption?

    Quote Originally Posted by Code Doc
    OK, I see what you mean. That takes more string space, but what the heck, memory is cheap these days. Here's my adjusted code for big files:
    Code:
    Dim MyOldData As String, MyNewData As String
    
    Private Sub Form_Load()
    Open "OldFile" For Binary As #1
    MyOldData = Space$(LOF(1))
    Get #1, , MyOldData
    For I = Len(MyOldData) To 1 Step -1
        MyNewData = MyNewData + Mid$(MyOldData, I, 1)
    Next
    Open "NewFile" For Binary As #2
    Put #2, , MyNewData
    Close
    End Sub
    not exactly. If you append a string over and over like that, you are redimensioning the memory area every time you append one character to it. That was why the code sample i posted set up a 2nd string of the same length first. When speed is a concern, every little bit helps.

  35. #35

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    Quote Originally Posted by randem
    Why are you having trouble. You have all the information from the header. After reading the header and getting the information. Redim the buffer to the blocksize from the header then read the file. The very next read will be the start of the file...
    That is where I am getting confuse. So the only way to GET the data back is by redim the array right?

    If I was to use a
    Code:
    PUT #File, , "TEST"
    , is there any way of pulling the data back without using a byte array?

    Also, for the header, what do you save in it? The file size, remainder and anything else?

    Thanks
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  36. #36
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: What is the fastest and simplest file encryption?

    Try reading the data back into the header, then redim the array to the buffer size then read the data...

  37. #37

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: What is the fastest and simplest file encryption?

    hmmmm....

    Anyway, don't seem like I am getting any where with my questions. Thanks for the initial start but I am on a deadline for my project so will resolve this on my own at another time.
    Last edited by Liquid Metal; Nov 7th, 2007 at 04:13 AM.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  38. #38
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,585

    Re: [RESOLVED] What is the fastest and simplest file encryption?

    Really don't know what the issue is here.
    #1 you load the original file into memory
    #2, you created the header with the size of the original file stored in it.
    #3, you wrote the header
    #4, you appended the reversed file with a 2nd PUT statement
    so reading it back in reverse
    #1 you open the file and read the header(and it HAS to be a fixed size no matter what some other idiot is telling you to confuse you. Trust Randem on this, how else would you know how big the header is?)
    #2 you take the size of the original file from the newly read header and redim the variable to that size so the next GET statement knows how much data to read
    #3 You call the GET statement with the newly dimmed array and it will read the original file into memory(but still reversed)
    #4 you reverse it and write it back to disk with a simple PUT statement.

    It's really not that complicated.

  39. #39
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,792

    Re: What is the fastest and simplest file encryption?

    Hi Liquid Metal,

    I know you marked the thread as resolved, but I see you still have problems.

    Take a look at this example:
    VB - 128, 160 and 256 Bit File Encryption/Decryption with MD5, SHA1 and SHA256
    The encryption is very strong, I know you asked for a simple encryption, but you don't have to understand it. Just look at how the header file is saved in the encrypted file, and after that you have the encrypted data, just as you want to do.

    As for your questions:
    Quote Originally Posted by Liquid Metal
    If I was to use a
    Code:
    PUT #File, , "TEST"
    , is there any way of pulling the data back without using a byte array?

    Also, for the header, what do you save in it? The file size, remainder and anything else?
    To read that "test" into a string variable, you do it like this.
    Code:
        Dim StrData As String
        
        StrData = String(4, 0)
        Get #File, , StrData
    Note that i'm setting the string variable to an empty 4 character string. This is done to "make" a buffer.
    The Get function does not know how much data to read from the file, 1 byte ?, 2 bytes ?, etc. That is why you have to set the buffer to a predefined length.... also, that is why you need a header in you encrypted file, to define what you have in the file, so then later you know how to read the file. You read the header that is always the same length, where in the header it describes what is in the rest of the file, so you know what to read after the header.

    For example, in the link with my encryption, you will see that in the header i save:
    3 bytes to define that that is an encrypted file
    1 byte for encryption algorithm (one of 128 bit or 160 bit or 256 Bit)
    4 bytes to store a random number that helps making the encryption stronger
    16 or 20 or 32 bytes of the encrypted password (to verify if the password is correct when decrypting the file)
    Then the actual encrypted data.

    Note that the header in my case is not always the same length, because the encrypted password could be 16 or 20 or 32 bytes. But because before that I save the encryption algorithm, I know what to expect when reading the file.

    Saving in the header what algorithm I used to encrypt the file also helps decrypting the file, otherwise I won't know what encryption strength to use when decrypting the file.

    Anyways... I hope you have a better idea now why you need a header.

    But in your case when you use such a simple encryption (personally I would not even call it encryption), you don't really need a header. If you do use a header it would be just to flag the file that the file is encrypted or not.... or maybe even the file name of the original file if you change the file name or extension after you encrypt it.
    Last edited by CVMichael; Nov 7th, 2007 at 09:33 AM.

  40. #40

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: [RESOLVED] What is the fastest and simplest file encryption?

    CVMichael,

    Thanks for taking the time to answer my post. Thanks for posting the code for obtaining the string. I was trying to understand on all the different methods to obtain data from a binary file.

    I knew that you are specialized in encryption/decryption because I have viewed your http://www.vbforums.com/showthread.php?t=232284 last year. Frankly, before starting this thread, I was going to use your encryption program but was concerned about the speed performance since it is strong encryption.

    I was going to make something simple so I can learn the basic concept first and was going to revisit your encryption program at a later time.

    Randem, LO and the rest have been really helpful. So far, with their help, I was able to code this out. If the code needs to be cleaned, please point out. Thanks

    Code:
    Private Type udtHeader
        lngBufferSize As Long
        lngRemainderByte As Long
        intEncrypted As Integer
    '    strEncrypted As String * 10
    End Type
    
    Private Sub Flip(strSource As String, strTarget As String)
        If Dir(strTarget) <> "" Then Kill strTarget
        
        ' Open as Read - Input File
        Dim intFreeFileIn As Integer
        intFreeFileIn = FreeFile
        Open strSource For Binary Access Read Shared As #intFreeFileIn
        
        Dim udtHD As udtHeader
        Get #intFreeFileIn, 1, udtHD
        
        ' Open as Write - Output File
        Dim intFreeFileOut As Integer
        intFreeFileOut = FreeFile
        Open strTarget For Binary Access Write As #intFreeFileOut
                            
        Dim lngPos As Long
        Dim byteArr() As Byte
        Dim intBufferSize As Integer
        If udtHD.intEncrypted <> 12345 Then
            ' Allocate the space
            Put #intFreeFileOut, 1, udtHD
        
            intBufferSize = Int((5 * Rnd) + 1)   ' Generate random value between 1 and 5.
            lngPos = LOF(intFreeFileIn)    'Input File Size
        Else
            lngPos = LOF(intFreeFileIn) - udtHD.lngRemainderByte
            intBufferSize = udtHD.lngBufferSize
            
            'Write out the remainder first
            ReDim byteArr(udtHD.lngRemainderByte - 1)     'Subtract 1 because of 0 base array
            Get #intFreeFileIn, lngPos + 1, byteArr
            Put #intFreeFileOut, , byteArr
        End If
        
        ReDim byteArr(intBufferSize - 1)     'Subtract 1 because of 0 base array
        Do
            ' Work from the end of the file and subtract the buffer
            lngPos = lngPos - intBufferSize
            If udtHD.intEncrypted <> 12345 Then
                If lngPos <= 0 Then Exit Do
            Else
                If lngPos < Len(udtHD) Then Exit Do
            End If
            
            ' Read In - Write Out
            Get #intFreeFileIn, lngPos + 1, byteArr
            Put #intFreeFileOut, , byteArr
        Loop
        
        If udtHD.intEncrypted <> 12345 Then
            ' Read In - Write Out - the REMAINING Bytes
            ReDim byteArr((intBufferSize + lngPos) - 1)
            Get #intFreeFileIn, 1, byteArr  'LenB(udtHD)
            Put #intFreeFileOut, , byteArr
            
            udtHD.lngBufferSize = intBufferSize
            udtHD.lngRemainderByte = intBufferSize + lngPos
            udtHD.intEncrypted = 12345
            Put #intFreeFileOut, 1, udtHD
        End If
        
        Erase byteArr
        Close #intFreeFileIn
        Close #intFreeFileOut
    End Sub
    Last edited by Liquid Metal; Nov 7th, 2007 at 09:01 PM.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

Page 1 of 2 12 LastLast

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width