Bijective Function "Integer & # 8596; String"

Here is the problem I'm trying to create a better solution to. I have a finite set of non-negative integers ranging from [0 ... N]. I should be able to represent each number in this set as a string and be able to convert that string back to the original number. Therefore, it should be a bijective function.

Additional requirements:

  • The string representation of the number should wrap the original number, at least to some extent. So a primitive solution like f (x) = x.toString () will not work.
  • String length is important: the less the better.
  • If the string representation of K is known, I would like it to be nontrivial (to some extent) in order to guess the string representation of K + 1.

For p.1 and p.2, the obvious solution is to use something like Base64 (or any other BaseXXX value for all values). But can we fit into p.3 with minimal extra effort? Common sense tells me that I additionally need the bijective function String โ†” String for BaseXXX values. Any suggestions? Or maybe there is something better than BaseXXX to meet all three requirements?

+5
source share
5 answers

If you do not want this to be too secure, you can simply use a simple symmetric cipher after encoding in BaseXXX. For example, you can select the sequence of keys of integers [nโ‚, nโ‚‚, nโ‚ƒ ...], and then use the Vigenere cipher .

- C C + K (mod 26), K - . , , , .

: baseXXX, , , . mod 26 mod N + 1.

, xor . ( Vigenere.) , .

+1

1-3, , , :

  • p > N+2,
  • g p, .. , p p-1
  • 0 <= k <= N, enc(k) = min {j > 0 : g^j == (k+2) (mod p)}
  • f(k) = enc(k).toString()
+1

M. 0 M-1 . base-M, , . .

M=26 . M=256 .

!

+1

, , , str (K + 1), str (K)?

How about doing f(x) = (x + a).toString()where ais secret? Then the external user cannot determine xfrom f(x), but they can be sure that if they have the string "1234", say, for the unknown x, then "1235" is displayed on x+1.

0
source

R. 1 and p. 3 slightly contradict each other and a little vague.

I would suggest using the hexadecimal representation of integers.

17 => 0x11
123123 => 1E0F3
0
source

All Articles