Guidance
指路人
g.yi.org
software / rapidq / Examples / ASM / QRC4Asm / QRC4Asm_Type_1b.Inc

Register 
新用户注册
Search 搜索
首页 
Home Home
Software
Upload

  
'
' ------------------------------------------------------------
' RC4 - CRYPTING AND DECRYPTING - RAPIDQ  ASSEMBLER  FUNCTIONS
' Version 002       Augut 13th, 2004       by Jacques PHILIPPE
' Speed : ~20  MegaBytes  encrypted/second  on  a  DURON  1200
' Type_1b : NO EXTRA FILES REQUIRED (my choice)
' ------------------------------------------------------------
' ============================================================
' ASM PROCEDURES : RC4BinStr, RC4Init, RC4Pipe
' ============================================================
     $IFNDEF CALL_ASM_PROC_X
      $DEFINE CALL_ASM_PROC_X
      DECLARE FUNCTION CallAsmProc LIB "user32" ALIAS "CallWindowProcA" _
       (Proc AS LONG, A1 AS LONG, A2 AS LONG, A3 AS LONG, A4 AS LONG) AS LONG
     $ENDIF
' ============================================================
' ----- START ASM RC4BinStr -----
     DEFBYTE RC4BinStrArray (0 TO 310) = _
      { _
      &HC8, &H00, &H00, &H00, &H81, &HEC, &H2C, &H01, &H00, &H00,  _
      &H89, &HE8, &H2D, &H2C, &H01, &H00, &H00, &H89, &H45, &HD8,  _
      &H53, &H51, &H52, &H56, &H57, &H8B, &H75, &H08, &H81, &HEE,  _
      &H04, &H00, &H00, &H00, &H8B, &H06, &H89, &H45, &HDC, &H8B,  _
      &H75, &H0C, &H81, &HEE, &H04, &H00, &H00, &H00, &H8B, &H06,  _
      &H89, &H45, &HE0, &H81, &H7D, &H0C, &H00, &H00, &H00, &H00,  _
      &H75, &H0A, &HB8, &H01, &H00, &H00, &H00, &HE9, &HE6, &H00,  _
      &H00, &H00, &H81, &H7D, &HE0, &H00, &H00, &H00, &H00, &H75,  _
      &H0A, &HB8, &H02, &H00, &H00, &H00, &HE9, &HD3, &H00, &H00,  _
      &H00, &H81, &H7D, &H08, &H00, &H00, &H00, &H00, &H75, &H0A,  _
      &HB8, &H03, &H00, &H00, &H00, &HE9, &HC0, &H00, &H00, &H00,  _
      &H81, &H7D, &HDC, &H00, &H00, &H00, &H00, &H75, &H0A, &HB8,  _
      &H04, &H00, &H00, &H00, &HE9, &HAD, &H00, &H00, &H00, &HE8,  _
      &H6A, &H00, &H00, &H00, &HB8, &H00, &H00, &H00, &H00, &HBB,  _
      &H00, &H00, &H00, &H00, &H8B, &H75, &HD8, &H8B, &H7D, &H08,  _
      &HB9, &H00, &H00, &H00, &H00, &HBA, &H00, &H00, &H00, &H00,  _
      &HFE, &HC3, &H02, &H04, &H33, &H8A, &H14, &H33, &H86, &H14,  _
      &H30, &H86, &H14, &H33, &H8A, &H14, &H33, &H02, &H14, &H30,  _
      &H50, &HB8, &H00, &H00, &H00, &H00, &H25, &HFF, &H00, &H00,  _
      &H00, &H8A, &H04, &H32, &H30, &H04, &H39, &H58, &H41, &H3B,  _
      &H4D, &HDC, &H75, &HD4, &HB8, &H00, &H00, &H00, &H00, &HE9,  _
      &H58, &H00, &H00, &H00, &HB8, &H00, &H00, &H00, &H00, &H8B,  _
      &H7D, &HD8, &H88, &H07, &H47, &HFE, &HC0, &H75, &HF9, &HB8,  _
      &H00, &H00, &H00, &H00, &HB8, &H00, &H00, &H00, &H00, &HC3,  _
      &HE8, &HE1, &HFF, &HFF, &HFF, &HB8, &H00, &H00, &H00, &H00,  _
      &HBB, &H00, &H00, &H00, &H00, &HB9, &H00, &H00, &H00, &H00,  _
      &H8B, &H75, &HD8, &H8B, &H7D, &H0C, &HBA, &H00, &H00, &H00,  _
      &H00, &H02, &H04, &H1E, &H39, &H4D, &HE0, &H75, &H05, &HB9,  _
      &H00, &H00, &H00, &H00, &H02, &H04, &H0F, &H8A, &H14, &H30,  _
      &H86, &H14, &H33, &H86, &H14, &H30, &H41, &HFE, &HC3, &H75,  _
      &HE2, &HC3, &H5F, &H5E, &H5A, &H59, &H5B, &HC9, &HC2, &H10,  _
      &H00 _
      }
' ----- END ASM RC4BinStr -----
'
' ----- POINTER to use In CallAsmProc -----
'       A Bit Faster than Calling RC4BinStr
     DEFINT ptrRC4BinStr
     ptrRC4BinStr = VARPTR (RC4BinStrArray(0))
'
' ----- RQ CALL RC4BinStr -----
     FUNCTION RC4BinStr (sDatas AS STRING, sKEY AS STRING) AS LONG
      DEFSTR ssDatas = sDatas
      DEFSTR ssKEY = sKEY
      Result = CallAsmProc (ptrRC4BinStr, VARPTR(ssDatas), VARPTR(ssKEY), 0, 0)
     END FUNCTION
' ============================================================
' ----- START ASM RC4Init -----
     DEFBYTE RC4InitArray (0 TO 187) = _
      { _
      &HC8, &H00, &H00, &H00, &H81, &HEC, &H28, &H00, &H00, &H00,  _
      &H53, &H51, &H52, &H56, &H57, &H8B, &H75, &H0C, &H81, &HEE,  _
      &H04, &H00, &H00, &H00, &H8B, &H06, &H89, &H45, &HD8, &H81,  _
      &H7D, &H08, &H00, &H00, &H00, &H00, &H75, &H0A, &HB8, &H05,  _
      &H00, &H00, &H00, &HE9, &H83, &H00, &H00, &H00, &H81, &H7D,  _
      &H0C, &H00, &H00, &H00, &H00, &H75, &H0A, &HB8, &H01, &H00,  _
      &H00, &H00, &HE9, &H70, &H00, &H00, &H00, &H81, &H7D, &HD8,  _
      &H00, &H00, &H00, &H00, &H75, &H0A, &HB8, &H02, &H00, &H00,  _
      &H00, &HE9, &H5D, &H00, &H00, &H00, &HB8, &H00, &H00, &H00,  _
      &H00, &H8B, &H7D, &H08, &H88, &H07, &H47, &HFE, &HC0, &H75,  _
      &HF9, &HB8, &H00, &H00, &H00, &H00, &HBB, &H00, &H00, &H00,  _
      &H00, &HB9, &H00, &H00, &H00, &H00, &H8B, &H75, &H08, &H8B,  _
      &H7D, &H0C, &HBA, &H00, &H00, &H00, &H00, &H02, &H04, &H1E,  _
      &H39, &H4D, &HD8, &H75, &H05, &HB9, &H00, &H00, &H00, &H00,  _
      &H02, &H04, &H0F, &H8A, &H14, &H30, &H86, &H14, &H33, &H86,  _
      &H14, &H30, &H41, &HFE, &HC3, &H75, &HE2, &H8B, &H7D, &H08,  _
      &HC6, &H87, &H00, &H01, &H00, &H00, &H00, &HC6, &H87, &H01,  _
      &H01, &H00, &H00, &H00, &HB8, &H00, &H00, &H00, &H00, &H5F,  _
      &H5E, &H5A, &H59, &H5B, &HC9, &HC2, &H10, &H00 _
      }
' ----- END ASM RC4Init -----
'
' ----- POINTER to use In CallAsmProc -----
'       A Bit Faster than Calling RC4Init
     DEFINT ptrRC4Init
     ptrRC4Init = VARPTR (RC4InitArray(0))
'
' ----- RQ CALL RC4Init -----
     FUNCTION RC4Init (sTable AS STRING, sKEY AS STRING) AS LONG
      DEFSTR ssTable = sTable
      DEFSTR ssKEY = sKEY
      Result = CallAsmProc (ptrRC4Init, VARPTR(ssTable), VARPTR(ssKEY), 0, 0)
     END FUNCTION
