Well, here's an attempt at a much better algorithm than the ones described before:
Take a generic function for hiding information, F(key,data)
. This can either be constructed from a secure hash function H(data)
as F(key,data)=H(data+key)
or a from a symmetric cipher function C(key,block)
as F(key,data)=C(key,truncate(data))
.
Next, initialize a block of data B
as either zeroes, or some fixed starting value (this should not matter much). Also initialize an integer I
as the integer-encoded IP address.
Then, for N
going from 1 to 32:
Extract the bit I_N
(the Nth bit, counting from 1, starting at the MSB) of the integer-encoded IP. Calculate and set B = H(key,B)
. If bit 0 (or some other arbitarily selected bit) of B
is 1, invert bit N
of the integer I
. Finally, set B = I_N+B
.
I
will now be the ID code corresponding to the IP. It will have the properties that if two different IPs have the same N first bits, the corresponding ID codes will also have the same first N bits. Furthermore, there is a one-to-one mapping between IPs and IDs (this was not true of the earlier algorithms, where several nearby IPs could have the same ID). Finally, if you know the key
, the encoding is reversible - you can do this with almost the same algorithm, moving the extraction of I_N
to the end of the algoirthm.
People familiar with crypto may notice that this is very similar to a cipher working in CFB mode on single bits.