- mapping numbers to 'random' numbers and back
- Posted by JdV on August 11th, 2006
Hello,
I am looking for a simple algorithm to map small integers (4 digits,
0000-1000) to large random-looking numbers in a certain range (5 digits,
10000-99999). For example (just with some made-up numbers), 1 -> 62521,
2 -> 30162, and I need to be able to map the big numbers back to their
originals (30162 -> 2, etc).
Any tips on a method or algorithm to use for this ?
Thank you,
- Posted by osmium on August 11th, 2006
"JdV" writes:
Since one of the numbers is random I see no alternative to keeping a copy of
the mapping.that was used. How about storing all the mappings used in an
array? You can do the "reversal" thing by using a hash table based on the
big number.
- Posted by Richard Heathfield on August 11th, 2006
JdV said:
Given that you need a reversible mapping, I suggest you just pick two or
three reversible operations and do them one after the other. For example:
unsigned long hash(unsigned int n)
{
unsigned long h = n << 4;
h ^= 0x01234567;
return h - 42;
}
unsigned int unhash(unsigned long h)
{
unsigned long n = h + 42;
n ^= 0x01234567;
return n >> 4;
}
I haven't checked whether hash() produces numbers in the range you're after,
but it hardly matters - just fiddle with it till it does, and do the
opposite fiddling in unhash().
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
- Posted by rossum on August 11th, 2006
On 11 Aug 2006 13:15:03 GMT, JdV <usenet@zevv.nl> wrote:
1 Do you want to change the mapping every so often or is it permanent?
2 I assume you are doing this for security purposes. Do you want to
stop Aunt Edna reading it or do you want to stop Nasty Megacorp Inc
(with a lot of money) reading it?
If the mapping is permanent then a look-up table will do all that you
want.
If you want to change the mapping every so often then you will need
some sort of keyed mapping. One option is to take your four digits,
add a spurious digit for padding and shift each of the five digits by
some number mod 10.
For example:
Coding
Number: 1234
Add padding: 12345
Shift by: 81823
Result: 93168
(1+8 = 9, 2+1 = 3, 3+8 = 1, 4+2 = 6, 5+3 = 8, all mod 10)
Decoding
Cyphertext: 93168
Backshift by: 81823
Gives: 12345
(9-8 = 1, 3-1 = 2, 1-8 = 3, 6-2 = 4, 8-3 = 5, all mod 10)
Remove padding to give: 1234
How you generate the shift digits (81823) depends on who you are
hiding it from. For Aunt Edna then rand() is good enough, just pick a
key to reseed rand() so you get the same shift numbers for coding and
decoding. For Nasty Megacorp then you should use something
cryptographically secure, probably AES in CTR mode with the output
modified to produce results in the range 0-9 rather than 0-255. With
the second option you are going to have to get into all sorts of
cryptographic complications like nonces, securing the key and the
rest. That will give you some serious cryptographic work to do if you
go down that route.
rossum