' ============================================================
' ----- START ASM RC4Pipe -----
     DEFBYTE RC4PipeArray (0 TO 195) = _
      { _
      &HC8, &H00, &H00, &H00, &H81, &HEC, &H28, &H00, &H00, &H00,  _
      &H53, &H51, &H52, &H56, &H57, &H8B, &H75, &H0C, &H81, &HEE,  _
      &H04, &H00, &H00, &H00, &H8B, &H06, &H89, &H45, &HD8, &H81,  _
      &H7D, &H0C, &H00, &H00, &H00, &H00, &H75, &H0A, &HB8, &H03,  _
      &H00, &H00, &H00, &HE9, &H8B, &H00, &H00, &H00, &H81, &H7D,  _
      &HD8, &H00, &H00, &H00, &H00, &H75, &H0A, &HB8, &H04, &H00,  _
      &H00, &H00, &HE9, &H78, &H00, &H00, &H00, &H81, &H7D, &H08,  _
      &H00, &H00, &H00, &H00, &H75, &H0A, &HB8, &H05, &H00, &H00,  _
      &H00, &HE9, &H65, &H00, &H00, &H00, &H8B, &H75, &H08, &H8A,  _
      &H9E, &H00, &H01, &H00, &H00, &H81, &HE3, &HFF, &H00, &H00,  _
      &H00, &H8A, &H86, &H01, &H01, &H00, &H00, &H25, &HFF, &H00,  _
      &H00, &H00, &H8B, &H7D, &H0C, &HB9, &H00, &H00, &H00, &H00,  _
      &HBA, &H00, &H00, &H00, &H00, &HFE, &HC3, &H02, &H04, &H33,  _
      &H8A, &H14, &H33, &H86, &H14, &H30, &H86, &H14, &H33, &H8A,  _
      &H14, &H33, &H02, &H14, &H30, &H50, &HB8, &H00, &H00, &H00,  _
      &H00, &H81, &HE2, &HFF, &H00, &H00, &H00, &H8A, &H04, &H32,  _
      &H30, &H04, &H39, &H58, &H41, &H3B, &H4D, &HD8, &H75, &HD3,  _
      &H88, &H9E, &H00, &H01, &H00, &H00, &H88, &H86, &H01, &H01,  _
      &H00, &H00, &HB8, &H00, &H00, &H00, &H00, &H5F, &H5E, &H5A,  _
      &H59, &H5B, &HC9, &HC2, &H10, &H00 _
      }
' ----- END ASM RC4Pipe -----
'
' ----- POINTER to use In CallAsmProc -----
'       A Bit Faster than Calling RC4Pipe
     DEFINT ptrRC4Pipe
     ptrRC4Pipe = VARPTR (RC4PipeArray(0))
'
' ----- RQ CALL RC4Pipe -----
     FUNCTION RC4Pipe (sTable AS STRING, sDatas AS STRING) AS LONG
      DEFSTR ssTable = sTable
      DEFSTR ssDatas = sDatas
      Result = CallAsmProc (ptrRC4Pipe, VARPTR(ssTable), VARPTR(ssDatas), 0, 0)
     END FUNCTION
' ============================================================
' --- RQ CODE ------------------------------------------------
' ============================================================
     $TYPECHECK ON
' ============================================================
' -- CONSTANTES RETURNED ERROR --
' ----------------------------
     CONST RC4ASM_FINISHED_OK = 0
     CONST RC4ASM_ERROR_PTR_KEY = 1
     CONST RC4ASM_ERROR_LEN_KEY = 2
     CONST RC4ASM_ERROR_PTR_DATAS = 3
     CONST RC4ASM_ERROR_LEN_DATAS = 4
     CONST RC4ASM_ERROR_PTR_STABLE = 5
     CONST RC4ASM_ERROR_FILE_IN_CANT_BE_OPENED = 6
     CONST RC4ASM_ERROR_CANT_CREATE_FILE_OUT = 7
     CONST RC4ASM_ERROR_IBITSIZE_IN_RC4FILE_PIPE = 8
     CONST RC4ASM_ERROR_UNEXPECTED = 255
