Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Edit1: TEdit;
- Label1: TLabel;
- Edit2: TEdit;
- Label2: TLabel;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- function ROTL8(x: Byte; shift:Integer):byte;
- begin
- Result:= ((x Shl shift) or (x shr (8-shift)));
- end;
- function Mx2(x: byte):byte;
- var y:Byte;
- begin
- if x < $80 //Check high bit
- then
- Result:=x shl 1
- else
- begin
- y:=x shl 1;
- Result:=y xor $1B;
- end;
- end;
- function Mx3(x: byte):byte;
- var y:Byte;
- begin
- if x < $80
- then
- begin
- y:=x shl 1;
- Result:=y xor x;
- end
- else
- begin
- y:=x shl 1;
- y:=y xor $1B;
- Result:=y xor x;
- end;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- i,j,k,l,n,z,BlQ:integer; // i,k - cycle counters; j - utility counter
- StateArray:array of Byte; // BlQ - Block quantity; z - 1 to 9 round counter
- KeyArray:array[1..16] of Byte; // n - amount of bytes in all block
- SBox:array[1..255] of Byte; // p,q and c - S-Box generator utility
- SpoofBuffer,p,c,q,r1,r2,r3,r4:Byte; // r1-4 - MixColumns() result, bx - buffer
- InputString,KeyString:string;
- begin
- p:=1; q:=1; // S-Box var reset
- k:=1; // Key repeater reset
- j:=0; // SubBytes() block counter reset
- InputString:=Edit1.Text; // Read plain text
- KeyString:=Edit2.Text; // Read key text
- n:=16-Length(InputString) mod 16; // Find modulo (ostatok ot deleniya)
- n:=n+Length(InputString); // Make number divisible by 16
- BlQ:=n div 16; // Amount of blocks
- SetLength(StateArray,n); // Array length divisible by 16
- //ShowMessage(IntToStr(x)); {DEBUG: Check int value}
- //ShowMessage(IntToHex(x,2)); Exit; {DEBUG: Check hex byte}
- for i:=1 to Length(InputString) do // Fill StateArray with plain input text
- begin StateArray[i-1]:=Byte(InputString[i]); end; // Not filling it with 0s cuz' it's all 0s already
- for i:=1 to 16 do begin KeyArray[i]:=$00; end; // Fill KeyArray with zeroes
- for i:=1 to Length(KeyArray) do // Fill KeyArray with key text
- begin KeyArray[i]:=Byte(KeyString[i]); end; //
- repeat // Generate S-Box
- begin // COPYPASTE FROM WIKI C IMPLEMENTATION
- if (p and $80)=$80 then c:=$1B else begin c:=$00; end; // [url]https://en.wikipedia.org[/url]
- p:= p xor (p shl 1) xor c; // /wiki/Rijndael_S-box
- q:= q xor q shl 1;
- q:= q xor q shl 2;
- q:= q xor q shl 4;
- if (q and $80)=$80 then c:=$09 else begin c:=$00; end;
- q:= q xor c;
- SBox[p+1]:=$63 xor q xor ROTL8(q,1)xor ROTL8(q,2)xor ROTL8(q,3)xor ROTL8(q,4);
- end;
- until p=1;
- SBox[1]:=$63; // 0 has no inverse
- //for i:=0 to Length(StateArray)-1 do // Initial Round AddRoundKey()
- // begin // All blocks of StateArray XOR KeyArray
- // if k=17 then k:=1; // Цикл который можно закомментировать
- // StateArray[i]:=StateArray[i] xor KeyArray[k];
- // k:=k+1;
- // end;
- for z:=1 to 6 do // Вот этот цикл до 3х раз повторяется с Initial Round AddRoundKey(), если оставить тот цикл закомментированным, тогда ошибка появится только на 7-м цикле
- begin
- for i:=0 to Length(StateArray)-1 do // SubBytes()
- begin j:=StateArray[i]; StateArray[i]:=SBox[j]; end;
- for i:=1 to BlQ do // ShiftRows() cycle start for all blocks
- begin // 2nd row shift
- SpoofBuffer:=StateArray[j+4]; // Save 1st byte to buffer var
- StateArray[j+4]:=StateArray[j+5]; // Move 2nd byte to 1st
- StateArray[j+5]:=StateArray[j+6]; // Move 3rd to 2nd
- StateArray[j+6]:=StateArray[j+7]; // Move 4th to 3rd
- StateArray[j+7]:=SpoofBuffer; // Write to 4th from buffer var
- for k:=1 to 2 do // 3rd row shift (2 times)
- begin
- SpoofBuffer:=StateArray[j+8];
- StateArray[j+8]:=StateArray[j+9];
- StateArray[j+9]:=StateArray[j+10];
- StateArray[j+10]:=StateArray[j+11];
- StateArray[j+11]:=SpoofBuffer;
- end;
- for k:=1 to 3 do // 4th row shift (3 times)
- begin
- SpoofBuffer:=StateArray[j+12];
- StateArray[j+12]:=StateArray[j+13];
- StateArray[j+13]:=StateArray[j+14];
- StateArray[j+14]:=StateArray[j+15];
- StateArray[j+15]:=SpoofBuffer;
- end;
- end; // End of ShiftRows()
- j:=0; l:=0; // Counters reset
- for i:=1 to BlQ do // MixColumns() cycle for all blocks
- begin
- for k:=0 to 3 do // Cycle for 4 columns
- begin
- j:=k+l;
- r1:= Mx2(StateArray[j+0]) xor Mx3(StateArray[j+4]) xor StateArray[j+8] xor StateArray[j+12];
- r2:= StateArray[j+0] xor Mx2(StateArray[j+4]) xor Mx3(StateArray[j+8]) xor StateArray[j+12];
- r3:= StateArray[j+0] xor StateArray[j+4] xor Mx2(StateArray[j+8]) xor Mx3(StateArray[j+12]);
- r4:= Mx3(StateArray[j+0]) xor StateArray[j+4] xor StateArray[j+8] xor Mx2(StateArray[j+12]);
- StateArray[j+0]:=r1;
- StateArray[j+4]:=r2;
- StateArray[j+8]:=r3;
- StateArray[j+12]:=r4;
- end;
- l:=l+16; // Adding 16 to go for next block
- end;
- end; // End of round
- Button1.Caption:='Processed!';
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement