home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue58 / alfresco / Unit1.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2000-05-01  |  3.6 KB  |  161 lines

  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     procedure Button1Click(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.   public
  16.     { Public declarations }
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.DFM}
  25.  
  26. uses
  27.   AADES;
  28.  
  29. procedure TForm1.Button1Click(Sender: TObject);
  30. var
  31.   DESEngine : TaaDESEngine;
  32.   Key       : TaaDESKey64;
  33.   Plain     : TaaDESBlock;
  34.   Cipher    : TaaDESBlock;
  35.   i         : integer;
  36.  
  37.   InStrm    : TFileStream;
  38.   OutStrm   : TFileStream;
  39.  
  40. begin
  41.   writeln('--Perform standard test case--');
  42.   Key[0] := $01;
  43.   Key[1] := $23;
  44.   Key[2] := $45;
  45.   Key[3] := $67;
  46.   Key[4] := $89;
  47.   Key[5] := $ab;
  48.   Key[6] := $cd;
  49.   Key[7] := $ef;
  50.   Plain[0] := $01;
  51.   Plain[1] := $23;
  52.   Plain[2] := $45;
  53.   Plain[3] := $67;
  54.   Plain[4] := $89;
  55.   Plain[5] := $ab;
  56.   Plain[6] := $cd;
  57.   Plain[7] := $e7;
  58.  
  59.   write('Key:        ');
  60.   for i := 0 to 7 do
  61.     write(Format('%.2x', [Key[i]]));
  62.   writeln;
  63.  
  64.   write('Plaintext:  ');
  65.   for i := 0 to 7 do
  66.     write(Format('%.2x', [Plain[i]]));
  67.   writeln;
  68.  
  69.   writeln('encrypting...');
  70.   FillChar(Cipher, sizeof(Cipher), 0);
  71.   DESEngine := TaaDESEngine.Create(Key, true);
  72.   try
  73.     DESEngine.ProcessBlock(Plain, Cipher);
  74.     write('Ciphertext: ');
  75.     for i := 0 to 7 do
  76.       write(Format('%.2x', [Cipher[i]]));
  77.     writeln;
  78.   finally
  79.     DESEngine.Free;
  80.   end;
  81.  
  82.   writeln('(should be: C95744256A5ED31D)');
  83.  
  84.   writeln('decrypting...');
  85.   FillChar(Plain, sizeof(Plain), 0);
  86.   DESEngine := TaaDESEngine.Create(Key, false);
  87.   try
  88.     DESEngine.ProcessBlock(Cipher, Plain);
  89.     write('Plaintext:  ');
  90.     for i := 0 to 7 do
  91.       write(Format('%.2x', [Plain[i]]));
  92.     writeln;
  93.   finally
  94.     DESEngine.Free;
  95.   end;
  96.  
  97.   writeln('--Encrypt file with DES/ECB--');
  98.   InStrm := nil;
  99.   OutStrm := nil;
  100.   DESEngine := nil;
  101.   try
  102.     DESEngine := TaaDESEngine.Create(Key, true);
  103.     InStrm := TFileStream.Create('unit1.pas', fmOpenRead + fmShareDenyWrite);
  104.     OutStrm := TFileStream.Create('c:\unit1.ecb', fmCreate);
  105.     DESEngine.ProcessStream(InStrm, OutStrm, dmECB);
  106.   finally
  107.     DESEngine.Free;
  108.     OutStrm.Free;
  109.     InStrm.Free;
  110.   end;
  111.  
  112.   writeln('--Decrypt file with DES/ECB--');
  113.   InStrm := nil;
  114.   OutStrm := nil;
  115.   DESEngine := nil;
  116.   try
  117.     DESEngine := TaaDESEngine.Create(Key, false);
  118.     InStrm := TFileStream.Create('c:\unit1.ecb', fmOpenRead + fmShareDenyWrite);
  119.     OutStrm := TFileStream.Create('c:\unit1.pa1', fmCreate);
  120.     DESEngine.ProcessStream(InStrm, OutStrm, dmECB);
  121.   finally
  122.     DESEngine.Free;
  123.     OutStrm.Free;
  124.     InStrm.Free;
  125.   end;
  126.  
  127.   writeln('--Encrypt file with DES/CBC--');
  128.   InStrm := nil;
  129.   OutStrm := nil;
  130.   DESEngine := nil;
  131.   try
  132.     DESEngine := TaaDESEngine.Create(Key, true);
  133.     InStrm := TFileStream.Create('unit1.pas', fmOpenRead + fmShareDenyWrite);
  134.     OutStrm := TFileStream.Create('c:\unit1.cbc', fmCreate);
  135.     DESEngine.ProcessStream(InStrm, OutStrm, dmCBC);
  136.   finally
  137.     DESEngine.Free;
  138.     OutStrm.Free;
  139.     InStrm.Free;
  140.   end;
  141.  
  142.   writeln('--Decrypt file with DES/CBC--');
  143.   InStrm := nil;
  144.   OutStrm := nil;
  145.   DESEngine := nil;
  146.   try
  147.     DESEngine := TaaDESEngine.Create(Key, false);
  148.     InStrm := TFileStream.Create('c:\unit1.cbc', fmOpenRead + fmShareDenyWrite);
  149.     OutStrm := TFileStream.Create('c:\unit1.pa2', fmCreate);
  150.     DESEngine.ProcessStream(InStrm, OutStrm, dmCBC);
  151.   finally
  152.     DESEngine.Free;
  153.     OutStrm.Free;
  154.     InStrm.Free;
  155.   end;
  156.  
  157.   writeln('Done');
  158. end;
  159.  
  160. end.
  161.