Sergei Basharov
Sergei Basharov

Reputation: 53850

Encrypt a string that is of constant length when encrypted

Say, I have a string that is a chat message, and its length in characters can be, for example, from 1 to 200.

How do I encrypt it the way so that independently of the length of the incoming string, always produces the encrypted string of fixed length, say, 400 characters?

Are there encryption algorithms that provide this functionality or what technique should I use to achieve this result?

If I choose to pad the original string to n symbols, then what algorithms produce an output string of the same length for different strings of the same length? I noticed that the most of algorithms produce strings that have different lengthed strings for the same input string, like "Hello!" and "MewMew".

Upvotes: 4

Views: 1512

Answers (1)

Ebbe M. Pedersen
Ebbe M. Pedersen

Reputation: 7488

Just pad your message to the length you want prior to encryption.

A block cipher like AES would always produce an predictable output size that are dictated by the size of input. Here a message of size n would produce an encrypted message of size n+1 (assuming padding mode) rounded up to nearest block of 16 bytes. So a message of size 384-399 bytes would produce output of 400 bytes.

Upvotes: 1

Related Questions