unit AES;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ElAES, Math;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Label2: TLabel;
    Button2: TButton;
    Label3: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
  private
    { Private declarations }
    function StringToHex(S: string): string;
    function HexToString(S: string): string;
    function Encrypt(Passwd: String): String;
    function Decypt(Passwd: String): String;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//AES加解密用到的 Function ===========================
function StringToHex(S: string): string;
var
	i: integer;

begin
  Result := '';

  // Go throught every single characters, and convert them
  // to hexadecimal...
	for i := 1 to Length( S ) do
  	Result := Result + IntToHex( Ord( S[i] ), 2 );
end;

function HexToString(S: string): string;
var
	i: integer;

begin
  Result := '';

  // Go throught every single hexadecimal characters, and convert
  // them to ASCII characters...
  for i := 1 to Length( S ) do
  begin
    // Only process chunk of 2 digit Hexadecimal...
  	if ((i mod 2) = 1) then
	  	Result := Result + Chr( StrToInt( '0x' + Copy( S, i, 2 )));
  end;
end;

//AES加密Function
function Encrypt(Passwd: String): String;
var
  Source: TStringStream;
  Dest: TStringStream;
  Start, Stop: cardinal;
  Size: integer;
  Key: TAESKey128;
  PWD: String;
begin
  //設定加密密碼;
  PWD := '/.,mnbvcxz';

  Source := TStringStream.Create( Passwd );
  Dest   := TStringStream.Create( '' );

  try
    // Save data to memory stream...
    Size := Source.Size;
    Dest.WriteBuffer( Size, SizeOf(Size) );

    // Prepare key...
    FillChar( Key, SizeOf(Key), 0 );
    Move( PChar(PWD)^, Key, Min( SizeOf( Key ), Length( PWD )));

    // Start encryption...
    Start := GetTickCount;
    EncryptAESStreamECB( Source, 0, Key, Dest );
    Stop := GetTickCount;
    //Label_Time.Caption := IntToStr(Stop - Start) + ' ms';
    //Refresh;

    // Display encrypted text using hexadecimals...
	  //Memo_CyperText.Lines.BeginUpdate;
    Result := StringToHex( Dest.DataString );
	  //Memo_CyperText.Lines.EndUpdate;

  finally
    Source.Free;
    Dest.Free;
  end;
end;

//AES解密Function
function Decypt(Passwd: String): String;
var
  Source: TStringStream;
  Dest: TStringStream;
  Start, Stop: cardinal;
  Size: integer;
  Key: TAESKey128;
  EncryptedText: TStrings;
  S: string;
  PWD: String;
begin
  //設定加密密碼;
  PWD := '/.,mnbvcxz';

  // Convert hexadecimal to a strings before decrypting...
  Source := TStringStream.Create( HexToString( Passwd ));
  Dest   := TStringStream.Create( '' );

  try
    // Start decryption...
    Size := Source.Size;
    Start := GetTickCount;
    Source.ReadBuffer(Size, SizeOf(Size));

    // Prepare key...
    FillChar(Key, SizeOf(Key), 0);
    Move(PChar(PWD)^, Key, Min(SizeOf(Key), Length(PWD)));

    // Decrypt now...
    DecryptAESStreamECB(Source, Source.Size - Source.Position, Key, Dest);
    Stop := GetTickCount;

    // Display unencrypted text...
    Result := Dest.DataString;

  finally
    Source.Free;
    Dest.Free;
  end;
end;
//AES加解密用到的 Function ===========================


end.
