Reputation: 22248
I want to obfuscate my ids in me webapp.
So I need to change
f(id) = hidden_id
with easily accessible f-1(hidden_id) = id
.
Security is not a big deal, but it would be nice if f
is not obvious (like f(x) = ax + b
). A good signal to achieve this would be that a < b => f(a) < f(b)
is not true.
My basic knowledge is crypto tells me I should look at something like f(x) = x^a % b
but I haven't found my way through this until now.
I thought that was a simple question!
Thanks
Upvotes: 0
Views: 168
Reputation: 22248
I ended up writing those functions (did the XOR trick suggested by @Niels2000)
In Ruby:
def self.encrypt(id)
# Concat [1] because first number IS a 0 hence information is lost
([1] + id.to_s(2).split('').reverse.map{ |b| 1 - b.to_i}).join('').to_i(2)
end
def self.decrypt(encrypted_id)
# Remove the [1]
(encrypted_id.to_s(2).split('').map{ |b| 1 - b.to_i}).join('')[1..-1].reverse.to_i(2)
end
Upvotes: 0
Reputation: 4457
I was going to talk about encryption and xor, but maybe what you really need is to just use random ids. It depends on what it is you're trying to defend against.
Upvotes: 1
Reputation: 5661
Use any fast block cipher (AES or even old DES) with a random fixed key. It is not cryptographically secure, but it has all the desired properties.
Upvotes: 1