32
|
Emre Yazici · 技术社区 · 14 年前 |
![]() |
1
33
Use any 32-bit block cipher! By definition, a block cipher maps every possible input value in its range to a unique output value, in a reversible fashion, and by design, it's difficult to determine what any given value will map to without the key. Simply pick a key, keep it secret if security or obscurity is important, and use the cipher as your transformation. For an extension of this idea to non-power-of-2 ranges, see my post on Secure Permutations with Block Ciphers . Addressing your specific concerns:
|
![]() |
2
6
我将尝试在一个更简单的例子上解释我的解决方案,然后可以很容易地扩展到您的大例子中。 假设我有一个4位的数字。有16个不同的值。把它看成是一个四维立方体: 4 dimensional cube http://www.ams.org/featurecolumn/images/january2009/klee8.jpg . Every vertex represents one of those numbers, every bit represents one dimension. So its basicaly XYZW, where each of the dimensions can have only values 0 or 1. Now imagine you use a 不同顺序 尺寸的For example XZYW. Each of the vertices now changed its number! You can do this for any number of dimensions, just permute those dimensions. If security is not your concern this could be a nice fast solution for you. On the other hand, i dont know if the output will be "obscure" enough for your needs and certainly after a large amount of mapping done, the mapping can be reversed (which may be an advantage or disadvantage, depending on your needs.) |
![]() |
3
5
The following paper gives you 4 or 5 mapping examples, giving you functions rather than building mapped sets: www.cs.auckland.ac.nz/~john-rugis/pdf/BijectiveMapping.pdf |
![]() |
4
4
Apart from generating random lookup-tables, you can use a combination of functions:
|
![]() |
5
3
If your goal is simply to get a seemingly random permutation of numbers of a 粗略地 defined size, then there is another possible way: reduce the set of numbers to a prime number. Then you can use a mapping of the form f(i) = (i * a + b) % p and if p is indeed a prime, this will be a bijection for all a != 0 and all b. It will look fairly random for larger a and b. For example, in my case for which I stumbled on this question, I used 1073741789 as a prime for the range of numbers smaller than 1 << 30. That makes me lose only 35 numbers, which is fine in my case. My encoding is then
and the decoding is
Note that 507371178 * 233233408 % 1073741789 == 1, so those two numbers are inverse the field of numbers modulo 1073741789 (you can figure out inverse numbers in such fields with the extended euclidean algorithm). 我选择A和B相当随意,我只是确定它们大约是P的一半。 |
![]() |
6
1
Can you use a random generated lookup-table? As long as the random numbers in the table are unique, you get a bijective mapping. It's not symmetric, though. One 16 GB lookup-table for all 32 bit values is probably not practical, but you could use two separate 16-bit lookup tables for the high-word and the low word. PS: I think you can generate a symmetric bijective lookup table, if that's important. The algorithm would start with an empty LUT:
Pick the first element, assign it a random mapping. To make the mapping symmetric, assign the inverse, too:
Pick the next number, again assign a random mapping, but pick a number that's not been assigned yet. (i.e. in this case, don't pick 1 or 3). Repeat until the LUT is complete. This should generate a random bijective symmetric mapping. |
![]() |
7
1
Take a number, multiplies by 9, inverse digits, divide by 9.
Should be obscure enough !! Edit : it is not a bijection for 0 ending integer
You can always add a specific rule like : Take a number, divide by 10 x times, multiplies by 9, inverse digits, divide by 9, multiples by 10^x. 如此
W00它工作! Edit 2 : For more obscurness, you can add an arbitrary number, and substract at the end.
|
![]() |
8
1
Here is my simple idea: You can move around the bits of the number, as PeterK proposed, but you can have a different permutation of bits for each number, and still be able to decipher it.
The cipher goes like this:
Treat the input number as an array of bits
Now this will not work well, as you may take the same bit twice. In order to avoid it, renumber the bits after each iteration, omitting the used bits. To generate the position from which to take
To illustrate this, I'll give an example: 我们有一个4位数字和8个随机数:25、5、28、19、14、20、0、18。
25 mod 4=1,所以我们取位置为1的位(从0开始计数)
We've just taken a bit of value 1, so we skip one random number and use 28. There are 3 bits left, so to count position we take 28 mod 3 = 1. We take the first (counting from 0) of the remaining bits:
Again we skip one number, and take 14. 14 mod 2 = 0, so we take the 0th bit:
Now it doesn't matter, but the previous bit was 0, so we take 20. 20 mod 1=0:
就是这样。 Deciphering such a number is easy, one just has to do the same things. The position at which to place the first bit of the code is known from the key, the next positions are determined by the previously inserted bits. This obviously has all the disadvantages of anything which just moves the bits around (for example 0 becomes 0, and MAXINT becomes MAXINT), but is seems harder to find how someone has encrypted the number without knowing the key, which has to be secret. |
![]() |
9
1
如果你不想使用适当的密码算法(也许是出于性能和复杂性的原因),你可以使用一个更简单的密码,比如 Vigenère cipher . This cipher was actually described as L.CHIFRE工业株式会社 (法语为“牢不可破的密码”)。 这里是一个简单的C实现,它根据相应的键值移位值:
当输入稍微改变时,该算法不会在输出中产生大的移位。但是,您可以使用另一个双射操作而不是加法来实现这一点。 |
![]() |
10
0
Draw a large circle on a large sheet of paper. Write all the integers from 0 to MAXINT clockwise from the top of the circle, equally spaced. Write all the integers from 0 to MININT anti-clockwise, equally spaced again. Observe that MININT is next to MAXINT at the bottom of the circle. Now make a duplicate of this figure on both sides of a piece of stiff card. Pin the stiff card to the circle through the centres of both. Pick an angle of rotation, any angle you like. Now you have a 1-1 mapping which meets some of your requirements, but is probably not obscure enough. Unpin the card, flip it around a diameter, any diameter. Repeat these steps (in any order) until you have a bijection you are happy with. If you have been following closely it shouldn't be difficult to program this in your preferred language.
For Clarification
下面的评论:如果你只对纸旋转卡片,那么方法和你抱怨的一样简单。但是,当您翻转卡片时,映射不等于
|
![]() |
11
0
Split the number in two (16 most significant bits and 16 least significant bits) and consider the bits in the two 16-bit results as cards in two decks. Mix the decks forcing one into the other.
So if your initial number is
If you look at the decimal representation of the results, the series looks pretty obscure. You can manually map 0 -> maxvalue and maxvalue -> 0 to avoid them mapping onto themselves. |