I'm working with a vendor's API and it recommends the following steps for transmitting an encrypted value:

1) Convert the encryption password to a byte array.
2) Convert the value to be encrypted to a byte array.
3) The entire length of the array is inserted as the first four bytes onto the front of the first block of the resultant byte array before encryption.
4) Encrypt value with AES

Just curious, do steps 1-3 make the encrypted values significantly more secure than if they just started with step 4? Is this a recommended practice?

It is making implementation more complicated.

  • 1
    What do you mean by "just start with step 4"? Every AES implementation I have ever seen operates on byte arrays. Which AES implementation are you using that accepts Strings for the key and the message? Dec 20, 2018 at 22:01
  • 1
    To continue in this topic, if this is really the description of the library, don't use it. It sounds very much that the library writer has little knowledge from AES (or from writing libraries, like why no dedicated length param). ... Same goes for the advice to use ECB, just don't hear on such nonsense.
    – deviantfan
    Dec 20, 2018 at 22:04
  • @MikeOunsworth I meant in terms of using a library to encrypt. I am not trying to implement AES myself. I am trying to use a library for the aes encryption. What i listed was in their instructions to me to encrypt the current time. In order to use their API they listed for me to encrypt the current time using AES. They gave me a secret key to use and I wasn't sure if all their instructions were necessary really.
    – Gabriel
    Dec 20, 2018 at 22:12
  • 1
    @Gabriel I'm not sure I understand. Can you link an example of an AES library that accepts something other than byte arrays, or link to some example code that "just starts with step 4"? I honestly don't know what that means. Like, maybe the library you're using is fine, or maybe you should use a different library. Hard to tell if we don't know which AES library you're referring to. Dec 20, 2018 at 22:17
  • Using Ruby. OpenSSL for the encryption.
    – Gabriel
    Dec 20, 2018 at 22:38

1 Answer 1


First two steps are normal since AES libraries accept bytes. 3th one for prevention of the length extension attacks. Assume that we are adding the length at the end and we have 2 ciphertext blocks; {B1,B2}. If someone noticed another ciphertext with 3 blocks {D1,D2,D3} then {B1,D2,D3} will be valid block. Of course, one can do the same trick if the length at the beginning or even both.

Actually, the real problems are;

  • Which encryption mode are you using?

    • You should not use ECB mode which is not secure. Use CBC mode that requires IV or CTR mode.
  • Is there any authentication?

    • The problems in the first paragraph are due to there is no authentication mentioned. AES-GCM, which is a standard in TLS 1.3 , provides Authenticated Encryption. That prevents the modification by the attacker since you can check the authentication tag of the data.

Note: Nothing mentioned about the padding scheme. Assumed that there is one around as PKCS#5 padding.

  • They want us to use ECB mode with an empty IV. As for authentication we are using a JWT authentication process.
    – Gabriel
    Dec 20, 2018 at 21:54
  • 1
    ECB leaks patterns. See at Wikipedia. You should convince them to use authenticated encryption AES-GCM
    – kelalaka
    Dec 20, 2018 at 21:56
  • 2
    @Gabriel Nobody is talking about authentication of users here. "Authenticated encryption" refers to something that not only makes the data unreadable (without key), but also can verify if the data was changed when decrypting.
    – deviantfan
    Dec 20, 2018 at 22:00
  • @deviantfan I see, my apologies. I misunderstood. I'm not entirely sure what they are doing to verify this. To interact with their api I have to encrypt the current time using AES. They gave me a secret key and gave me steps to do this but their steps/instructions are vague/unclear and I'm having trouble getting the correct result.
    – Gabriel
    Dec 20, 2018 at 22:10
  • 3
    Anyways, you need this data format information, otherwise nobody can solve this. And maybe make a question why [your situation for time encryption] is a bad idea, and then show the answers to your boss. Putting everything here together, I get a strong feeling the API creators are wildly incompetent.
    – deviantfan
    Dec 20, 2018 at 22:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .