2

Possible Duplicate:
How do I encrypt a string and get a equal length encrypted string?

I am new to Encryption and Decryption. I have a string which is 24 char length. I need to encrypt and decrypt the word. The encryption may be less secure but I need encrypted word should be same length as input string (24 char). I have searched through web and find some sample Encryption algorithm (AES, MD5). But the encrypted word is too length than input string. This is product key that we will share to customer, so strong encryption is not required. It would be useful if you share sample codes.

Community
  • 1
  • 1
Vinoth
  • 1,975
  • 21
  • 34
  • 3
    MD5 is a hashing function and it can not be decrypted. What your looking for is not really viable with only 24 characters. About the only thing you might be able to do is XOR encryption and then convert the values to HEX, but that will double your length to 48. – Geoffrey Aug 09 '12 at 08:51
  • 2
    So if I'm understanding this correctly, you have a string that has 24 characters, you want to "mix up" this string to another string of 24 characters and then be able to recover the original string later using some cipher / algorithm from the "mixed up" string? – Paul Aldred-Bann Aug 09 '12 at 08:53
  • @Terric, yes this is what exactly my requirement. – Vinoth Aug 09 '12 at 09:01

3 Answers3

4

Use Vernam cipher. For a single string with a truly-randomly generated key it's theoretically unbreakable. If you start using the same key for multiple strings you reduce its security significantly but apparently you are not looking for utmost security. If you are, you must be able to come up with a different random key for each encrypted password.

Although you can find lots of sample code on the web, I think it would be good practice to you to implement it yourself. It's pretty straightforward.

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
2

What you're looking for seems to be Format-preserving encryption, I don't think there are any implementations of this in .NET (I certainly haven't used any). You may need to think up a custom algorithm for this. You say strong encryption isn't required, but you'll obviously need the algorithm to not be obvious. Unfortunately there are literally hundreds of ways you could do this, so it depends on which one suits you.

This seems to be a great post for encryption algorithms

Paul Aldred-Bann
  • 5,840
  • 4
  • 36
  • 55
0

To make the cyphertext the same length as the plaintext, use a stream cypher. This can either be a block cypher in CTR mode, such as AES-CTR, or a dedicated stream cypher, like Rabbit or RC4.

Be aware that you cannot reuse a key for a stream cypher, otherwise an attacker will probably be able to break the encryption. Two cyphertexts that use the same key can be used to eliminate the key entirely, leaving just the two plaintexts.

If you only have the one 24 byte word to encrypt then this is not a problem. If you need to encrypt more than one piece of data, then key management becomes important.

rossum
  • 15,344
  • 1
  • 24
  • 38