Advertisement
Guest User

AES

a guest
Dec 4th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.68 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Edit1: TEdit;
  13.     Label1: TLabel;
  14.     Edit2: TEdit;
  15.     Label2: TLabel;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.     { Private declarations }
  19.   public
  20.     { Public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.dfm}
  29.  
  30. function ROTL8(x: Byte; shift:Integer):byte;
  31. begin
  32.   Result:= ((x Shl shift) or (x shr (8-shift)));
  33. end;
  34.  
  35. function Mx2(x: byte):byte;
  36. var y:Byte;
  37. begin
  38.   if x < $80 //Check high bit
  39.   then
  40.   Result:=x shl 1
  41.   else
  42.   begin
  43.   y:=x shl 1;
  44.   Result:=y xor $1B;
  45.   end;
  46. end;
  47. function Mx3(x: byte):byte;
  48. var y:Byte;
  49. begin
  50. if x < $80
  51.   then
  52.   begin
  53.   y:=x shl 1;
  54.   Result:=y xor x;
  55.   end
  56.   else
  57.   begin
  58.   y:=x shl 1;
  59.   y:=y xor $1B;
  60.   Result:=y xor x;
  61.   end;
  62. end;
  63.  
  64. procedure TForm1.Button1Click(Sender: TObject);
  65. var
  66.   i,j,k,l,n,z,BlQ:integer;                                                       // i,k - cycle counters; j - utility counter
  67.   StateArray:array of Byte;                                                      // BlQ - Block quantity; z - 1 to 9 round counter
  68.   KeyArray:array[1..16] of Byte;                                                 // n - amount of bytes in all block
  69.   SBox:array[1..255] of Byte;                                                    // p,q and c - S-Box generator utility
  70.   SpoofBuffer,p,c,q,r1,r2,r3,r4:Byte;                                            // r1-4 - MixColumns() result, bx - buffer
  71.   InputString,KeyString:string;
  72. begin
  73.   p:=1; q:=1;                                                                    // S-Box var reset
  74.   k:=1;                                                                          // Key repeater reset
  75.   j:=0;                                                                          // SubBytes() block counter reset
  76.   InputString:=Edit1.Text;                                                       // Read plain text
  77.   KeyString:=Edit2.Text;                                                         // Read key text
  78.   n:=16-Length(InputString) mod 16;                                              // Find modulo (ostatok ot deleniya)
  79.   n:=n+Length(InputString);                                                      // Make number divisible by 16
  80.   BlQ:=n div 16;                                                                 // Amount of blocks
  81.   SetLength(StateArray,n);                                                       // Array length divisible by 16
  82. //ShowMessage(IntToStr(x));                                                      {DEBUG: Check int value}
  83. //ShowMessage(IntToHex(x,2)); Exit;                                              {DEBUG: Check hex byte}
  84. for i:=1 to Length(InputString) do                                               // Fill StateArray with plain input text
  85.   begin StateArray[i-1]:=Byte(InputString[i]); end;                              // Not filling it with 0s cuz' it's all 0s already
  86. for i:=1 to 16 do begin KeyArray[i]:=$00; end;                                   // Fill KeyArray with zeroes
  87. for i:=1 to Length(KeyArray) do                                                  // Fill KeyArray with key text
  88.   begin KeyArray[i]:=Byte(KeyString[i]); end;                                    //
  89. repeat                                                                           // Generate S-Box
  90.   begin                                                                          // COPYPASTE FROM WIKI C IMPLEMENTATION
  91.   if (p and $80)=$80 then c:=$1B else begin c:=$00; end;                         // [url]https://en.wikipedia.org[/url]
  92.   p:= p xor (p shl 1) xor c;                                                     // /wiki/Rijndael_S-box
  93.   q:= q xor q shl 1;
  94.   q:= q xor q shl 2;
  95.   q:= q xor q shl 4;
  96.   if (q and $80)=$80 then c:=$09 else begin c:=$00; end;
  97.   q:= q xor c;
  98.   SBox[p+1]:=$63 xor q xor ROTL8(q,1)xor ROTL8(q,2)xor ROTL8(q,3)xor ROTL8(q,4);
  99.  
  100.   end;
  101. until p=1;
  102. SBox[1]:=$63;                                                                    // 0 has no inverse
  103. //for i:=0 to Length(StateArray)-1 do                                              // Initial Round AddRoundKey()
  104. //  begin                                                                          // All blocks of StateArray XOR KeyArray
  105. //  if k=17 then k:=1;                                                             // Цикл который можно закомментировать
  106. //  StateArray[i]:=StateArray[i] xor KeyArray[k];
  107. //  k:=k+1;
  108. //  end;
  109. for z:=1 to 6 do  // Вот этот цикл до 3х раз повторяется с Initial Round AddRoundKey(), если оставить тот цикл закомментированным, тогда ошибка появится только на 7-м цикле
  110.  begin
  111.  for i:=0 to Length(StateArray)-1 do // SubBytes()
  112.  begin j:=StateArray[i]; StateArray[i]:=SBox[j]; end;
  113.  for i:=1 to BlQ do // ShiftRows() cycle start for all blocks
  114.   begin                              // 2nd row shift
  115.    SpoofBuffer:=StateArray[j+4];     // Save 1st byte to buffer var
  116.    StateArray[j+4]:=StateArray[j+5]; // Move 2nd byte to 1st
  117.    StateArray[j+5]:=StateArray[j+6]; // Move 3rd to 2nd
  118.    StateArray[j+6]:=StateArray[j+7]; // Move 4th to 3rd
  119.    StateArray[j+7]:=SpoofBuffer; // Write to 4th from buffer var
  120.    for k:=1 to 2 do // 3rd row shift (2 times)
  121.    begin
  122.    SpoofBuffer:=StateArray[j+8];
  123.    StateArray[j+8]:=StateArray[j+9];
  124.    StateArray[j+9]:=StateArray[j+10];
  125.    StateArray[j+10]:=StateArray[j+11];
  126.    StateArray[j+11]:=SpoofBuffer;
  127.    end;
  128.    for k:=1 to 3 do // 4th row shift (3 times)
  129.    begin
  130.    SpoofBuffer:=StateArray[j+12];
  131.    StateArray[j+12]:=StateArray[j+13];
  132.    StateArray[j+13]:=StateArray[j+14];
  133.    StateArray[j+14]:=StateArray[j+15];
  134.    StateArray[j+15]:=SpoofBuffer;
  135.    end;
  136.   end; // End of ShiftRows()
  137.   j:=0; l:=0; // Counters reset
  138.   for i:=1 to BlQ do // MixColumns() cycle for all blocks
  139.   begin
  140.    for k:=0 to 3 do // Cycle for 4 columns
  141.    begin
  142.     j:=k+l;
  143.     r1:= Mx2(StateArray[j+0]) xor Mx3(StateArray[j+4]) xor StateArray[j+8] xor StateArray[j+12];
  144.     r2:= StateArray[j+0] xor Mx2(StateArray[j+4]) xor Mx3(StateArray[j+8]) xor StateArray[j+12];
  145.     r3:= StateArray[j+0] xor StateArray[j+4] xor Mx2(StateArray[j+8]) xor Mx3(StateArray[j+12]);
  146.     r4:= Mx3(StateArray[j+0]) xor StateArray[j+4] xor StateArray[j+8] xor Mx2(StateArray[j+12]);
  147.     StateArray[j+0]:=r1;
  148.     StateArray[j+4]:=r2;
  149.     StateArray[j+8]:=r3;
  150.     StateArray[j+12]:=r4;
  151.    end;
  152.   l:=l+16; // Adding 16 to go for next block
  153.   end;
  154.  end; // End of round
  155. Button1.Caption:='Processed!';
  156. end;
  157. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement