Question

i found on internet some source for crypt string and i saw that on delphi 7 the string is crypted and decrypted well, and when i try to do the same things with delphi xe2,xe3,xe4,xe5 the encryption end decryption fail with this error "invalid buffer size for decryption"

i'm using aes.pas and eiaes.pas from here: http://code.google.com/p/jalphi-lib/source/browse/trunk/codemyth/delphi/dontuseit/?r=7

i think that the problem is the enconding of string.

Is it possible to solve this problem?

Was it helpful?

Solution

The AES library you provided the link to has not been updated to support the fact that "String" in later versions of Delphi (Delphi 2009 onward) is now a UnicodeString where each character is a WideChar.

You have 4 options:

  1. Contact the library author and ask if a Unicode version is planned/available

  2. Attempt to modify the library to support Unicode yourself (or find someone who can/will help do this)

  3. Find an alternative encryption library that already supports Unicode

  4. Ensure that you only use ANSI Strings with the library.

This last option may not be viable for you but if it is then you will still need to modify the AES library, but you will not need to do any code changes.

The problem is that "String" and "Char" in the later versions of Delphi are "Wide" types (2 bytes per 'character'). This difference is almost certainly causing problems with code in the AES library that assumes only ONE byte per character.

You can make these assumptions valid by making sure that the AES code is working with ANSI Strings.

If you choose to do this, then I would suggest that you introduce two new types:

type
  AESString = ANSIString;
  AESChar   = ANSIChar;
  PAESChar  = ^AESChar;

You will then need to go through the AES library code replacing any reference to "String" with "AESString", "Char" with "AESChar" and "PChar" with "PAESChar".

This should then make AES an ANSI String library and it will still be usable in Delphi 7 (i.e. pre-Delphi 2009) if that's important for you.

If you then find in the future that you do need to fully support Unicode strings and then need to properly fix the AES library code itself, you can do this and then simply change the AESString and AESChar types to:

type
  AESString = String;
  AESChar   = Char;

If this is then compiled with a non-Unicode version of Delphi, the library will automatically revert back to ANSI String ("String" == ANSIString pre-D2009), so your Unicode changes will need to take this into account if you need to support both Unicode and non-Unicode versions of Delphi. You do need to be careful, but this is not difficult.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top