home *** CD-ROM | disk | FTP | other *** search
/ Multimedia & CD-ROM 3 / mmcd03-jun1995-cd.iso / utils / various / utils-1 / encrypt.asm < prev    next >
Assembly Source File  |  1991-06-24  |  13KB  |  270 lines

  1. ;****************************************************************************
  2. ; ENCRYPT encrypts a data file using a variation on the Vernam cipher
  3. ; technique, which XORs each character of a message with each character
  4. ; of a password.  Its syntax is:
  5. ;
  6. ;       ENCRYPT [d:][path]filename "password"
  7. ;
  8. ; where "file1" is the name of the file to be encrypted and "password" is
  9. ; the password used to encrypt the file.  The password must be enclosed in
  10. ; quotation marks and may be as long as the command line permits.  
  11. ;
  12. ; Files are unencrypted by passing them through ENCRYPT again with the
  13. ; same password.  If a password is forgotten, there is no way to recover
  14. ; the original file short of manually breaking the code.
  15. ;****************************************************************************
  16.  
  17. code            segment
  18.                 assume  cs:code,ds:code
  19.                 org     100h
  20. begin:          jmp     main
  21.  
  22. helpmsg         db      "Encrypts a file for data security.",13,10,13,10
  23.                 db      "ENCRYPT [d:][path]filename ",22h,"password",22h
  24.                 db      13,10,13,10
  25.                 db      "  filename  Name of the file to be encrypted",13,10
  26.                 db      "  password  Password used for encryption",13,10
  27.                 db      13,10
  28.                 db      "To unencrypt a file, run it through ENCRYPT again "
  29.                 db      "with the same password.",13,10,"$"
  30.  
  31. errmsg1         db      "Syntax: ENCRYPT [d:][path]filename "
  32.                 db      22h,"password",22h,13,10,"$"
  33. errmsg2         db      "File not found or path invalid",13,10,"$"
  34. errmsg3         db      "File could not be opened for reading",13,10,"$"
  35. errmsg4         db      "File could not be opened for writing",13,10,"$"
  36. errmsg5         db      "File read error",13,10,"$"
  37. errmsg6         db      "File write error",13,10,"$"
  38. errmsg7         db      "Disk full error",13,10,"$"
  39. errmsg8         db      "Not enough memory",13,10,"$"
  40.  
  41. filename        dw      ?                       ;Address of file name
  42. r_handle        dw      ?                       ;File handle for reading
  43. w_handle        dw      ?                       ;File handle for writing
  44. password        dw      ?                       ;Password address
  45. count           dw      ?                       ;Password length
  46. blocksize       dw      ?                       ;Block size for file I/O
  47. bytesread       dw      ?                       ;Bytes read from input file
  48.  
  49. ;****************************************************************************
  50. ; Procedure MAIN
  51. ;****************************************************************************
  52.  
  53. main            proc    near
  54.                 cld                             ;Clear direction flag
  55.                 mov     si,81h                  ;Point SI to command line
  56.                 call    scanhelp                ;Scan for "/?" switch
  57.                 jnc     checkmem                ;Branch if not found
  58.                 mov     ah,09h                  ;Display help text and exit
  59.                 mov     dx,offset helpmsg       ;  with ERRORLEVEL=0
  60.                 int     21h
  61.                 mov     ax,4C00h
  62.                 int     21h
  63.  
  64. checkmem:       mov     dx,offset errmsg8       ;Make sure there's enough
  65.                 cmp     sp,0A000h               ;  memory to run the
  66.                 ja      parse                   ;  program
  67.  
  68. error:          mov     ah,09h                  ;Display error message and
  69.                 int     21h                     ;  exit with ERRORLEVEL=1
  70.                 mov     ax,4C01h
  71.                 int     21h
  72. ;
  73. ; Parse the command line.
  74. ;
  75. parse:          call    findchar                ;Find first parameter
  76.                 jnc     parse2                  ;Branch if found
  77. error1:         mov     dx,offset errmsg1       ;Error if not
  78.                 jmp     error                   
  79.  
  80. parse2:         cmp     byte ptr [si],22h       ;Error if quotation mark
  81.                 je      error1
  82.                 mov     filename,si             ;Save address
  83.                 call    finddelim               ;Find end of string
  84.                 jc      error1                  ;Error if end of line
  85.                 mov     byte ptr [si],0         ;Convert to ASCIIZ
  86.                 inc     si                      ;Advance SI
  87.  
  88.                 call    findchar                ;Find next parameter
  89.                 jc      error1                  ;Error if end of line
  90.                 cmp     byte ptr [si],22h       ;Error if NOT quotation
  91.                 jne     error1                  ;  mark this time
  92.                 inc     si                      ;Skip quotation mark
  93.                 mov     password,si             ;Save address
  94.                 sub     cx,cx                   ;Initialize counter
  95. readnext:       lodsb                           ;Find the closing quotation
  96.                 cmp     al,0Dh                  ;  mark and exit on error
  97.                 je      error1                  ;  if end of line is
  98.                 cmp     al,22h                  ;  encountered first
  99.                 je      quotefound
  100.                 inc     cx
  101.                 jmp     readnext
  102. quotefound:     jcxz    error1                  ;Error if length is 0
  103.                 mov     count,cx                ;Save password length
  104. ;
  105. ; Open the file with separate handles for reading and writing.
  106. ;
  107.                 mov     ax,3D00h                ;Open the file for reading
  108.                 mov     dx,filename             ;  using DOS function 3DH
  109.                 int     21h
  110.                 mov     r_handle,ax             ;Save file handle
  111.                 jnc     open                    ;Continue if no error
  112.  
  113.                 mov     dx,offset errmsg3       ;Determine what the error
  114.                 cmp     ax,2                    ;  was and point DX to the
  115.                 jb      goto_error              ;  appropriate error
  116.                 cmp     ax,3                    ;  message
  117.                 ja      goto_error
  118.                 mov     dx,offset errmsg2
  119. goto_error:     jmp     error                   ;Exit on error
  120.  
  121. open:           mov     ax,3D01h                ;Open the file for writing
  122.                 mov     dx,filename             ;  using DOS function 3Dh
  123.                 int     21h
  124.                 mov     w_handle,ax             ;Save file handle
  125.                 mov     dx,offset errmsg4       ;Branch on error
  126.                 jc      error
  127. ;
  128. ; Read the file, encrypt it, and write it back out to disk.
  129. ;
  130.                 mov     ax,8000h                ;Compute block size for
  131.                 sub     dx,dx                   ;  file I/O
  132.                 div     count                   ;Quotient in AX
  133.                 mul     count                   ;Product in AX (<8000H)
  134.                 mov     blocksize,ax            ;Save block size
  135.  
  136. readblock:      mov     ah,3Fh                  ;Read one block using
  137.                 mov     bx,r_handle             ;  DOS function 3FH
  138.                 mov     cx,blocksize
  139.                 mov     dx,offset buffer
  140.                 int     21h
  141.                 mov     dx,offset errmsg5
  142.                 jc      goto_error              ;Error if call failed
  143.                 jcxz    exit                    ;Done if zero bytes read
  144.                 mov     bytesread,ax            ;Save bytes read count
  145.  
  146.                 call    encrypt                 ;Encrypt the block
  147.  
  148.                 mov     ah,40h                  ;Write one block using
  149.                 mov     bx,w_handle             ;  DOS function 40H
  150.                 mov     cx,bytesread
  151.                 mov     dx,offset buffer
  152.                 int     21h
  153.                 mov     dx,offset errmsg6
  154.                 jc      goto_error              ;Error if call failed
  155.                 cmp     ax,bytesread            ;Proceed if bytes written
  156.                 je      check                   ;  equals bytes read
  157.                 mov     dx,offset errmsg7       ;Queue up "Disk full"
  158.                 jmp     error                   ;  message and exit
  159.  
  160. check:          mov     ax,bytesread            ;Loop back for more if
  161.                 cmp     ax,blocksize            ;  bytes read is less
  162.                 jnb     readblock               ;  than bytes requested
  163. ;
  164. ; Close file handles and exit.
  165. ;
  166. exit:           mov     ah,3Eh                  ;Close the read handle
  167.                 mov     bx,r_handle
  168.                 int     21h
  169.                 mov     ah,3Eh                  ;Close the write handle
  170.                 mov     bx,w_handle
  171.                 int     21h
  172.                 mov     ax,4C00h                ;Exit with ERRORLEVEL=0
  173.                 int     21
  174. main            endp
  175.  
  176. ;****************************************************************************
  177. ; FINDCHAR advances SI to the next non-space or non-comma character.
  178. ; On return, carry set indicates EOL was encountered.
  179. ;****************************************************************************
  180.  
  181. findchar        proc    near
  182.                 lodsb                           ;Get the next character
  183.                 cmp     al,20h                  ;Loop if space
  184.                 je      findchar
  185.                 cmp     al,2Ch                  ;Loop if comma
  186.                 je      findchar
  187.                 dec     si                      ;Point SI to the character
  188.                 cmp     al,0Dh                  ;Exit with carry set if end
  189.                 je      eol                     ;  of line is reached
  190.  
  191.                 clc                             ;Clear carry and exit
  192.                 ret
  193.  
  194. eol:            stc                             ;Set carry and exit
  195.                 ret
  196. findchar        endp
  197.  
  198. ;****************************************************************************
  199. ; FINDDELIM advances SI to the next space or comma character.  On return,
  200. ; carry set indicates EOL was encountered.
  201. ;****************************************************************************
  202.  
  203. finddelim       proc    near
  204.                 lodsb                           ;Get the next character
  205.                 cmp     al,20h                  ;Exit if space
  206.                 je      fd_exit
  207.                 cmp     al,2Ch                  ;Exit if comma
  208.                 je      fd_exit
  209.                 cmp     al,0Dh                  ;Loop back for more if end
  210.                 jne     finddelim               ;  of line isn't reached
  211.  
  212.                 dec     si                      ;Set carry and exit
  213.                 stc
  214.                 ret
  215.  
  216. fd_exit:        dec     si                      ;Clear carry and exit
  217.                 clc
  218.                 ret
  219. finddelim       endp
  220.  
  221. ;****************************************************************************
  222. ; SCANHELP scans the command line for a /? switch.  If found, carry returns
  223. ; set and SI contains its offset.  If not found, carry returns clear.
  224. ;****************************************************************************
  225.  
  226. scanhelp        proc    near
  227.                 push    si                      ;Save SI
  228. scanloop:       lodsb                           ;Get a character
  229.                 cmp     al,0Dh                  ;Exit if end of line
  230.                 je      scan_exit
  231.                 cmp     al,"?"                  ;Loop if not "?"
  232.                 jne     scanloop
  233.                 cmp     byte ptr [si-2],"/"     ;Loop if not "/"
  234.                 jne     scanloop
  235.  
  236.                 add     sp,2                    ;Clear the stack
  237.                 sub     si,2                    ;Adjust SI
  238.                 stc                             ;Set carry and exit
  239.                 ret
  240.  
  241. scan_exit:      pop     si                      ;Restore SI
  242.                 clc                             ;Clear carry and exit
  243.                 ret
  244. scanhelp        endp
  245.  
  246. ;****************************************************************************
  247. ; ENCRYPT encrypts a block of data using an XOR algorithm.
  248. ;****************************************************************************
  249.  
  250. encrypt         proc    near
  251.                 mov     di,offset buffer        ;Point DI to buffer
  252.                 mov     si,password             ;Point SI to password
  253.                 mov     cx,bytesread            ;Initialize CX with count
  254.  
  255. enloop:         lodsb                           ;Get password character
  256.                 cmp     al,22h                  ;Branch if not a quotation
  257.                 jne     notquote                ;  mark
  258.                 mov     si,password             ;Reset BX to start of
  259.                 lodsb                           ;  start of password
  260. notquote:       xor     [di],al                 ;Encrypt the character
  261.                 inc     di                      ;Advance buffer pointer
  262.                 loop    enloop                  ;Loop until done
  263.                 ret
  264. encrypt         endp
  265.  
  266. buffer          =       $                       ;File I/O buffer
  267.  
  268. code            ends
  269.                 end     begin
  270.