Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make encrypted string same length as input string?

I have a 15 digit string to encrypt.

I have tried using .NET's various encryption functions but all my encrypted strings are at least 24, 28 or 32 in length.

I have heard of CipherMode.CTS with Padding = None that produces the same length output as the input length but I can't seem to get the same result. I have toyed with the block-size, key-size and salt size.

I don't mind adding a digit to my input string to make it 16 in length.

Anyone know a workaround?

like image 664
Unomono Avatar asked Nov 24 '10 05:11

Unomono


2 Answers

The code word is Format-preserving encryption. Unfortunately I am not aware of any .NET implementations (the framework certainly hasn't any).

like image 24
Rasmus Faber Avatar answered Oct 14 '22 23:10

Rasmus Faber


If you use ECB mode, it'll round the input up to the next multiple of the block size, and then produce exactly that same size of output.

Almost every other mode includes/uses an Initialization Vector (IV) that makes the output one block larger than the input (again, after the input has been rounded up to the next multiple of the block size).

Most stream ciphers produce output that is exactly the same size as the input -- but in typical encryption libraries, stream ciphers are much less common than block ciphers. One reason is that key distribution is a much more serious problem with stream ciphers (using the same key twice with a stream cipher produces a major security hole).

like image 162
Jerry Coffin Avatar answered Oct 15 '22 00:10

Jerry Coffin