hi guys, i need to encrypt a plain text limited to 15 characters but the output the encryption must also be 15 characters since i am limited to database constraint.
how can i do this?


7 Years
Discussion Span
Last Post by Tom Gunn

For a good block cipher the responsible thing to do is change the constraint to expect cipher text length instead of plain text length or change the limit of the plain text to be short enough to meet the current database constraint. If you use a block cipher it is easy to figure out the size of the cipher text because it will be a multiple of the block size for the algorithm.

For example, if you want to use Blowfish and the plain text limit is 15 characters, that is 30 bytes for C#'s char type and the next block multiple is 32 bytes because Blowfish uses a block size of 8 bytes. Then you need to add the overhead of a text representation like base 64. Base 64 adds 4 characters to the string for every 3 bytes and does some padding for lengths not divisible by 3. To hold the base 64 cipher text of 15 two byte characters the database constraint should grow from 15 characters to 44 characters.

Growing the database constraint is better than shrinking the application constraint because if you do the math, the plain text length would have to be very small for 15 base 64 characters.

If you really want the cipher text to have the same length as the plain text, look into stream cipher algorithms. AES can be run in a mode called CFB that turns it into a stream cipher. Or you can use any of the crappy algorithms like XOR or any variation of the Caesar ciphers. But I still think changing the database to hold an encrypted string instead of plain text is best. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.