I gave a task which encrypts an input integer value, this integer maximum 4 length, however, I'm required to encrypt into an alphanumeric string. Apart from this, the result that i generated from the same value (eg 10) the value have to not same. The most difficult parts is, The encrypted string maximum to only can have 15 length since we have to put it in the query string. It was a difficult task and I tried to ask google and I don't found any solution can help me with this. all the length is too long and doesn't meet the requirement I needed. Any encrypt professional can help me with this?
-
1Please include some sample inputs and outputs (in your post, not as a comment). _`maximum 4 integer` could be read many ways._ Also, why is the length so important to you? – mjwills Jul 10 '17 at 09:02
-
it is just pure integer , input maximum 4 integer, output maximum alphanumeric 15 characters – Ryan Shine Jul 10 '17 at 09:03
-
1Can you ask your leader why he/she wants it that way? Also, I have no idea what `6 integer` means. – mjwills Jul 10 '17 at 09:04
-
Possible duplicate: https://stackoverflow.com/questions/35247441/encrypt-and-decrypt-a-string-to-fixed-length – M0CH1R0N Jul 10 '17 at 09:06
-
mean that 6 parameters – Ryan Shine Jul 10 '17 at 09:50
-
sorry all, because my leader want to put 6 parameters in the query string, in the end he dont want the query string to be very long since he have 6 parameters need to encrypt – Ryan Shine Jul 10 '17 at 13:00
2 Answers
Assumptions: "integer maximum 6 length" means 6 numeric characters 000000-999999.
Encrypt with a algorithm that has a 8-byte block size and then Base64 encode, that will produce 12 characters of output.
Append 2 random bytes to the 6 characters of data to make 8-bytes, this will cause up to 2^16 or 65536 different results on encryption of the same value. Encrypt in ECB mode and Base64 encode. That will produce 12 characters of output.
To recover the input decode the Base64 encrypted to data, decrypt that and delete the 2 random bytes.
Possible encryption algorithm include Blowfish, XTEA, DES and others.
Note: For a larger range of different output the 6-digit number could first be converted to a binary representation of 3-bytes allowing 5 random bytes producing 2^40 different outputs for the same 6-digit input.

- 111,848
- 21
- 189
- 228
-
i have edit my question maybe u can take a look to determine which encrypt method i should use – Ryan Shine Jul 10 '17 at 13:07
I assume that you want a function to input a four digit number: [0000 .. 9999] and produce a 15 character alphanumeric output.
You do not make it clear if this function is to be reversible. If reversibility is not needed, then a one-way hash function will do what you want. 15 hex characters are 60 bits or 15 Base32 characters are 75 bits. Use a larger size hash function, truncate and convert to hex or Base32. Base32 gives a wider range of output characters than hex.
For reversibility you will need a Format Preserving Encryption, where the output size is limited to 60 or 75 significant bits. For 60 bits, use DES as the base cipher as it has a 64 bit block size. 75 bits is more awkward. AES, at 128 bits, has too large a block size so you might need to write a simple 76 bit Feistel cipher. That will give you good obscurity, but only middling security. You do not say how secure this function needs to be.

- 15,344
- 1
- 24
- 38
-
Note from the question: "**maximum** can have 15 lengths of the encrypted string" – zaph Jul 10 '17 at 12:03
-
1