home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 1112.dms / 1112.adf / Encrypt / Encrypt.src < prev    next >
Text File  |  1988-07-25  |  8KB  |  361 lines

  1.  
  2.     opt    D-            
  3.     
  4. ; Assembly settings should be executable.  Change D- to D+ for debugging
  5. ; and use M+ to expand MACROs into listing during debugging
  6. ; compiled DEVPAC software (works on all versions).
  7.  
  8. ;******************************************************************
  9. ;*                                  *
  10. ;* Data Encryption program by Steve Meredith 1991.  The program   *
  11. ;* prevents unauthorised reading or use of a file.  Encryption    *
  12. ;* does not support FASTMEM (ie. Extra memory).                   *
  13. ;* Assembled with DEVPAC 1 or 2.                            *
  14. ;*                                  *
  15. ;* Letters, questions and general stuff to me at :          *
  16. ;*    74 Pemberton Road, Winstanley, Wigan, WN3 6DA, UK      *             
  17. ;* or   28 Seaton Street, Middlesbrough, Cleveland, TS1 3NQ, UK      *
  18. ;******************************************************************
  19.  
  20. ;----- DOS calls --------------------
  21.  
  22. _LVOOpen    EQU    -30 
  23. _LVOClose    EQU    -36 
  24. _LVORead    EQU    -42 
  25. _LVOWrite    EQU    -48 
  26. _LVOOutput    EQU    -60 
  27. _LVOLock    EQU    -84 
  28. _LVOUnLock    EQU    -90 
  29. _LVOExamine    EQU    -102 
  30. _LVOInfo    EQU    -114
  31.  
  32. ;----- EXEC calls -------------------
  33.  
  34. _LVOAllocMem    EQU    -198 
  35. _LVOFreeMem    EQU    -210 
  36. _LVOOpenLibrary        EQU    -552
  37. _LVOCloseLibrary    EQU    -414 
  38.  
  39. ;----- MISC Equates -----------------
  40.  
  41. _SysBase    EQU    4
  42. MEMF_CHIP    EQU    1<<1
  43. MEMF_CLEAR    EQU    1<<16
  44. MODE_OLDFILE    EQU    1005
  45. MODE_NEWFILE    EQU    1006
  46.  
  47. ;----- PRINT Macro equates ----------
  48.  
  49. Free        EQU 2
  50. Exit            EQU 1
  51. Comeback        EQU 0
  52.  
  53. ;----- CALL EXEC MACRO --------------
  54.  
  55. CALLEXEC MACRO
  56.     move.l    (_SysBase).w,a6
  57.     jsr    _LVO\1(a6)
  58.     ENDM
  59.  
  60. ;----- CALL DOS MACRO ---------------
  61.  
  62. CALLDOS    MACRO
  63.     move.l    _DOSBase,a6
  64.     jsr    _LVO\1(a6)
  65.     ENDM
  66.  
  67. ;----- PRINT TEXT MACRO -------------
  68.  
  69. PRINT MACRO
  70.     CALLDOS    Output
  71.     move.l    #\1,d2
  72.     move.l    #53,d3
  73.     CALLDOS Write
  74.     move.b    #\2,d0
  75.     cmp.b    #Exit,d0
  76.     beq    clib
  77.     cmp.b    #Free,d0
  78.     beq    FreeMemory
  79.     ENDM
  80.  
  81. ;----- MAIN PROGRAM BEGINS HERE -----
  82.  
  83. Open_Dos_Lib
  84.     move.l    a0,a4
  85.     move.l    d0,a0
  86.       move.l    #dosname,a1
  87.     CALLEXEC OpenLibrary        ; Open library
  88.     tst.l    d0
  89.     beq    Quit            ; Whoops, error.
  90.     move.l    d0,_DOSBase        ; Ok. Keep DOSBase
  91.     move.l    a4,a0
  92.     
  93. Check_Input
  94.     move.b    (a0),d0            ; Check if user just presses
  95.     cmp.b    #$0a,d0            ; [RETURN]
  96.     bne.s    Split_Args
  97.     PRINT    UsageTxt,Comeback
  98.     PRINT    UsageTxt2,Exit        ; Print usage and quit
  99.  
  100. Split_Args
  101.     PRINT    Working,Comeback
  102.     move.l    a4,a0
  103.     bsr    StringSplit        ; Get password
  104.     move.l    d1,PassWORD
  105.     lea.l    2(a0),a0
  106.     bsr    StringSplit        ; Get passcode
  107.     move.l    d1,PassCODE
  108.     lea.l    2(a0),a0
  109.     move.l    a0,FileName        ; Get filename
  110.  
  111. FindSpc    lea.l    1(a0),a0        ; NULL terminate filename
  112.     cmp.b    #$0A,(a0)
  113.     bne    FindSpc
  114.     move.b    #0,(a0)
  115.  
  116. Get_Lock
  117.     move.l    FileName,d1        ; Check if file exists
  118.     move.l    #-2,d2
  119.     CALLDOS Lock    
  120.     move.l    d0,MyLock
  121.  
  122.     
  123. Alloc_InfoMem
  124.     move.l    #256,d0
  125.     move.l    #MEMF_CHIP!MEMF_CLEAR,d1 ; I want CHIP memory
  126.     CALLEXEC AllocMem          ;
  127.     tst.l    d0             ;
  128.     bne.s    Get_DiskInfo         ; Successful
  129.     PRINT    NoMemory,Exit
  130.     
  131. Get_DiskInfo            ; Check WRITEPROTECT ENABLED
  132.     move.l    d0,TOP
  133.     move.l    d0,d2
  134.     move.l    MyLock,d1
  135.     CALLDOS Info
  136.     move.l    TOP,a1
  137.     move.l    8(a1),a1
  138.     move.l    a1,d0
  139.     cmp.b    #$52,d0        ; Check ENABLED
  140.     beq.s    Get_FileInfo
  141.     move.l    TOP,a1
  142.     move.l    #256,d0        ; Size of memory allocated
  143.     moveq    #0,d1
  144.     CALLEXEC FreeMem    
  145.     move.l    MyLock,d1
  146.     CALLDOS UnLock
  147.     PRINT    DiskErr,Exit    ; WRITE PROTECTED exit
  148.  
  149. Get_FileInfo            ; Get information on file
  150.     move.l    TOP,a5
  151.     move.l    MyLock,d1
  152.     move.l    a5,d2
  153.     CALLDOS Examine
  154.  
  155. Free_Lock
  156.     move.l    MyLock,d1
  157.     CALLDOS UnLock
  158.  
  159. Get_Size
  160.     move.l    124(a5),a5
  161.     move.l    a5,FileSize    ; Get filesize
  162.  
  163. Free_InfoMem
  164.     move.l    TOP,a1
  165.     move.l    #256,d0        ; Size of memory allocated
  166.     moveq    #0,d1
  167.     CALLEXEC FreeMem    
  168.  
  169. Allocate_Memory
  170.     move.l    FileSize,d0
  171.     add.b    #$10,d0
  172.     move.l    d0,TotalSize
  173.     move.l    #MEMF_CHIP!MEMF_CLEAR,d1 ; I want CHIP memory
  174.     CALLEXEC AllocMem          ;
  175.     tst.l    d0             ;
  176.     bne    MemOK             ; Successful
  177.     PRINT    NoMemory,Exit
  178.     
  179. MemOK    move.l    d0,TOP            ; Set save block addresses
  180.     add    #16,d0            ;
  181.     move.l    d0,MIDDLE        ;
  182.     add    #16,d0            ;
  183.     move.l    d0,BOTTOM        ;
  184.     
  185. ; ***** OPEN DATAFILE *****
  186.     move.l    #0,d0
  187.     move.l    FileName,d1        ; Filename address to d1
  188.     move.l    #MODE_OLDFILE,d2    ; 
  189.     CALLDOS Open            ;         
  190.     tst.l    d0
  191.     bne    Readfle
  192.     PRINT    Error1,Free        ; Couldn't get file handle
  193.     
  194. ; ***** READ DATAFILE INTO ALLOCATED MEMORY (at MIDDLE address) *****
  195.  
  196. Readfle    move.l    d0,FHandle    
  197.     move.l    MIDDLE,d2        ; Allocated-memory space  to d2        
  198.     move.l    FileSize,d3        ; File size to d3
  199.     CALLDOS Read            ; Read data
  200.     move.l    d0,FileSize
  201.     
  202.     move.l    MIDDLE,a1        ; Old or New file?
  203.     cmp.l    #'Cryp',(a1)
  204.     beq    CheckPass        ; old file, check pass
  205.     
  206. PrepNew    PRINT    NewTxt,Comeback
  207.     move.l    TOP,a1
  208.     move.l    #'Cryp',(a1)+
  209.     move.l    #'t-2 ',(a1)+
  210.     move.l    #'(c) ',(a1)+
  211.     lea.l    PassWORD,a2        ; Create diskcode
  212.     move.l    (a2),d1
  213.     lea.l    PassCODE,a2
  214.     move.l    (a2),d2
  215.     EOR.l    d1,d2
  216.     not.l    d2
  217.     move.l    d2,(a1)
  218.     move.l    MIDDLE,CryptAddress
  219.     move.l    TOP,FileAddress
  220.     move.b    #$10,ExtraBytes
  221.     bra    Cr_DeCrypt
  222.     
  223. CheckPass
  224.     lea.l    12(a1),a1
  225.     move.l    (a1),d0
  226.     lea.l    PassCODE,a1
  227.     move.l    (a1),d1
  228.     EOR.l    d1,d0
  229.     not.l    d0
  230.     lea.l    PassWORD,a1
  231.     move.l    (a1),d1
  232.     cmp.l    d1,d0
  233.     beq    PassOK
  234.     move.l    FHandle,d1        ; FILEHANDLE to d1
  235.     CALLDOS Close            ; Close file
  236.     PRINT   UnAuth,Free        ; Wrong code! exit.
  237.     
  238. PassOK    PRINT    OldTxt,Comeback
  239.     add.l    #-$10,FileSize
  240.     move.l  BOTTOM,CryptAddress
  241.     move.l    BOTTOM,FileAddress
  242.     
  243. Cr_DeCrypt
  244.     lea.l    PassWORD,a1
  245.     move.l    (a1),d2
  246.     not.l    d2
  247.     move.l    CryptAddress,a1
  248.     move.l    FileSize,d0
  249. Crypt                    ; Crypt/DeCrypt datafile    
  250.     eor.l    d2,(a1)            ; Xor password with data
  251.     add.l    #4,a1            ; Encrypt every longword
  252.     sub.l    #4,d0            ; Decrement file-size counter
  253.     tst.l    d0
  254.     bgt.s    Crypt
  255.     eor.l    d2,(a1)
  256.  
  257. Save    move.l    FHandle,d1
  258.     CALLDOS Close
  259.  
  260.     move.l    FileName,d1        ; Filename address to a1
  261.     move.l    #MODE_NEWFILE,d2     ; 
  262.     CALLDOS Open            ;         
  263.     move.l    d0,FHandle        ; Store file handle.
  264.         
  265.     move.l    FHandle,d1
  266.     move.l    FileAddress,d2
  267.     move.l    FileSize,d3
  268.     add.b    ExtraBytes,d3
  269.     CALLDOS Write
  270.     move.l    FHandle,d1        ; FILEHANDLE to d1
  271.     CALLDOS Close            ; Close file
  272.     PRINT    Finished,Comeback
  273.     
  274. FreeMemory
  275.     move.l    TOP,a1
  276.     move.l    TotalSize,d0        ; Size of memory allocated
  277.     moveq    #0,d1
  278.     CALLEXEC FreeMem    
  279.  
  280. clib    move.l    _DOSBase,a1        ; Close DOS library
  281.     CALLEXEC CloseLibrary
  282.  
  283. Quit    rts    
  284.  
  285. ;*********************************************************************
  286. ;*                                     *
  287. ;* Sub-routine to split users CODE and PASS words and to extend the  *
  288. ;* CODE or PASS to 4 characters if need be.                 *
  289. ;*                                     *
  290. ;*********************************************************************
  291.  
  292. StringSplit
  293.     clr.l    d2
  294.     clr.l    d1
  295.     move.b    (a0),-(sp)
  296.     moveq    #3,d0
  297. Push    lea.l    1(a0),a0
  298.     cmp.b    #$20,(a0)
  299.     beq    Extend
  300.     
  301.     move.b    (a0),-(sp)
  302.     subq    #1,d0
  303.     tst.b    d0
  304.     bne    Push
  305.     
  306. Zoom    lea.l    1(a0),a0
  307.     cmp.b    #$20,(a0)
  308.     bne    Zoom
  309.     lea.l    -1(a0),a0
  310.     bra    Pullstk
  311.     
  312. Extend    lea.l    -1(a0),a0    ; pad last letter to 4 character word 
  313. Again    move.b    (a0),-(sp)
  314.     subq    #1,d0
  315.     tst.b    d0
  316.     bne    Again
  317.     
  318. Pullstk    move.l    #0,d1        ; Pull characters from stack
  319.     move.b    (sp)+,d1
  320.     mulu.w    #$100,d1
  321.     move.b    (sp)+,d1
  322.     swap.w    d1
  323.     move.b    (sp)+,d2
  324.     mulu.w    #$100,d2
  325.     move.b    (sp)+,d2
  326.     
  327.     add.l    d2,d1        ; 4 characters into d1
  328.     rts
  329.     
  330. ;************************* STORAGE *********************************
  331.  
  332. dosname        dc.b     'dos.library',0
  333. _DOSBase    dc.l    0    ; DOSbase address
  334. PassWORD    dc.l    0    ;
  335. PassCODE    dc.l    0    ;
  336. FileName    dc.l    0    ;
  337. CryptAddress    dc.l    0    ;
  338. FileAddress    dc.l    0    ;
  339. FileSize    dc.l    0    ;
  340. FHandle        dc.l    0    ;
  341. MIDDLE        dc.l    0    ;
  342. TOP        dc.l    0    ;
  343. BOTTOM        dc.l    0    ;
  344. TotalSize    dc.l    0    ;
  345. ExtraBytes    dc.b    0    ;
  346. MyLock        dc.l    0    ;
  347.  
  348. DiskErr        dc.b    'Disk Error : Please Write-Enable?.                  ',10
  349. NoMemory    dc.b    'Cant Allocate Memory - Kill External Sources/ReBoot ',10
  350. Error1        dc.b    'Unable to open file/incorrect parameters.           ',10
  351. UnAuth        dc.b    'Operation Aborted - Incorrect keys.                 ',10
  352. Finished    dc.b    'Operation complete.                                 ',10
  353. UsageTxt2    dc.b    'PassCode : MAX 4 chars in length. No FASTMEM support',10
  354. UsageTxt    dc.b    '   Usage : Crypt [PassWord] [PassCode] [filename]   ',10
  355. OldTxt        dc.b    '->De-Crypting file.                                 ',10
  356. NewTxt        dc.b    '->Crypting ',27,'[3mnewfile.',27,'[0m','                         ',10
  357. Working        dc.b    27,'[7m',10,'Encryption  by S.Meredith.',10,27,'[0m','->Checking File ',10
  358.    
  359.         even
  360.  
  361.