3

Possible Duplicate:
Which “good” block encryption algorithm has the shortest output?

I need to encrypt a string and also be able to decrypt it. The problem I have is that I need to keep the encrypted string as short as possible, ideally the same length as the inputted string. I’ve looked at the Cryptography classes but the smallest size seems to use a 64bit block. This means that if my string is nine characters long I end up with a much longer, padded, encrypted string. I need to be able to read the encrypted string to people via the phone so a short code is a must. Can anyone recommend a solution or will I need to write my own encryption class to do this?

Community
  • 1
  • 1
Retrocoder
  • 4,483
  • 11
  • 46
  • 72
  • this isn't a duplicate - if you read the other thread, it's actually asking about something else (with a really bad title). stream ciphers - a good answer here - aren't mentioned there. – andrew cooke Sep 02 '13 at 02:06

1 Answers1

2

If you don't mind your strings getting slightly longer, you can use for example CTR mode encryption with any conventional block cipher. The extra length comes from the fact that you have to include a unique nonce in the output, otherwise it's not secure.

For example, assuming that you don't plan to encrypt more than a million strings, you could choose your nonce as four alphanumeric characters. That gives you a total space of 364 ≈ 1.68 million possible nonces. You just have to keep track of which nonces you've already used so that you won't reuse them. (If you choose your nonces at random, you have to make them about twice as long because of the birthday paradox.)

If you don't want your strings to get any longer when encrypted, what you need is format-preserving encryption.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153