AES.dpr 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. library AES;
  2. { Important note about DLL memory management: ShareMem must be the
  3. first unit in your library's USES clause AND your project's (select
  4. Project-View Source) USES clause if your DLL exports any procedures or
  5. functions that pass strings as parameters or function results. This
  6. applies to all strings passed to and from your DLL--even those that
  7. are nested in records and classes. ShareMem is the interface unit to
  8. the BORLNDMM.DLL shared memory manager, which must be deployed along
  9. with your DLL. To avoid using BORLNDMM.DLL, pass string information
  10. using PChar or ShortString parameters. }
  11. uses
  12. SysUtils,
  13. Classes,
  14. ElAES,
  15. Math;
  16. {$R *.res}
  17. function AESDecryptStr(StrIn, AESKey: PChar): PChar; stdcall;
  18. function HexToString(S: string): string;
  19. var
  20. i: integer;
  21. begin
  22. Result := '';
  23. // Go throught every single hexadecimal characters, and convert
  24. // them to ASCII characters...
  25. for i := 1 to Length( S ) do
  26. begin
  27. // Only process chunk of 2 digit Hexadecimal...
  28. if ((i mod 2) = 1) then
  29. Result := Result + Chr( StrToInt( '0x' + Copy( S, i, 2 )));
  30. end;
  31. end;
  32. var
  33. Source: TStringStream;
  34. Dest: TStringStream;
  35. Size: integer;
  36. Key: TAESKey128;
  37. begin
  38. // Convert hexadecimal to a strings before decrypting...
  39. Source := TStringStream.Create( HexToString( strIn ));
  40. Dest := TStringStream.Create( '' );
  41. try
  42. // Start decryption...
  43. Size := Source.Size;
  44. Source.ReadBuffer(Size, SizeOf(Size));
  45. // Prepare key...
  46. FillChar(Key, SizeOf(Key), 0);
  47. Move(PChar(AESKey)^, Key, Min(SizeOf(Key), Length(AESKey)));
  48. // Decrypt now...
  49. DecryptAESStreamECB(Source, Source.Size - Source.Position, Key, Dest);
  50. // Display unencrypted text...
  51. //Result := StrNew(PChar(Dest.DataString));
  52. Result := PChar(Dest.DataString);
  53. finally
  54. Source.Free;
  55. Dest.Free;
  56. end;
  57. end;
  58. //------------------------------------------------------------------------------
  59. function AESEncryptStr(StrIn, AESKey: PChar): PChar; stdcall;
  60. function StringToHex(S: string): string;
  61. var
  62. i: integer;
  63. begin
  64. Result := '';
  65. // Go throught every single characters, and convert them
  66. // to hexadecimal...
  67. for i := 1 to Length( S ) do
  68. Result := Result + IntToHex( Ord( S[i] ), 2 );
  69. end;
  70. var
  71. Source: TStringStream;
  72. Dest: TStringStream;
  73. Size: integer;
  74. Key: TAESKey128;
  75. begin
  76. // Encryption
  77. Source := TStringStream.Create(strIn );
  78. Dest := TStringStream.Create( '' );
  79. try
  80. // Save data to memory stream...
  81. Size := Source.Size;
  82. Dest.WriteBuffer( Size, SizeOf(Size) );
  83. // Prepare key...
  84. FillChar( Key, SizeOf(Key), 0 );
  85. Move( PChar(AESKey)^, Key, Min( SizeOf( Key ), Length( AESKey)));
  86. // Start encryption...
  87. EncryptAESStreamECB( Source, 0, Key, Dest );
  88. // Display encrypted text using hexadecimals...
  89. //Result := StrNew(PChar(StringToHex( Dest.DataString )));
  90. Result := PChar(StringToHex( Dest.DataString ));
  91. finally
  92. Source.Free;
  93. Dest.Free;
  94. end;
  95. end;
  96. exports
  97. AESDecryptStr, AESEncryptStr;
  98. begin
  99. IsMultiThread := TRUE;
  100. end.