home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 99 / af099a.adf / archives / af99a2.lzx / ARexx_Tutorial / Encrypt2.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1997-05-11  |  2.3 KB  |  137 lines

  1. /* -------------------------------------------------------- */
  2.  
  3. /* encrypt2.rexx */
  4.  
  5. /* -------------------------------------------------------- */
  6.  
  7. TEMP='ram:t/temp'
  8.  
  9. SOURCE_PROMPT='Please enter source file name'
  10.  
  11. DEST_PROMPT='Please enter destination file name'
  12.  
  13. KEY_PROMPT='Please enter encryption key file'
  14.  
  15. SIZE_ERROR='source or key file length error!'
  16.  
  17. SOURCE_ERROR='cannot open source file'
  18.  
  19. DESTINATION_ERROR='cannot open destination file'
  20.  
  21. END_PROMPT='All done!'
  22.  
  23. /* -------------------------------------------------------- */
  24.  
  25. say SOURCE_PROMPT; pull source_name
  26.   
  27. say DEST_PROMPT; pull dest_name
  28.  
  29. say KEY_PROMPT;parse pull key_name
  30.  
  31. address command 'list' key_name 'to' TEMP 'nodates nohead'
  32.  
  33. if Open(t,TEMP,'r') then
  34.  
  35.   do 
  36.  
  37.   list_output=Readln(t)
  38.   
  39.   key_length=Word(list_output,2)
  40.  
  41.   Close(t)
  42.  
  43.   if key_length >0 & key_length<65536 then
  44.   
  45.     do    
  46.  
  47.     address command 'list' source_name 'to' TEMP 'nodates nohead'
  48.  
  49.     if Open(t,TEMP,'r') then
  50.  
  51.       do 
  52.  
  53.        list_output=Readln(t)
  54.   
  55.        source_length=Word(list_output,2)
  56.  
  57.        Close(t)
  58.     
  59.        if source_length>0 then
  60.        
  61.          do
  62.            
  63.           block_count=source_length%key_length
  64.           
  65.           remaining_bytes=source_length-block_count*key_length
  66.  
  67.           call Encrypt()
  68.    
  69.           end /* of source_length >0 ok */           
  70.    
  71.       end /* of source_name temp file open ok */
  72.      
  73.      end /* of key_length ok */
  74.  
  75.     else say SIZE_ERROR
  76.      
  77.     end /* of key_name temp file open ok */
  78.   
  79. exit
  80.  
  81. /* -------------------------------------------------------- */
  82.  
  83. Encrypt:
  84.  
  85. if Open(k,key_name,'r') then
  86.     
  87.   do
  88.        
  89.     key=Readch(k,key_length)
  90.    
  91.     Close(k)
  92.    
  93.   end
  94.  
  95. if Open(s,source_name,'r') then
  96.  
  97.   do 
  98.   
  99.   if Open(d,dest_name,'w') then
  100.  
  101.      do 
  102.                 
  103.         do i=1 to block_count
  104.  
  105.           source=Readch(s,key_length) 
  106.           
  107.           source=BitXOR(source,key)           
  108.            
  109.           Writech(d,source)            
  110.  
  111.         end /* of handling i'th block */
  112.  
  113.         source=Readch(s,remaining_bytes)
  114.         
  115.         source=BitXOR(source,key)
  116.         
  117.         Writech(d,Left(source,remaining_bytes))
  118.         
  119.         say END_PROMPT
  120.  
  121.         Close(d)
  122.  
  123.      end /* open dest */
  124.  
  125.     else say DESTINATION_ERROR 
  126.  
  127.   end /* open source */
  128.  
  129.   else say SOURCE_ERROR
  130.  
  131. Close(s)
  132.  
  133. return
  134.  
  135. /* -------------------------------------------------------- */
  136.  
  137.