I’m working on a site that generates a random puzzle and the exact puzzle can be recreated using this number. So i give them the url to the puzzle in case they want to share it with a friend or solve it later etc. somepuzzlesite.com/4233312409408127365 would generate a unique puzzle that is always the same if they use that link/number
What I don’t want is to expose how the puzzle is generated. The 9th digit, for example, can be 0 to 3, and defines the rotation of the puzzle.
If I just use it “as is” then a user could change a single digit in the url, see what changes, and eventually discover how I make my puzzle. I also wouldn’t mind if my number were smaller, since I don’t need all the way to 9:
digits 1st to 8th [possible values 0 to 5]
digit 9 [value 0 to 3]
digits 11th to 20th represent the arrangement of 10 objects in order.
I could just specify the first 9 objects in order, and then the unmentioned item is assumed to be last. (that gets me down to 9 digits used)
I could change the base, or use alpha characters in my URL in addition to digits, but some alpha characters are always trouble – lowercase “L” and “1” get mixed up easily, and “o” and zero can too.
But to keep the question simple, I’d just like to make it so that changing a single digit would represent a totally different number, and thereby create a totally different puzzle, rather than the minor difference that would result if I only changed one factor.
Let’s see… a rather naive approach would be this: Assign each value so many bits as is necessary to hold it. That is, you’d have eight 3-bit values, one 2-bit value, and ten 4-bit values. That’s 8*3+2+10*4=66 bits. Well, if you skip that last one, you’ll get 62 bits. You can get it even smaller, but that gets unnecessarily complicated.
Anyway.
Just take any standard encryption algorithm and apply it to these 62 bits. The industry-standard AES (aka Rijndael) operates on 128-bit blocks, which might be a bit too lengthy – or maybe not, depending on your preferences. 3DES won’t be any worse for your purposes, and works on 64-bit blocks, which is just perfect.
When you’ve got your encrypted 64 or 128 bits, just hex-encode them and make that the URL. If it’s 64 bits, you’ll have 16 hex characters. Not too much. And you’d be hard pressed to go lower anyway. Plus, it uses only 0-9, A-F, and there is little chance of mix-ups when calling over the phone. Not that people often share links vocally these days. 😛