tags:

views:

112

answers:

3

Hey,

I have a string of 10-15 characters and I want to encrypt that string.

The problem is - I want to get a shortest encrypted string as possible.

I will also want to decrypt that string back to its original string.

Which encryption algorithm fits best to this situation ?

Thanks

+4  A: 

AES uses a 16-byte block size; it is admirably suited to your needs if your limit of 10-15 characters is firm. The PKCS#11 (IIRC) padding scheme would add 6-1 bytes to the data and generate an output of exactly 16 bytes. You don't really need to use an encryption mode (such as CBC) since you're only encrypting one block. There is an issue of how you'd be handling the keys - there is always an issue of how you handle encryption keys.

If you must go with shorter data lengths for shorter strings, then you probably need to consider AES in CTR mode. This uses the key and a counter to generate a byte stream which is XOR'd with the bytes of the string. It would leave your encrypted string at the same length as the input plaintext string.

You'll be hard pressed to find a general purpose compression algorithm that reliably reduces the length of such short strings, so compressing before encrypting is barely an option.

Jonathan Leffler
+3  A: 

If it's just one short string, you could use a one-time pad which is mathematically perfect secrecy.

http://en.wikipedia.org/wiki/One-time_pad

Just be sure you don't use the key more than one time.

A: 

If the main goal is shortening, I would look for a compression library that allows a fixed dictionary built on a corpus of common strings.
Personally I do not have experience with that, but I bet LZMA can do that.

Michel de Ruiter