$option explicit            'get rapidq to check all variables
$include "rapidq.inc"

'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
form.center
form.showmodal

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
        file2.writeline(plainline$)
      else
        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
    wend
    file1.close
    file2.close
    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
