Announcement

Collapse
No announcement yet.

Better Methods Than This?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Better Methods Than This?

    Greetings!

    I've some working code that I'm hoping to streamline. I'm open to varying interpretations of better algorithms that accomplish the same task. Here is the code that encrypts/decrypts:

    Code:
    #COMPILE EXE
    #DIM ALL
    DEFDWD A - Z
    
    DECLARE SUB Encrypt (BYVAL TxtFileName AS STRING)
    
    FUNCTION PBMAIN()
      Encrypt "aeotp10.txt"
    END FUNCTION
    
    'For encryption/decryption you basically just need to XOR the bytes with the correct key.
    
    '1. Copy first 12 bytes.
    '2. XOR next 12 bytes with this key:
    
    '       BYTE hkey[12] = {0xA7, 0x8F, 0x56, 0xFD, 0x3E, 0x1D, 0x7C, 0xBD, 0xDC, 0xE6, 0xBE, 0x6D };
    
    '3. XOR the bytes to the end of the data file with this key:
    
    '       BYTE key[35] = { 0x80, 0xDD, 0x13, 0x7F, 0x8C, 0xDA, 0x80, 0xC2, 0xA7, 0x51, 0xEA, 0x21,
    '       0x86, 0xA1, 0xAE, 0xF0, 0x1D, 0xB1, 0xFC, 0x1D, 0xA1, 0x55, 0xDC, 0x9C, 0x47, 0x49, 0x80,
    '       0x8B, 0xBF, 0x60, 0x65, 0xFD, 0xDA, 0xFD, 0x47 };
    
    SUB Encrypt (BYVAL TxtFileName AS STRING)
      DIM sFile   AS STRING ' For storing File Name to be encrypted/Decrypted
      DIM lFnum1  AS LONG   ' For Source File
      DIM lFnum2  AS LONG   ' For Temprory Destination File
      DIM lPos    AS LONG   ' For Iterating through each byte in Source File
      DIM bData   AS BYTE   ' For Reading Data from Source File
      DIM bDataC  AS BYTE   ' For storing Complemented (or encrypted) bData in  Destination File
    
      DIM sKey1(12) AS INTEGER ' 0xA7, 0x8F, 0x56, 0xFD, 0x3E, 0x1D, 0x7C, 0xBD, 0xDC, 0xE6, 0xBE, 0x6D
      DIM sKey2(35) AS INTEGER
    
      sKey1(0)  = &HA7
      sKey1(1)  = &H8F
      sKey1(2)  = &H56
      sKey1(3)  = &HFD
      sKey1(4)  = &H3E
      sKey1(5)  = &H1D
      sKey1(6)  = &H7C
      sKey1(7)  = &HBD
      sKey1(8)  = &HDC
      sKey1(9)  = &HE6
      sKey1(10) = &HBE
      sKey1(11) = &H6D
    
      sKey2(0)  = &H80
      sKey2(1)  = &HDD
      sKey2(2)  = &H13
      sKey2(3)  = &H7F
      sKey2(4)  = &H8C
      sKey2(5)  = &HDA
      sKey2(6)  = &H80
      sKey2(7)  = &HC2
      sKey2(8)  = &HA7
      sKey2(9)  = &H51
      sKey2(10) = &HEA
      sKey2(11) = &H21
      sKey2(12) = &H86
      sKey2(13) = &HA1
      sKey2(14) = &HAE
      sKey2(15) = &HF0
      sKey2(16) = &H1D
      sKey2(17) = &HB1
      sKey2(18) = &HFC
      sKey2(19) = &H1D
      sKey2(20) = &HA1
      sKey2(21) = &H55
      sKey2(22) = &HDC
      sKey2(23) = &H9C
      sKey2(24) = &H47
      sKey2(25) = &H49
      sKey2(26) = &H80
      sKey2(27) = &H8B
      sKey2(28) = &HBF
      sKey2(29) = &H60
      sKey2(30) = &H65
      sKey2(31) = &HFD
      sKey2(32) = &HDA
      sKey2(33) = &HFD
      sKey2(34) = &H47
    
      lPos = 1
      sFile = TxtFileName
      lFnum2 = FREEFILE
      OPEN "tmp" FOR BINARY ACCESS WRITE AS #lFnum2
        lFnum1 = FREEFILE
        OPEN sFile FOR BINARY ACCESS READ AS #lFnum1
          DO WHILE NOT EOF(lFnum1)
            GET #lFnum1, lPos, bData  'Reading Data Byte by Byte from Source File
            IF NOT EOF(lFnum1) THEN
              IF lPos <= 12 THEN ' just copy the data
                bDataC = bData
              ELSE
                IF lPos <= 24 THEN   'Encrypt/Decrypt with first key
                  bDataC = bData XOR sKey1(lPos - 12 - 1)
                ELSE                 'Encrypt/Decrypt with second key
                  bDataC = bData XOR sKey2((lPos - 24 - 1) MOD 35)
                END IF
              END IF
              PUT #lFnum2, lPos, bDataC 'Writing Encrypted Data To Temporary Destination File
              lPos = lPos + 1
            END IF
          LOOP
        CLOSE #lFnum1
      CLOSE #lFnum2
    
      KILL sFile ' Deleting Source File
      NAME "tmp" AS sFile 'Renaming Temporary Destination File to Real File Name
    END SUB
    ...I've attached the source, a sample text and compiled version with PB/CC 4 if you'd like to download that instead. Much thanks.
    Attached Files
    Donnie Ewald
    [email protected]

  • #2
    You should use pointers instead of arrays for much faster processing and easier management.

    Here's a small example of what I mean
    Code:
    Local String1 As String  ' Data to be encrypted/decrypted
    Local Passwd As String   ' Encryption password / key
    Local OutStr As String   ' Encrypted Output
    Local s      As Byte Ptr
    Local p      As Byte Ptr
    Local o      As Byte Ptr
    Local i      As Dword
    Local t      As Dword
     
    String1 = "Some Data Here, could be read from a file or wherever"
    Passwd = "Encryption Key Password
    OutStr = String1  ' Reserve equal space in output string as original string
     
    s = StrPtr(String1)
    p = StrPtr(Passwd)
    o = StrPtr(OutStr)
     
    For i = 0 To Len(String1)-1
       o[i] = s[i] Xor p[i]  ' Encrypt 1 byte using simple XOR, not very strong at all, but this is to demo pointers not encryption.
    Next i
    This is much easier to manage, and much faster.
    Scott Slater
    Summit Computer Networks, Inc.
    www.summitcn.com

    Comment


    • #3
      Greetings Scott!

      Much thanks for the tips. I took your code and tinkered with it to come up with this working sample:

      Code:
      #COMPILE EXE
      #DIM ALL
      DEFDWD A - Z
      
      DECLARE FUNCTION NewEncrypt (BYVAL TxtFileName AS STRING) AS STRING
      DECLARE FUNCTION Encrypt (BYVAL TxtFileName AS STRING) AS STRING
      DECLARE FUNCTION GetFile(WildCard$) AS STRING
      
      FUNCTION PBMAIN()
        LOCAL CString AS STRING
      
        PRINT "Ruler:   1111111111222222222233333333334444444444555555555566666666667777777777"
        PRINT "1234567890123456789012345678901234567890123456789012345678901234567890123456789"
        PRINT
      
        PRINT "My Results:"
        CString = NewEncrypt("aeotp10.txt")
        PRINT LEFT$(CString, 12)
        PRINT MID$(CString, 13, 12)
        PRINT RIGHT$(CString, 30)
      
        PRINT
      
        PRINT "Expected Results:"
        CString = Encrypt("aeotp10.txt")
        PRINT LEFT$(CString, 12)
        PRINT MID$(CString, 13, 12)
        PRINT RIGHT$(CString, 30)
      
        WAITKEY$
      END FUNCTION
      
      FUNCTION NewEncrypt (BYVAL TxtFileName AS STRING) AS STRING
        LOCAL String1 AS STRING  ' Full Data to be encrypted/decrypted
        LOCAL String2 AS STRING  ' Data to be encrypted/decrypted with First Key
        LOCAL String3 AS STRING  ' Data to be encrypted/decrypted with Second Key
        LOCAL CString AS STRING  ' Data to be returned
        LOCAL Passwd AS STRING   ' Encryption password / key
        LOCAL OutStr AS STRING   ' Encrypted Output
        LOCAL s      AS BYTE PTR
        LOCAL p      AS BYTE PTR
        LOCAL o      AS BYTE PTR
        LOCAL i      AS DWORD
      
        String1 = GetFile(TxtFileName)
        CString = LEFT$(String1, 12)
        String2 = MID$(String1, 13, 12)
        String3 = RIGHT$(String1, LEN(String1)-24)
      
        Passwd = CHR$(&hA7, &h8F, &h56, &hFD, &h3E, &h1D, &h7C, &hBD, &hDC, &hE6, &hBE, &h6D)
        OutStr = String2
      
        s = STRPTR(String2)
        p = STRPTR(Passwd)
        o = STRPTR(OutStr)
      
        FOR i = 0 TO LEN(String2)-1
          @o[i] = @s[i] XOR @p[i]  ' Encrypt 1 byte using simple XOR, not very strong at all, but this is to demo pointers not encryption.
        NEXT i
      
        CString = CString + OutStr
      
        Passwd = CHR$(&h80, &hDD, &h13, &h7F, &h8C, &hDA, &h80, &hC2, &hA7, &h51, &hEA, &h21, _
          &h86, &hA1, &hAE, &hF0, &h1D, &hB1, &hFC, &h1D, &hA1, &h55, &hDC, &h9C, &h47, &h49, &h80, _
          &h8B, &hBF, &h60, &h65, &hFD, &hDA, &hFD, &h47)
        OutStr = String3
      
        s = STRPTR(String3)
        p = STRPTR(Passwd)
        o = STRPTR(OutStr)
      
        FOR i = 0 TO LEN(String3)-1
          @o[i] = @s[i] XOR @p[i MOD 35]  ' Encrypt 1 byte using simple XOR, not very strong at all, but this is to demo pointers not encryption.
        NEXT i
      
        CString = CString + OutStr
        FUNCTION = CString
      END FUNCTION
      
      
      'For encryption/decryption you basically just need to XOR the bytes with the correct key.
      
      '1. Copy first 12 bytes.
      '2. XOR next 12 bytes with this key:
      
      '       BYTE hkey[12] = {0xA7, 0x8F, 0x56, 0xFD, 0x3E, 0x1D, 0x7C, 0xBD, 0xDC, 0xE6, 0xBE, 0x6D };
      
      '3. XOR the bytes to the end of the data file with this key:
      
      '       BYTE key[35] = { 0x80, 0xDD, 0x13, 0x7F, 0x8C, 0xDA, 0x80, 0xC2, 0xA7, 0x51, 0xEA, 0x21,
      '       0x86, 0xA1, 0xAE, 0xF0, 0x1D, 0xB1, 0xFC, 0x1D, 0xA1, 0x55, 0xDC, 0x9C, 0x47, 0x49, 0x80,
      '       0x8B, 0xBF, 0x60, 0x65, 0xFD, 0xDA, 0xFD, 0x47 };
      FUNCTION Encrypt (BYVAL TxtFileName AS STRING) AS STRING
        DIM sFile   AS STRING ' For storing File Name to be encrypted/Decrypted
        DIM lFnum1  AS LONG   ' For Source File
        DIM lFnum2  AS LONG   ' For Temprory Destination File
        DIM lPos    AS LONG   ' For Iterating through each byte in Source File
        DIM bData   AS BYTE   ' For Reading Data from Source File
        DIM bDataC  AS BYTE   ' For storing Complemented (or encrypted) bData in  Destination File
      
        DIM sKey1(12) AS INTEGER ' 0xA7, 0x8F, 0x56, 0xFD, 0x3E, 0x1D, 0x7C, 0xBD, 0xDC, 0xE6, 0xBE, 0x6D
        DIM sKey2(35) AS INTEGER
      
        sKey1(0)  = &HA7
        sKey1(1)  = &H8F
        sKey1(2)  = &H56
        sKey1(3)  = &HFD
        sKey1(4)  = &H3E
        sKey1(5)  = &H1D
        sKey1(6)  = &H7C
        sKey1(7)  = &HBD
        sKey1(8)  = &HDC
        sKey1(9)  = &HE6
        sKey1(10) = &HBE
        sKey1(11) = &H6D
      
        sKey2(0)  = &H80
        sKey2(1)  = &HDD
        sKey2(2)  = &H13
        sKey2(3)  = &H7F
        sKey2(4)  = &H8C
        sKey2(5)  = &HDA
        sKey2(6)  = &H80
        sKey2(7)  = &HC2
        sKey2(8)  = &HA7
        sKey2(9)  = &H51
        sKey2(10) = &HEA
        sKey2(11) = &H21
        sKey2(12) = &H86
        sKey2(13) = &HA1
        sKey2(14) = &HAE
        sKey2(15) = &HF0
        sKey2(16) = &H1D
        sKey2(17) = &HB1
        sKey2(18) = &HFC
        sKey2(19) = &H1D
        sKey2(20) = &HA1
        sKey2(21) = &H55
        sKey2(22) = &HDC
        sKey2(23) = &H9C
        sKey2(24) = &H47
        sKey2(25) = &H49
        sKey2(26) = &H80
        sKey2(27) = &H8B
        sKey2(28) = &HBF
        sKey2(29) = &H60
        sKey2(30) = &H65
        sKey2(31) = &HFD
        sKey2(32) = &HDA
        sKey2(33) = &HFD
        sKey2(34) = &H47
      
        lPos = 1
        sFile = TxtFileName
        lFnum2 = FREEFILE
        OPEN "tmp" FOR BINARY ACCESS WRITE AS #lFnum2
          lFnum1 = FREEFILE
          OPEN sFile FOR BINARY ACCESS READ AS #lFnum1
            DO WHILE NOT EOF(lFnum1)
              GET #lFnum1, lPos, bData  'Reading Data Byte by Byte from Source File
              IF NOT EOF(lFnum1) THEN
                IF lPos <= 12 THEN ' just copy the data
                  bDataC = bData
                ELSE
                  IF lPos <= 24 THEN   'Encrypt/Decrypt with first key
                    bDataC = bData XOR sKey1(lPos - 12 - 1)
                  ELSE                 'Encrypt/Decrypt with second key
                    bDataC = bData XOR sKey2((lPos - 24 - 1) MOD 35)
                  END IF
                END IF
                PUT #lFnum2, lPos, bDataC 'Writing Encrypted Data To Temporary Destination File
                lPos = lPos + 1
              END IF
            LOOP
          CLOSE #lFnum1
        CLOSE #lFnum2
      
        sFile = GetFile("tmp")
        KILL "tmp"
        FUNCTION = sFile
      END FUNCTION
      
      FUNCTION GetFile(WildCard$) AS STRING
        DIM f AS DWORD
        DIM buffer AS STRING
      
        f = FREEFILE 'get file contents
        OPEN WildCard$ FOR BINARY AS f LEN = 16384
          GET$ f, LOF(f), Buffer
        CLOSE f
      
        FUNCTION = Buffer
      END FUNCTION
      ...it's substancially faster than what I had before which is quite nice. I'm not quite comprehending everything that is happening with pointers and such in my new FUNCTION though. I also learned about MOD which I don't recall using in any code that I've written. The results on several test files match up with the expected output which is perfect.

      Since I need to handle each file in three different chunks: Chunk 1 is unencrypted, Chunk 2 is with Key 1 and Chunk 3 is with Key 2; is there a more efficient way than what I used? I wouldn't mind minimizing the string handling which is typically a bottleneck.
      Attached Files
      Donnie Ewald
      [email protected]

      Comment


      • #4
        >GET #lFnum1, lPos, bData 'Reading Data Byte by Byte from Source File

        I know there are ways to do this a lot faster. But if your file is small enough it won't really matter since a lot of data are cached anyway. (Windows itself always reads in page size multiples).
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Greetings Michael!

          I know little about the Windows' workings that deep; but, I did replace the original Encrypt FUNCTION with some code suggested by Scott that is found in the NewEncrypt FUNCTION. I now use:
          Code:
          FUNCTION GetFile(WildCard$) AS STRING
            DIM f AS DWORD
            DIM buffer AS STRING
          
            f = FREEFILE 'get file contents
            OPEN WildCard$ FOR BINARY AS f LEN = 16384
              GET$ f, LOF(f), Buffer
            CLOSE f
          
            FUNCTION = Buffer
          END FUNCTION
          ...to load files which has been much faster for me.
          Donnie Ewald
          [email protected]

          Comment


          • #6
            Michael is correct that windows usually reads files in 64KB chunks to cache so most work is memory based but of course you are doing an OS call for every byte.
            You use this for one input file
            Code:
              DIM f AS DWORD
              DIM buffer AS STRING
            
              f = FREEFILE 'get file contents
              OPEN WildCard$ FOR BINARY AS f  LEN = 16384
                GET$ f, LOF(f), Buffer
              CLOSE f
            The LEN statement is invalid on a binary file so should be removed but you have shown the fastest way to get a file (of reasonable size) into a programs memory so why not do the same with 1fnum, something like this
            Code:
                LOCAL 1fnum1buffer AS STRING
                LOCAL 1fnum1 AS LONG
                LOCAL 1fnum1ptr AS BYTE PTR
                LOCAL x AS LONG
                1fnumb = FREEFILE
                OPEN sFile FOR BINARY ACCESS READ AS #lFnum1
                GET$ 1Fnum1,LOF(1Fnum1), 1Fnum1buffer
                1Fnum1ptr = STRPTR(1Fum1Buffer)
                FOR x = 0 TO LEN(1FnumBuffer) - 1
                    @1Fnumptr[x] 'will give you access to the bytes 1 at a time
                NEXT
            The output file might be a little harder depending on how it is different in size to the input file

            Comment


            • #7
              Don,
              a better method?
              The most important saving has been made, read the file in big chunks as that's a lot faster.

              Further savings can be made by:
              XORing 4 byte blocks at a time (LONGs) instead of 1 byte.
              Your first (12 byte) loop can be replaced with 3 inline XORs of 4 bytes each.

              Removing that MOD from every byte you XOR by unrolling that big loop into chunks of 36 bytes at a time (that'd be 9 LONG XORs).

              Don't break the strings into parts, just operate on the one, initial string. You know where the parts of the string are and can access them in place so why break it up?

              Paul.

              Comment


              • #8
                This is completely untested but shows what I had in mind:
                Code:
                FUNCTION Encrypt (BYVAL TxtFileName AS STRING) AS STRING      
                REGISTER i AS LONG
                LOCAL String1 AS STRING
                
                'get the data into memory
                  String1 = GetFile(TxtFileName) + STRING$(36," ") 'make the string 36 bytes too long so I don't need to worry about overrunning the end of it
                    
                  
                  'do the first 12 bytes .. i.e. just leave them alone.
                  
                  'now do the second 12 bytes
                LOCAL pLong AS LONG PTR
                  pLong = STRPTR(String1)+12   'point to the 12th byte in the string
                
                  @pLong    =    @pLong XOR &hFD568FA7    'XOR 4 bytes at a time
                  @pLong[1] = @pLong[1] XOR &hBD7C1D3E    'the offset is 1 and 2 as the pointer is pointing to LONGs so it's actually 4 and 8 bytes
                  @pLong[2] = @pLong[2] XOR &h6DBEE6DC    'the password bytes are combined into blocks of 4
                  
                  'now do the remainder of the string
                  pLong = pLong+12   'point to the next (24th) byte in the string
                  
                  
                  FOR i = 0 TO LEN(String1)\35   '\35 because I'm doing 35 bytes in each pass
                      @pLong    = @pLong    XOR &h7F13DD80
                      @pLong[1] = @pLong[1] XOR &hC280DA8C
                      @pLong[2] = @pLong[2] XOR &h21EA51A7
                      @pLong[3] = @pLong[3] XOR &hF0EAA186
                      @pLong[4] = @pLong[4] XOR &h1DFCB11D
                      @pLong[5] = @pLong[5] XOR &h9CDC55A1
                      @pLong[6] = @pLong[6] XOR &h8B804947
                      @pLong[7] = @pLong[7] XOR &hFD6560BF
                      @pLong[8] = @pLong[8] XOR &h0047FDDA      'note 36th byte is zero so it'll not affect anything when XORed
                      
                      pLong=pLong+35                             'point to next block of 35
                  
                  NEXT
                  
                  FUNCTION = LEFT$(String1,LEN(String1)-36)    'remove the extra 36 bytes I added at the start and return the answer  
                END FUNCTION

                Comment


                • #9
                  Greetings Paul!

                  I appreciate the ideas and I'll try to sift through your code as it's above my understanding. I mentioned before that I know little of pointers and your example has a hiccup in the FOR/NEXT loop (I think) somewhere and I don't understand what's going wrong.
                  Donnie Ewald
                  [email protected]

                  Comment


                  • #10
                    Like Don I'm still learning pointers and this is my first foray into encryption as well. (Just a curiosity for me as I have no need of it.).

                    Paul, I took your code and (attempted) to put it in PBWin. There are 2 problems.

                    1) The returned string is the right length (same Len() as sent) but only appears to be 100 or so chars long in the Msgbox. Is it possible the E method is putting a Chr$(0)s in it?

                    2) It GPF's after returning from the Encryption function.

                    The test data sent is 35 or so lines of:
                    1234567890_abcdefghijkilmnöpqrstuvwxyz.Encryption_Text_Gösta H. Lovgren
                    Also the Output file only has 15 lines of varying length in it. Is that supposed to be right?
                    '
                    Code:
                    'PBWIN 9.00 - WinApi 05/2008 - XP Pro SP3
                    #Compile Exe                                
                    #Dim All 
                    #Include "WIN32API.INC"
                    #Include "COMDLG32.INC"
                     
                    Function GetFile(fl$) As String
                      Local fnum, flen, n As Long
                      fnum = FreeFile   
                      Open fl$ For Binary As #fnum
                        n = Lof(#fnum) 
                        fl$=Space$(n)
                        Get #fnum, , fl$
                      Close #fnum 
                      Function = fl$   
                    End Function 
                    '
                    Function PutFile(fl$, s$) As Long
                      Local fnum, flen, n As Long
                      fnum = FreeFile   
                      Open fl$ For Binary As #fnum
                        Put #fnum, 1, s$
                      Close #fnum 
                    End Function 
                    '
                    Function Encrypt (ByVal TxtFileName As String) As String      
                    'http://www.powerbasic.com/support/pbforums/showthread.php?t=38942  - Paul Dixon
                      Register i As Long
                      Local fl, String1 As String
                        
                      'get the data into memory
                        String1 = GetFile(TxtFileName) + String$(36," ") 'make the string 36 bytes too long so I don't need to worry about overrunning the end of it
                         ? Str$(Len(string1))  & $CrLf & String1, , "Returned from file " & FuncName$
                      
                        'do the first 12 bytes .. i.e. just leave them alone.
                        
                        'now do the second 12 bytes
                      Local pLong As Long Ptr
                        pLong = StrPtr(String1) + 12   'point to the 12th byte in the string
                      
                        @pLong    =    @pLong Xor &hFD568FA7    'XOR 4 bytes at a time
                        @pLong[1] = @pLong[1] Xor &hBD7C1D3E    'the offset is 1 and 2 as the pointer is pointing to LONGs so it's actually 4 and 8 bytes
                        @pLong[2] = @pLong[2] Xor &h6DBEE6DC    'the password bytes are combined into blocks of 4
                        
                        'now do the remainder of the string
                        pLong = pLong+12   'point to the next (24th) byte in the string
                        
                        
                        For i = 0 To Len(String1)\35   '\35 because I'm doing 35 bytes in each pass
                            @pLong    = @pLong    Xor &h7F13DD80
                            @pLong[1] = @pLong[1] Xor &hC280DA8C
                            @pLong[2] = @pLong[2] Xor &h21EA51A7
                            @pLong[3] = @pLong[3] Xor &hF0EAA186
                            @pLong[4] = @pLong[4] Xor &h1DFCB11D
                            @pLong[5] = @pLong[5] Xor &h9CDC55A1
                            @pLong[6] = @pLong[6] Xor &h8B804947
                            @pLong[7] = @pLong[7] Xor &hFD6560BF
                            @pLong[8] = @pLong[8] Xor &h0047FDDA      'note 36th byte is zero so it'll not affect anything when XORed
                          
                            pLong=pLong+35                             'point to next block of 35
                        Next
                      
                    '  Function = Left$(String1,Len(String1) - 36)    'remove the extra 36 bytes I added at the start and return the answer  
                       Function = string1
                    End Function
                     
                    Function PBMain
                        Local fl, s As String                        
                        fl$ = CurDir$ & "\Cryption_Test_Data.txt"'TxtFileName
                        s$ = Encrypt(fl$)    
                        
                        fl$ = CurDir$ & "\Cryption_Test_Crypted.txt"'TxtFileName
                        PutFile(fl$, s$)                 
                        
                        MsgBox Str$(Len(s$))  & $CrLf & s$, , "Returned Ed/DeCryption technique in " & FuncName$
                     
                         '<<< GPF's here
                        
                    End Function
                    '
                    It's a pretty day. I hope you enjoy it.

                    Gösta

                    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                    Comment


                    • #11
                      The code I posted was intended as a guide, nothing more.
                      It looks like I might have the loop index wrong. Make it:
                      Code:
                      FOR i = 1 TO LEN(String1)\35
                      ..and the code appears to give the same results as the original.


                      Gösta,
                      it would be no surprise if a multiline test file returned with a differetn number of lines as the CR/LF characters are encrypted along with the rest.
                      This will also apply to CHR$(0), they might well appear in the final output BUT the original code should also produce the same final output if all is working.

                      Paul.

                      Comment


                      • #12
                        Greetings Paul!

                        Thanks for the example--and debugging it too! Pointers have made a substancial decrease in the amount of time to process a file. I need to spend more time learning about them.

                        Thanks to all for the disussion and examples.
                        Donnie Ewald
                        [email protected]

                        Comment


                        • #13
                          Originally posted by Paul Dixon View Post
                          Gösta,
                          it would be no surprise if a multiline test file returned with a differetn number of lines as the CR/LF characters are encrypted along with the rest.
                          Yes, I realized that after I posted.
                          This will also apply to CHR$(0), they might well appear in the final output BUT the original code should also produce the same final output if all is working.

                          Paul.
                          And it does. I did a little more work on the example this morning, which (for me anyway) clears a little of the fog away.

                          Your examples, as always, Paul are succinct and easy to follow. Thanks.

                          Don, Paul has a dandy example on Pointer usage here: http://www.powerbasic.com/support/pb...ad.php?t=38633 . You may find it useful, I sure did/do.

                          '
                          Code:
                          'PBWIN 9.00 - WinApi 05/2008 - XP Pro SP3
                          #Compile Exe                                
                          #Dim All 
                          #Include "WIN32API.INC"
                          #Include "COMDLG32.INC"
                           
                          Global Stp As Long 'keep track of steps in example
                           
                          Function GetFile(fl$) As String
                            Local fnum, flen, n As Long
                            fnum = FreeFile   
                            Open fl$ For Binary As #fnum
                              n = Lof(#fnum) 
                              fl$=Space$(n)
                              Get #fnum, , fl$
                            Close #fnum 
                            Function = fl$   
                          End Function 
                          '
                          Function PutFile(fl$, s$) As Long
                            Local fnum, flen, n As Long
                            fnum = FreeFile   
                            Open fl$ For Binary As #fnum
                              Put #fnum, 1, s$
                            Close #fnum 
                          End Function 
                          '
                          Function Encrypt (ByVal TxtFileName As String) As String      
                          'http://www.powerbasic.com/support/pbforums/showthread.php?t=38942  - Paul Dixon
                            Register i As Long
                            Local s1, fl, String1 As String
                              s1$ = TxtFileName    'temp holder
                              
                            'get the data into memory
                              String1 = GetFile(TxtFileName) + String$(36," ") 'make the string 36 bytes too long so I don't need to worry about overrunning the end of it
                                 Incr stp
                                ? Str$(Len(string1)) & $CrLf & String1, , Using$("Step # Data to work on from ", Stp) & s1$ & " in " & FuncName$
                            
                              'do the first 12 bytes .. i.e. just leave them alone.
                              
                              'now do the second 12 bytes
                            Local pLong As Long Ptr
                              pLong = StrPtr(String1) + 12   'point to the 12th byte in the string
                            
                              @pLong    =    @pLong Xor &hFD568FA7    'XOR 4 bytes at a time
                              @pLong[1] = @pLong[1] Xor &hBD7C1D3E    'the offset is 1 and 2 as the pointer is pointing to LONGs so it's actually 4 and 8 bytes
                              @pLong[2] = @pLong[2] Xor &h6DBEE6DC    'the password bytes are combined into blocks of 4
                              
                              'now do the remainder of the string
                              pLong = pLong+12   'point to the next (24th) byte in the string
                              
                              
                              For i = 0 To Len(String1)\35   '\35 because I'm doing 35 bytes in each pass
                                  @pLong    = @pLong    Xor &h7F13DD80
                                  @pLong[1] = @pLong[1] Xor &hC280DA8C
                                  @pLong[2] = @pLong[2] Xor &h21EA51A7
                                  @pLong[3] = @pLong[3] Xor &hF0EAA186
                                  @pLong[4] = @pLong[4] Xor &h1DFCB11D
                                  @pLong[5] = @pLong[5] Xor &h9CDC55A1
                                  @pLong[6] = @pLong[6] Xor &h8B804947
                                  @pLong[7] = @pLong[7] Xor &hFD6560BF
                                  @pLong[8] = @pLong[8] Xor &h0047FDDA      'note 36th byte is zero so it'll not affect anything when XORed
                                
                                  pLong=pLong+35                             'point to next block of 35
                              Next
                            
                            Function = Left$(String1,Len(String1) - 36)    'remove the extra 36 bytes I added at the start and return the answer  
                          End Function
                           
                          Function PBMain
                              Local fl, s As String                        
                               'Send file to be encrypted
                              fl$ = CurDir$ & "\Cryption_Test_Data.txt"'TxtFileName  to encrypt
                              s$ = Encrypt(fl$)    
                              
                               'Save the encrypted file
                              fl$ = CurDir$ & "\Cryption_Test_Crypted.txt"'Store result
                              PutFile(fl$, s$)                 
                          '    Incr Stp 
                          '    MsgBox Str$(Len(s$))  & $CrLf & s$, , Using$("Step # Data Returned from EnCryption technique in ", stp) & FuncName$
                              
                               'Now Decrypt the saved file
                              fl$ = CurDir$ & "\Cryption_Test_Crypted.txt"'TxtFileName to Decrypt
                              s$ = Encrypt(fl$)                    
                           
                              Incr stp
                              MsgBox Str$(Len(s$))  & $CrLf & s$, , Using$("Step # Returned from DeCryption technique in ", stp) & FuncName$
                              
                           
                              ? "All Done"
                              
                               '<<< GPF's here
                              
                          End Function
                          '
                          =========================================
                          "We are not retreating
                          we are advancing in another Direction."
                          General Douglas MacArthur (1880-1964)
                          =========================================
                          It's a pretty day. I hope you enjoy it.

                          Gösta

                          JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                          LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                          Comment

                          Working...
                          X