I have to encrypt a particular URL parameter. If I want the output to be below 6-7 characters, what algorithm should I use?
The inputs are integer only ranging from 1 to 1,000,000.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you require encryption and need to have the shortest result possible, you must use a stream cipher. Blowfish (what you previously used) is a blockcipher and the result will always have the minimum size of one single block.
Find a comparison of stream ciphers on Wikipedia and the list of supported ciphers in the PHP manual on mcrypt
Also, the result of the encryption may contain special chars, so when putting it into an URL as a parameter, you should use
urlencode()orbase64_encode()Using
urlencode()orbase64_encode()will expand your string, making it longer than the original data. This is necessary to make it transport/URL safe.However, since your input is always a number you can use
base_convert()to shorten your input. On the decoding side, you’d have to do reverse the same thing.To get even shorter results, you could make use of the
enminicode()/deminicode()function provided by Aron Cederholm instead of usingbase_convert().Here is an example using the RC4 stream cipher (which is not very strong, by the way) and conversion from base 10 to base 36.
NOTE: this example only works on numbers hence its using
base_convert()to shrink the input string!Result: