software / rapidq / Examples / Security / encrypt / encrypt.rqb

Search 搜索
Home Home

     $OPTION EXPLICIT            'get rapidq to check all variables
     $INCLUDE ""

'program takes any old file and encrypts it, except: first line outputted as is, any line
'starting with ' is ignored

     DECLARE SUB bc 'button click

     DIM form AS QFORM
     DIM button AS QBUTTON
     button.PARENT = form
     button.CAPTION  = "encrypt a file"
     button.onclick = bc

     SUB bc 'main button clicked
      DIM opendialog AS QOPENDIALOG   'open file dialog
      DIM file1 AS QFILESTREAM        'input file
      DIM file2 AS QFILESTREAM        'output file
      DIM linesread AS INTEGER        'lines read from a file
      DIM cwl AS INTEGER              'which letter (1st, 2nd etc) we are using from the code word
      DIM templine$ AS STRING         'a temporary string
      DIM b AS INTEGER                'counter
      DIM outfname$ AS STRING         'name for output file
      DIM plainline$ AS STRING        'each plain line read from file
      DIM cryptchar$ AS STRING        'an encrypted character
      DIM textchar$ AS STRING         'a character which has not been encrypted - from plain text
      DIM codechar$ AS STRING         'character from codeword to use in encryption
      DIM codeword$ AS STRING         'actual codeword
      DIM cryptline$ AS STRING        'encrypted line
      codeword$ = "tigerlily"
      IF opendialog.EXECUTE THEN
       file1.OPEN(opendialog.filename, fmopenread)
       outfname$ = opendialog.filename                          'same as filename of file to encode
       outfname$ = LEFT$(outfname$, INSTR(outfname$, ".")-1)    'strip off everything after "."
       outfname$ = outfname$ + ".cde"                           'add ".cde"
       file2.OPEN(outfname$, fmcreate)
       linesread  = 0                                           'number of lines read from file
       WHILE NOT file1.eof
        plainline$ = file1.readline()                          'a plain line read from file
        linesread = linesread + 1
        IF linesread = 1 THEN                                  'jsut copy first line to output
         IF LEFT$(plainline$, 1)="'" THEN                     'do nothing if a line starts with '
          'do nothing
        ELSE 'a line to encrypt
         templine$=plainline$+"#"                           'add a "#" to the line
         FOR b = INT(RND*6) TO 6+INT(RND*20)                'add some random characters to end of line
          templine$=templine$+CHR$(48+INT(RND*74))         'these are random letters - look up an ascii table
         NEXT b
         templine$ = reverse$(templine$)                    'reverse templine$
         cryptline$ = ""
         FOR b = 1 TO LEN(templine$)                        'go along each letter in templine
          cwl = b MOD LEN(codeword$)                       'work out which letter of code word to use
          IF cwl = 0 THEN
           cwl = LEN(codeword$)
          END IF
          codechar$ = MID$(codeword$, cwl, 1)              'find the letter of codeword we will be using
          textchar$ = MID$(templine$, b, 1)                'the plain test letter about to encode
          cryptchar$ = CHR$(40 + (ASC(textchar$) - 40) + (ASC(codechar$) - 40))
                                                             'basically add the two letters toegether...
                                                             'but subtract 40 - cos first 40 bytes in ascii are
                                                             'not needed... (this is tricky!)
          cryptline$ = cryptline$ + cryptchar$             'add each encrypted letter to cryptline$
         NEXT b
         file2.writeline (cryptline$)
        END IF
       END IF
      SHOWMESSAGE outfname$ +" created"
     END IF
     END SUB

'encrypt system:
'read each line
'don't encrypt first line at all: this will be description of this file - just write to .cde file
'if a line starts with ' ignore it
'add # to end of each line
'add 5 to 20 random textish characters
'reverse the line
'use a code word over and over again to create encrypted line
'write encrypted line to .cde file

'decrypt system
'read each line in turn
'don't decrypt first line
'unencrypt line character by character using code word
'reverse the line
'find the # character
'delete it and everything to the right of it
'put each line in an array: fileline(1), fileline(2) etc
'can now use this array of lines as needed
© Sat 2023-2-4  Guidance Laboratory Inc. Hits:0 Last modified:2004-11-24 23:44:00