Announcement

Collapse
No announcement yet.

RC4 Encryption issue

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

  • RC4 Encryption issue

    OK Perhaps I do not understand - some passwords work and anything over 16 to 20 characters does not get DECRYPTED.
    I think the issue may be in my conversion to HEx Characters.
    Sometimes encrypted characters will have a CR in them - when writing to a file it causes issues, so I convert to hex so it's all numbers/letters.
    Perhaps a base64 routine would be better?


    This is Scott Slaters RC4 and it appears to work FINE if I remove my code.........hmmmmm (Thinking aloud)

    It appears there is a length limit to the encryption.
    I wrote my own RC4 years ago and it works but this looks faster.
    Code:
    #Compile Exe
    #Dim All
    
    
    Declare Function ConvertToBinaryString(charSt As String)As String
    Declare Function ConvertToHexString(charSt As String)As String
    Declare Function CryptStr(StringIn As String, Pw As String) As String
    Declare Function EnDeCrypt(InString As Asciiz, Passwd As Asciiz, OutString As Asciiz, ByVal nLen As Dword) As Long
    
    
    Function PBMain () As Long
    Local sTmp As String
    Local wKey As String
    
    
    sTmp = "This is my phrase to encrypt/decrypt"
    wKey = "524E365B5A334C2F582D2A295E4F5249"
    
    sTmp = CryptStr(sTmp,wKey)
    sTmp = ConvertToHexString(sTmp) 'Done because it's easier to handle numbers than weird characters
    MsgBox sTmp 'Encrypted
    
    sTmp = ConvertToBinaryString(sTmp)
    sTmp = CryptStr(sTmp,wKey)
    MsgBox sTmp 'Supposedly decrypted but only halfway
    
    End Function
    
    '=========================================================================================================
    Function ConvertToHexString(charSt As String)Export As String
    Local x As Long
    Local y As Long
    Local St As String
    y = Len(charSt)
    For x = 1 To y
        St = St + Hex$(Asc(Mid$(charSt,x,1)))
    Next
    Function = St
    End Function
    '=========================================================================================================
    Function ConvertToBinaryString(charSt As String) As String
    Local y As Long
    Local x As Long
    Local PosPtr As Long
    Local St As String
    Local NewKeySt As String
    
    y = Len(charSt)
    For x = 1 To y Step 2
        St = Mid$(charSt,x,2)
        NewKeySt = NewKeySt + Chr$(Val("&h" + St))
    Next
    Function = NewKeySt
    End Function
    '=========================================================================================================
    ' ---------------------------------------------------------
    ' RC4 Encrypt/Decrypt Routine: Returns 1 on Error,
    '                                      0 for Success
    '
    '  nLen = Input String Len. NOTE: Buffer Lengths Must Be
    '  equal to or greater than this size.  We want To be able
    '  to encrypt Ascii Zero's so we can't look for These for
    '  the end of the string mark as you normally would with
    '  this type of data.
    '
    ' NOTE: If you are calling from PB you are better off using
    ' the wrapper function below to use BASIC Strings.
    ' ---------------------------------------------------------
    
    Function EnDeCrypt(InString As Asciiz, Passwd As Asciiz, OutString As Asciiz, ByVal nLen As Dword) As Long
    
       Local S   As Asciiz * 256
       Local K   As Asciiz * 256
       Local rtn As Long
    
       ! push ebx                 ; save ebx
       ! push esi                 ; save esi
       ! push edi                 ; save edi
       ! mov esi, InString        ; Input String Address
       ! and esi, esi             ; null pointer check
       ! jz EnDeCryptDone         ; exit function
       ! mov edi, OutString       ; Output String Address
       ! and edi, edi             ; null pointer check
       ! jz EnDeCryptDone         ; exit function
       ! mov edx, Passwd          ; Password String Address
       ! and edx, edx             ; null pointer check
       ! jz EnDeCryptDone         ; exit function
    
       ! push esi                 ; save it for temp use
       ! push edi                 ; save it for temp use
       ! push edx                 ; save original pw address
       ! xor eax, eax             ; zero eax
       ! lea esi, S               ; address of S
       ! lea edi, K               ; address of K
    
       EnDeCryptInitS:
       ! mov [esi+eax], al        ; store char in S
       ! mov bl, [edx]            ; get byte from password
       ! cmp bl, 0                ; see if its a null terminator
       ! je EnDeCryptFixPw        ; if so adjust it
       ! mov [edi+eax], bl        ; store char in K
       ! inc edx                  ; increment pw pointer
       ! inc eax                  ; increment ascii pointer
       ! cmp eax, 256             ; see if we're done
       ! jl EnDeCryptInitS        ; get next character
       ! jmp EnDeCryptCreateTable ; go to next step
    
       EnDeCryptFixPw:
       ! pop edx                  ; get starting address of pw
       ! push edx                 ; save it again
       ! mov bl, [edx]            ; get first char again
       ! mov [edi+eax], bl        ; store it in K
       ! inc edx                  ; increment pw pointer
       ! inc eax                  ; increment ascii pointer
       ! cmp eax, 256             ; see if we're done
       ! jl EnDeCryptInitS        ; get next character
    
       EnDeCryptCreateTable:
       ! xor ecx, ecx             ; zero ecx
       ! xor edx, edx             ; zero edx
    
       EnDeCryptTableLoop:
       ! mov al, [esi+ecx]        ; get S char into al
       ! mov ah, [edi+ecx]        ; get K char into ah
       ! add al, ah               ; add them together
       ! mov ah, dl               ; dl to ah
       ! add al, ah               ; add them together
       ! mov edx, eax             ; save it to edx
       ! and edx, 255             ; filter off high bits
       ! mov bl, [esi+ecx]        ; get S char to bl
       ! mov al, [esi+edx]        ; get K char to al
       ! mov [esi+ecx], al        ; swap the values
       ! mov [esi+edx], bl        ; swap the values
       ! inc ecx                  ; increment counter
       ! cmp ecx, 256             ; see if we're done
       ! jl EnDeCryptTableLoop    ; get next character
    
       ! pop edx                  ; restore password address
       ! pop edi                  ; restore Output address
       ! pop esi                  ; restore Input address
       ! xor eax, eax             ; zero eax
       ! xor ebx, ebx             ; zero ebx
       ! xor ecx, ecx             ; zero ecx
       ! xor edx, edx             ; zero edx
    
       EnDeCryptMainLoop:
       ! mov eax, 1234            ; normal exit code checked on exit
       ! cmp edx, nLen            ; See if we've reached the end yet
       ! jge EnDeCryptDone        ; if so we're done
       ! mov al, [esi+edx]        ; get char from input
       ! inc ch                   ; increment ch
       ! and ch, 255              ; range 0-255
       ! push esi                 ; save esi
       ! lea esi, S               ; address of S
       ! push eax                 ; save eax
       ! push ebx                 ; save ebx
       ! xor eax, eax             ; zero eax
       ! mov ah, ch               ; get ch
       ! shr eax, 8               ; move to al
       ! mov ebx, eax             ; copy it to ebx
       ! mov al, [esi+ebx]        ; get byte
       ! add cl, al               ; add them up
       ! and cl, 255              ; range 0-255
       ! pop ebx                  ; restore ebx
       ! xor eax, eax             ; zero eax
       ! mov al, cl               ; cl to al
       ! mov bl, [esi+eax]        ; swap table bytes
       ! mov al, ch               ; ch to al
       ! mov bh, [esi+eax]        ; swap table bytes
       ! mov [esi+eax], bl        ; swap table bytes
       ! mov al, cl               ; cl to al
       ! mov [esi+eax], bh        ; swap table bytes
       ! pop eax                  ; restore eax
       ! add bl, bh               ; add the bytes together
       ! and ebx, 255             ; range 0-255
       ! mov bh, [esi+ebx]        ; get xor Var
       ! shr ebx, 8               ; move it to bl
       ! xor al, bl               ; xor the value
       ! mov [edi+edx], al        ; store the encrypted char
       ! pop esi                  ; restore input address
       ! inc edx                  ; increment character counter
       ! jmp EnDeCryptMainLoop    ; get next character
    
       EnDeCryptDone:
       ! cmp eax, 1234            ; see if our normal exit code
       ! je EnDeCryptExitLab      ; got us here, if so exit
       ! mov eax, 1               ; if not return error code
       ! mov rtn, eax             ; to caller
    
       EnDeCryptExitLab:
       ! pop edi                  ; restore edi
       ! pop esi                  ; restore esi
       ! pop ebx                  ; restore ebx
    
       Function = rtn
    
    End Function
    
    
    ' ---------------------------------------------------------
    ' Wrapper Function that Calls EnDeCrypt and handles
    ' Conversions of Input And output to regular basic
    ' style strings instead of Asciiz.
    '
    '                     Returns:  String on Success
    '                               Empty String on Error
    ' ---------------------------------------------------------
    Function CryptStr(StringIn As String, Pw As String) Export As String
    
       Local pBuff As String
       Local oBuff As String
    
       pBuff = Pw & Chr$(0)              ' pad pw with a chr 0
       oBuff  = Space$(Len(StringIn))    ' set outbuff len
    
       ! push ebx                ; save ebx
       ! mov eax, StringIn       ; Get Input Buffer Handle to eax
       ! mov eax, [eax]          ; get address of data
       ! and eax, eax            ; null pointer check
       ! jz CryptStrDone         ; leave the function
    
       ! lea ebx, pBuff          ; pBuff's handle to ebx
       ! mov ebx, [ebx]          ; pBuff's address to ebx
       ! lea ecx, oBuff          ; oBuff's handle to ecx
       ! mov ecx, [ecx]          ; oBuff's address to ecx
       ! mov edx, [eax-4]        ; StringIn's Length to edx
    
       ! push edx                ; last parameter for call
       ! push ecx                ; 3rd parameter for call
       ! push ebx                ; 2nd parameter for call
       ! push eax                ; 1st paramter for call
       ! call EnDeCrypt          ; call the master function
    
       ! cmp eax, 0              ; make sure there were no errors
       ! jne CryptStrDone        ; if so then exit
    
       Function = oBuff          ' set output
    
       CryptStrDone:
       ! pop ebx                 ; restore ebx
    
    End Function
    '=========================================================================================================
    Last edited by Scott Turchin; 12 Jul 2008, 02:27 PM.
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

  • #2
    Once again I answered my own question.
    Seems PB has a cool new feature for Hex$ called "digits", make it 2 digits and voila that code works.....writes cleanly to an INI file etc..
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

    Comment


    • #3
      Use Write/GetPrivateProfileStruct if you need to access binary data in an INI file, it performs the hex conversion automatically and also uses a checksum to ensure the data is intact. If the data is var-length then a separate size key would also have to be kept
      kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

      Comment


      • #4
        I wasn't aware of that, thanks, that's the only reason I do my convert!!
        Scott Turchin
        MCSE, MCP+I
        http://www.tngbbs.com
        ----------------------
        True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

        Comment

        Working...
        X