I’m looking into encrypting a simple database primary key into a short, url safe string. I’ve looked into the jasypt encryption library, but the length of each encrypted number is at least 20 characters long. I’m looking for something that can be as small as possible, yet still 2 way encryptable, possibly around 5 characters long. The encryption doesn’t have to be extremely secure, I would prefer it not to be obvious. Any ideas?
Share
The shortest way would be to just use an index with a large enough alphabet of url safe characters and the url. Just “count” up the index and save that to the db with the url.
The length is just defined by the number of urls you have to store.
For example, if the alphabet you will use is using all letters plus numbers with case sensitivity you can store (24+24+10)^5 urls (with 5 characters).
So first url might be: 00001. This apporach assumes it is ok that the url keys can be guessed.
If you need to make sure that they cannot be guessed easily, just randomize the next key, check that the key is not in the db and use it. There is such a fast number of keys possible that they should not collide too often unless you have millions of keys in the db.