| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- library AES;
- { Important note about DLL memory management: ShareMem must be the
- first unit in your library's USES clause AND your project's (select
- Project-View Source) USES clause if your DLL exports any procedures or
- functions that pass strings as parameters or function results. This
- applies to all strings passed to and from your DLL--even those that
- are nested in records and classes. ShareMem is the interface unit to
- the BORLNDMM.DLL shared memory manager, which must be deployed along
- with your DLL. To avoid using BORLNDMM.DLL, pass string information
- using PChar or ShortString parameters. }
- uses
- SysUtils,
- Classes,
- ElAES,
- Math;
-
- {$R *.res}
- function AESDecryptStr(StrIn, AESKey: PChar): PChar; stdcall;
- 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;
- Size: integer;
- Key: TAESKey128;
- 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 := StrNew(PChar(Dest.DataString));
- Result := PChar(Dest.DataString);
- finally
- Source.Free;
- Dest.Free;
- end;
- end;
- //------------------------------------------------------------------------------
- function AESEncryptStr(StrIn, AESKey: PChar): PChar; stdcall;
- 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 := StrNew(PChar(StringToHex( Dest.DataString )));
- Result := PChar(StringToHex( Dest.DataString ));
- finally
- Source.Free;
- Dest.Free;
- end;
- end;
- exports
- AESDecryptStr, AESEncryptStr;
- begin
- IsMultiThread := TRUE;
- end.
|