I would like to encrypt / secure input string in the following format "AB123456789000" (two letters and 12 digits) so that the secured "version" can be stored as long (number) in the database.
I have tried RC2 encryption but it gives me 128 bit which does not fit in 64 bit long.
First of all I must have a little rant.
byte[]
, no need to represent it as onelong
. Every databasesystem should also be able to save binary blobs.Back to getting your input size down to under 64 bit.
Using ASCII, your 14-letter long string would be 14 bytes (= 112) bits, its encryption won't fit in 64 bits without compressing. You can cheat by saying that the format must be 2 letters and then 12 digits . For encoding, you'd take the first two letters verbatim in their ASCII representation. That would be 2*8 bit = 16 bit. Then take 12 digits as a whole number, e.g.
123456789000 = 0x1cbe991a08
which is 5 bytes (= 40 bit, it fits in along
). The biggest possible number here is9999999999
which is0xe8d4a50fff
, so everything can still be saved in 5 bytes.Alltogether we would have 2*8 bit + 5*8 bit = 56 bit, which is small enough to be fitting in one block of the RC2 encryption, which is 64 bit. You can set the last byte to
0x00
.So, in short, the 2 bytes of the encoded version of your plaintext would represent the ASCII value of these two letters, then the the next 5 bytes would be the number as a
long
.For example,
AB123456789000
would be as such be encoded to0x41421cbe991a08
('A' = 0x41, B = 0x42
, then123456789000 = 0x1cbe991a08
). You can write encoding and decoding functions in both Java and C# to realize this.