Convert encrypted strings with v4 but now decrypt with v5

Hi

I just installed version 5 of “cryptography pack,” and I see differences in how it works compared to v4 for AES encryption (for now).
In the old version, a 256-bit key required 32 characters, but in v5, you need 64 characters for a 256-bit key (the same number of characters is required for a 128-bit key).

My problem is that passwords were encrypted and stored in the database using CBC mode with a 256-bit key for a Base64 output format in version 4.

However, with version 5, decrypting these passwords does not work.
My question is: how can I decrypt these passwords from v4 with v5? Is there an intermediate step? i.e., decrypt with v4 then re-encrypt with v5?

This post can help me AES decryption fails with invalid key length ?

Regards

olivier

Hello Olivier,

The thread should indeed help you as the issue looks very similar.

Here what I said at that time.

“Just on "in earlier versions the key length was shorter": no, the key was in 'raw' format by default, so the length was then 32 bytes for kl256. If you use a key converted to 'hexa', then the size is 64 bytes.
The new 'inputFormat' property allows users to select the format (raw, hexa, base64, base64url or base32). The 'issue' is that the format shall be applied to all parameters (key, IV, salt, etc.) and input (text string or file).

Then, you are correct that parameters for the key have an order. Because of the possible encoding of the key, it is not possible to identify the key size only with the number of bytes in the key. A key length is also required.”

You can have a look at the code towards the end of the thread. It should fix your issue, with the appropriate format conversions.

Regards,

bernard

Bernard. I have the same error. I just don't want to create another thread. Previously, key was equal to 32 characters. On the client side, files were encrypted with this key. Today the same error was updated that the length should be 16. At the same time, the TAESEncryption component lost the keyLength object inspector field. And if I forcibly specify keyLength := kl256 in the code, Delphi does not compile. I also read this thread: AES decryption fails with invalid key length - #11 by Fab

And I didn’t understand how to solve the problem, because your last code in this post does not compile, it also gives an error:
Laes.KeyLength:= kl256;

How can I fix it so that on the client side the key of 32 characters is saved and it decrypts old files?

Hi All,

Could you please send me your code in 4.3.3 with examples key, IV, input and output?

I will reproduce the cases and get back to you with the code in 5.x

bernard [at] tmssoftware [dot ]com

I already had 5.0.1.0. And it worked fine there. Component cast on the form (SEA). There are the following settings (the key is indicated as an example, on the client side it is its own, but also 32 characters):slight_smile:

And the code that decrypts the file stream is simple

SEA.DecryptStream(zipStreamAES, zipStream);

But in version 5.1.1.x it no longer works. It says that a 16 character code is needed. And DesignTime is missing the KeyLength property

OK, this is a different issue then.

I moved the TAESKeyLength declaration from the AESObj file to CryptoConst.pas (to reduce duplication and to reduce conversions between identical constants from different files). The side effect is that the KeyLength property is no longer in the visual AESEncryption class. There may be a valid explanation, but it looks strange to me. Possibly a compiler issue that I am not aware of.

To fix that, you need to assign the key size in your code, as it is no longer in the graphic component.

How do I set the key size in code? KeyLength:= kl256 causes an error.

It’s hard to tell without the exact error, but most likely because the key was set (in the graphic component) before assigning the length.

The default size is 128, so if you set a longer key, you get an error. You can change the default length in the class (Create functions) or do this:

AES.keyLength := kl256;

AES.Key := MyKeyString; // 32 character long

That's the problem. I already wrote about it above. Delphi says she doesn't know what kl256 is.

Delphi 12.2

Then, add CryptoConst to the ‘uses’ section, as the definition has been moved.

Thank you very much! Everything worked! You need to indicate somewhere in the documentation that you changed the location.

Yes, I will improve the wording.

It is not an issue if you write a new app, but it is for legacy code where the ‘uses’ section is already there.

The app is old. It was written back in version 4 of the library.