| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- unit AES;
- interface
- uses
- Windows,SysUtils,Classes,ElAES,math;
- function AESDecyptStr(strIn,AESKey: string): string;
- function AESEncryptStr(strIn,AESKey: string): string;
- implementation
- function AESDecyptStr(strIn,AESKey: string): string;
- 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;
- var
- Source: TStringStream;
- Dest: TStringStream;
- Start, Stop: cardinal;
- Size: integer;
- Key: TAESKey128;
- EncryptedText: TStrings;
- S: string;
- begin
- // Convert hexadecimal to a strings before decrypting...
- Source := TStringStream.Create( HexToString( strIn ));
- Dest := TStringStream.Create( '' );
- try
- // Start decryption...
- Size := Source.Size;
- Source.ReadBuffer(Size, SizeOf(Size));
- // Prepare key...
- FillChar(Key, SizeOf(Key), 0);
- Move(PChar(AESKey)^, Key, Min(SizeOf(Key), Length(AESKey)));
- // Decrypt now...
- DecryptAESStreamECB(Source, Source.Size - Source.Position, Key, Dest);
- // Display unencrypted text...
- Result := Dest.DataString;
- finally
- Source.Free;
- Dest.Free;
- end;
- end;
- function AESEncryptStr(strIn,AESKey: string): string;
- 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;
- var
- Source: TStringStream;
- Dest: TStringStream;
- Size: integer;
- Key: TAESKey128;
- begin
- // Encryption
- Source := TStringStream.Create(strIn );
- 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(AESKey)^, Key, Min( SizeOf( Key ), Length( AESKey)));
- // Start encryption...
- EncryptAESStreamECB( Source, 0, Key, Dest );
- // Display encrypted text using hexadecimals...
- Result := StringToHex( Dest.DataString );
- finally
- Source.Free;
- Dest.Free;
- end;
- end;
- end.
|