' ==== GetRC4AsmErrorByName===================================
'  Returns the Name on an Rc4Asm Error 0 to 7
     FUNCTION GetRC4AsmErrorByName (iError AS LONG) AS STRING
      SELECT CASE iError
      CASE RC4ASM_FINISHED_OK                     ' = 0
       Result = " *** RC4ASM FINISHED_OK *** "
      CASE RC4ASM_ERROR_PTR_KEY                   ' = 1
       Result = " *** RC4ASM ERROR_PTR_KEY *** "
      CASE RC4ASM_ERROR_LEN_KEY                   ' = 2
       Result = " *** RC4ASM ERROR_LEN_KEY *** "
      CASE RC4ASM_ERROR_PTR_DATAS                 ' = 3
       Result = " *** RC4ASM ERROR_PTR_DATAS *** "
      CASE RC4ASM_ERROR_LEN_DATAS                 ' = 4
       Result = " *** RC4ASM ERROR_LEN_DATAS *** "
      CASE RC4ASM_ERROR_PTR_STABLE                ' = 5
       Result = " *** RC4ASM ERROR_PTR_STABLE *** "
      CASE RC4ASM_ERROR_FILE_IN_CANT_BE_OPENED    ' = 6
       Result = " *** RC4ASM ERROR FILE IN CANT BE OPENED ***"
      CASE RC4ASM_ERROR_CANT_CREATE_FILE_OUT      ' = 7
       Result = " *** RC4ASM ERROR CANT CREATE FILE OUT ***"
      CASE RC4ASM_ERROR_IBITSIZE_IN_RC4FILE_PIPE  ' = 8
       Result = " *** RC4ASM ERROR IBITSIZE_IN_RC4FILE_PIPE ***"
      CASE ELSE
       Result = " *** RC4ASM ERRONEOUS ERROR NUMBER :) " & STR$(iError) & " ***"
      END SELECT
     END FUNCTION
' ============================================================
' ==== RC4File ===============================================
' ENCRYPT/DECRYPT sPathFileNameIn in sPathFilNameOut With KEY by splitting
' the file in multiple bits of the same size -except the last bit-. The lenght
' of the bits to RC4 is set iBitsSize
'
     FUNCTION RC4File (PathFileNameIn AS STRING, PathFileNameOut AS STRING, Key AS STRING, iBitsSize AS LONG) AS LONG
      DEFSTR sPathFileNameIn = PathFileNameIn, sPathFileNameOut = PathFileNameOut
      DIM file2Rc4 AS QFILESTREAM
      DIM fileRC4Out AS QFILESTREAM
      DIM sTable AS STRING * 258
      DEFINT iReturn
      DEFINT iFileLengthLeft
      DEFSTR sBits, sTmp
      DEFSTR sKey = KEY
'
      IF iBitsSize < 1 THEN Result = RC4ASM_ERROR_IBITSIZE_IN_RC4FILE_PIPE:EXIT FUNCTION ' = 8
'
      IF file2RC4.OPEN (sPathFileNameIn, 0) <> 0 THEN                ' 0 = fmOpenRead
       IF fileRC4Out.OPEN (sPathFileNameOut, 65535) <> 0 THEN   ' 65535 = fmCreate
        iFileLengthLeft = file2RC4.Size
        file2RC4.Position = 0
            ' Initialise the sTable with the Key
        iReturn = RC4Init (sTable, sKey)
        DO
         IF iReturn = 0 THEN     ' Init OK
          IF iFileLengthLeft >= iBitsSize THEN
           sBits = file2RC4.ReadBinStr (iBitsSize)
           iReturn = RC4Pipe (sTable, sBits)
           IF iReturn <> 0 THEN Result = iReturn:EXIT DO
           fileRC4Out.WriteBinStr (sBits, iBitsSize)
           iFileLengthLeft = iFileLengthLeft - iBitsSize
          ELSE
           sBits = file2RC4.ReadBinStr (iFileLengthLeft)
           iReturn = RC4Pipe (sTable, sBits)
           IF iReturn <> 0 THEN Result = iReturn:EXIT DO
           fileRC4Out.WriteBinStr (sBits, iFileLengthLeft)
           iFileLengthLeft = 0
          END IF
         ELSE                    ' Init Failed
          Result = iReturn
          EXIT DO
         END IF

        LOOP UNTIL iFileLengthLeft = 0
        fileRC4Out.CLOSE
       ELSE
        Result = RC4ASM_ERROR_CANT_CREATE_FILE_OUT  ' = 7
       END IF
       file2RC4.CLOSE
      ELSE
       Result = RC4ASM_ERROR_FILE_IN_CANT_BE_OPENED    ' = 6
       EXIT FUNCTION
      END IF
      IF iFileLengthLeft <= 0 THEN Result = 0
     END FUNCTION
' ============================================================
     $TYPECHECK OFF
'
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2021-10-21  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2004-08-14 18:00:02