Static ciphertext length for any plaintext length less or equal than x? I am encrypting buffers using AES-256-CTR encryption. My encryption is working but I have a requirement that I want to make sure I implement correctly. The requirement is as follows:

Given any plaintext of length less that x, the ciphertext should always be of length x.

In my example


x is 1 KB but can be anything while consistent.
The plaintext could range between 1 byte to 1 KB, never larger.
The length of the plaintext is not know by decryptor.
If you can give up two bytes (so the longest message is 1022 bytes long), then this can be straightforward.

Create a 1024-byte buffer. Make the first two bytes the length of the message. Then add the message. Then pad the rest of the buffer with random data. Random data is best here because it leaks less information than zeros, but you could use zeros.

Encrypt the entire buffer. When you decrypt the result, pull the first two bytes and use that to extract the right number of bytes.

This of course will work for any length; so if you need a full 1024-byte message, you can just make the ciphertext always be 1026 bytes.