Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My problem is the following:

In an existing database I want to encrypt data in a couple of columns. The columns contains strings of different lengths.

I don't want to change the size of the columns so the encryption need to produce an equal length text representation of the input text.

The strength of the encryption algorithm is of secondary interest but of course I want it to be as strong as it can be. Otherwise I wouldn't need to encrypt the data. But the most important thing is the size of the output.

Is this possible? If so how would I do it?

I'm interested in doing it in .NET. No database-level encryption.

like image 869
Christian80 Avatar asked Jun 12 '09 09:06

Christian80


People also ask

What can we do when we need to encrypt a message which has an uneven length?

If you want a longer ciphertext, just pad with zeroes. You can make the ciphertext as long as you want that way.

How would you encrypt any string?

Given a string s, the task is to encrypt the string in the following way: If the frequency of current character is even, then increment current character by x. If the frequency of current character is odd, then decrement current character by x.

Can you encrypt and decrypt with the same key?

One key encrypts data and another key decrypts it. Symmetric encryption uses the same key to perform both encryption and decryption functions. Symmetric encryption uses a shared private key while asymmetric encryption uses a public/private key pair.

How long is an encrypted string?

Yes, with most operating modes of AES, the length of the encrypted string is the same regardless of key size (128, 192 or 256 bits). That's because AES-128, AES-192 and AES-256 all are 128-bit block ciphers, meaning they operate on 128-bit data blocks, regardless of key size.


1 Answers

Within your constrants, I would use AES in CFB mode, which turns it into a stream cipher and the output length will be the same as the input length. Unless you're storing the strings in blobs, you'll need to hex or base64 encode the output to make it char friendly, which will be a 100% or 33% increase in length.

One .NET implementation is here.

like image 165
Jason Fritcher Avatar answered Oct 23 '22 19:10

Jason Fritcher