I need to write a coupon code system but I do not want to save each coupon code in the database. (For performance and design reasons.) Rather I would like to generate codes subsequent that are watermarked with another code.
They should like kind of fancy and random. Currently they look like this:
1: AKFCU2, 2: AKFDU2, 3: AKFDW2, 4: AKHCU2, 5: AKHCW2, ..., 200: CLFSU2, 201: CLFSW2, ...
It's obvious that subsequent codes look very similar as I just converted my code ingredients (the watermark and the integer in the front) to binary base and permutated the order by a fixed scheme. But to prevent people to easily guess another valid code or even accidently enter another valid code (thus making another code invalid by using it) I would prefer something more chaotic, like this:
1: FIOJ32, 2: X9NIU2, 3: SIUUN7, 4: XTVV4S, ...
In the end the problem is to find a bijective, discrete function on the domain {0,1}^27 (or alternatively {0,1,2,3,4, ..., [10^(8.5)]}) that is far away from being continuous. Also it should be as simple as possible to implement. (EDIT: I also need to implement the reverse function.)
Any suggestions for such a function?
Take any odd $a$ and calculate $x \mapsto ax + b \pmod{2^{27}}$.
EDIT: Here's a more sophisticated suggestion. The following functions are all invertible and easy to implement:
If you compose them repeatedly they become harder to break (but probably still easy for someone who already knows the general form of your cipher).
By composing, I mean you apply several of them in succession (you can apply a given mapping more than once with same or different parameters).
Another suggestion is to use a random permutation. It's not difficult to generate a random permutation, and given a permutation to calculate its inverse. You can store both permutations in a file and load them to memory (if you have enough - it's 1GB for both) each time your main program starts.