I'm looking for a way to encrypt/obfuscate an integer ID into another integer. More precisely, I need a function int F(int x)
, so that
- x<->F(x) is one-to-one correspondence (if x != y, F(x) != F(y))
- given F(x), it's easy to find out x - so F is not a hash function
- given x and F(x) it's hard/impossible to find out F(y), something like
x ^ 0x1234
won't work
For clarity, I'm not looking for a strong encryption solution, it's only obfuscation. Imagine a web application with urls like example.com/profile/1
, example.com/profile/2
etc. The profiles themselves are not secret, but I'd like to prevent casual voyeurs to view/fetch all profiles one after another, so I'd rather hide them behind something like example.com/profile/23423
, example.com/profile/80980234
etc. Although database-stored tokens can do the job quite easily, I'm curious if there's some simple math available for this.
One important requirement I wasn't clear about is that results should look "random", that is, given a sequence x,x+1,...,x+n
, F(x),F(x+1)...F(x+n)
shouldn't form a progression of any kind.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…