views:

115

answers:

4

I need some pointers or a practical example on how to encrypt an int to another int, and a secret key would be required to decrypt the value.

Something like:

encrypt(1, "secret key") == 67123571122
decrypt(67123571122, "secret key") == 1

This guy asks pretty much the same question: http://stackoverflow.com/questions/3131193/symmetric-bijective-algorithm-for-integers
however me being a total encryption "n00b" I would like some more practical examples, in python if possible.

I understand I need to use some kind of block cipher, but I'm pretty lost on the part about keeping the encrypted result still be numeric and somewhat short (maybe a long instead of an int)

Any pointers? Thanks

UPDATE- Why do I want to do this?
I have a web service where each "object" gets a URL, e.g.: example.com/thing/123456/

Right now, those IDs are sequential. I want to hide the fact that they're sequential (database IDs).

The stuff on those pages is not "top secret" or anything like that, but it shouldn't be as easy for someone to snoop in some other random' object as just incrementing that ID in the URL.

So with some kind of two-way numeric encryption, the URL IDs will not be sequential at all, and it would take someone quite a bit of time to find more of these objects. (Additionally, requests are throttled)

And the only reason I want to keep this numeric instead of an arbitrary string is so that the change is a total drop-in replacement, and things will just work without any other code changes.

Also, I can't just generate new random database IDs. I have to handle this encrypt/decrypt in the application.

A: 

You want to encrypt just a single 'int' ie q 32/64 bit number?
Then the easiest way is to just XOR it with a 32/64bit secret key.

Martin Beckett
Alternately, XOR it with a smaller key, repeated over the span of the number.
Christian Mann
What would the code for this look like, more or less?
Infinity
be careful with straight xor because then with only 64 examples there's a chance the user can figure out the key.
Mike Axiak
Also note that this makes it trivial to get the key if you have just one encrypted/decrypted pair.
sth
@Mike: Xor with same length secret key is the definition of OTP. Of course you dont want to reuse the key.
knitti
@Knitti: Given that the poster mentioned "crypto" and "n00b" you might want to explain OTP...I'm guessing you mean "one-time-pad".
Cameron Skinner
Right, OTP=One Time Pad, and as @Spike mentioned: **Don't roll your own crypto**, there are many more things to do wrong than there are good free libraries out there (and there are some)
knitti
Thanks guys, I updated the question. Maybe you can lead me in a better direction now
Infinity
An OTP is totally unsuited to this situation.
Nick Johnson
+2  A: 

It depends how cryptographically secure you want to be. For not-very-secure (in the crypto sense - probably fine for everyday use if you don't really expect serious attack) then XOR with a fixed secret key will work. Just be aware that it will be vulnerable to some fairly basic cryptanalysis.

If you want real encryption, you'll probably have to use a stream cipher like RC4. You can grab 32 bits of keystream and XOR it with your value to encrypt it. As long as you get a new 32 bits of keystream for each value you'll be fine.

RC4 has some caveats, however, so read up on it first.

Block ciphers will not be your friend in this case as they all have block sizes of 64 bits or more. This means you need to pad your 32 bit integer to 64 bits and you'll get 64 bits back out...but you can't choose which 32 to keep. You won't be able to decrypt it with only half the bits. If you're happy to move to longs then you can use 3DES or Blowfish.

It all depends on exactly what you are encrypting and why, so it's hard to give a definitive answer. I hope this gives an idea of where to start, at least.

Cameron Skinner
+1 for 64 bit block cipher. I have a code example of this here: http://stackoverflow.com/questions/3569783/query-string-parameter-obfuscation/3571165#3571165 Also a 64 bit integer represented as hex is only 16 characters long and should be just fine for use in a URL.
Lunatic Experimentalist
+1  A: 

You may look at this paper: Perfect Block Ciphers with Small Blocks and the slides of the presentation at the FSE 2007 conference.

The paper explains how to randomly select a permutation of n elements (e.g. the integer between 0 and n-1) which can be viewed as a cipher for this set of n elements.

Jcs
+1  A: 

The answer I posted to that question applies to yours as well: use a short block cipher. Assuming your identifiers are 64 bits, in fact, you can simply use the XTEA cipher as-is, with a 64 bit integer as the data block.

Nick Johnson
That sounds good enough, and the code for XTEA looks simple enough to be easily ported to python. Thx I'll give this a shot
Infinity