home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / STREAM15.ZIP / ENCRYPT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-03-27  |  2.1 KB  |  75 lines

  1. {$B-}   { Use fast boolean evaluation. }
  2.  
  3. program Encrypt;
  4.  
  5. { Program to demonstrate use of TEncryptFilter }
  6.  
  7. {$i StDefine.inc}
  8.  
  9. uses
  10.   {$ifdef wobjects} wobjects, {$else} objects,  {$endif}
  11.   {$ifdef windows}  wincrt,   {$endif}
  12.   streams;
  13.  
  14. procedure SyntaxExit(s:string);
  15. begin
  16.   writeln;
  17.   writeln(s);
  18.   writeln;
  19.   writeln('Usage:  ENCRYPT Sourcefile Destfile');
  20.   writeln(' will encrypt sourcefile using key $12345678.');
  21.   writeln(' Run ENCRYPT on the encrypted file to decrypt it.');
  22.   halt(99);
  23. end;
  24.  
  25. var
  26.   Source : PBufStream;
  27.   Dest   : PEncryptFilter;
  28.   filename : string;
  29. begin
  30.   if paramcount <> 2 then
  31.     SyntaxExit('Two parameters required.');
  32.  
  33.   { Open the source file with a buffer size of 2048. }
  34.  
  35.   {$ifdef windows}
  36.   Filename := Paramstr(1);
  37.   Filename[length(filename)+1] := #0;
  38.   New(Source, Init( @filename[1], stOpenRead, 2048) );
  39.   {$else}
  40.   New(Source, Init( Paramstr(1), stOpenRead, 2048) );
  41.   {$endif windows}
  42.  
  43.   if (Source = nil) or (Source^.status <> stOk) then
  44.     SyntaxExit('Unable to open file '+ParamStr(1)+' for reading.');
  45.  
  46.   { Open the destination file with a buffer size of 2048, and insert it
  47.     into the encrypting filter. }
  48.  
  49.   {$ifdef windows}
  50.   Filename := Paramstr(2);
  51.   Filename[length(filename)+1] := #0;
  52.   New(Dest,   Init($12345678, New(PBufStream,
  53.                                   Init( @filename[1], stCreate, 2048))));
  54.   {$else}                                             
  55.   New(Dest,   Init($12345678, New(PBufStream,
  56.                                   Init( Paramstr(2), stCreate, 2048))));
  57.   {$endif windows}
  58.   if (Dest = nil) or (Dest^.status <> stOk) then
  59.     SyntaxExit('Unable to create file '+Paramstr(2)+'.');
  60.  
  61.   { Encrypt the source file by copying it to the filter.}
  62.  
  63.   Write('Encrypting ',Paramstr(1),' to ',Paramstr(2),'...');
  64.   Dest^.CopyFrom(Source^, Source^.GetSize);
  65.   if Dest^.status <> stOK then
  66.     SyntaxExit('File error during encryption.');
  67.  
  68.   { Dispose of stream variables to close the files.}
  69.  
  70.   Dispose(Source, done);
  71.   Dispose(Dest, done);
  72.  
  73.   Writeln('Done.');
  74. end.
  75.