Integers from 0 to M are to be mapped to n-character codes, made up from a certain alphabet.
The tricky part is that the codes should look pseudo-random, non-sequential. Like so:
0 BX07SU
1 TYN9RQ
2 QZ1697
I assume it’s a common knowledge piece that I miss. How do they call this type of functions f(i) = s – which maps an integer to a pseudo-random character string, with no collisions within a certain range?
Reverse function would be great too: h(s) = i, able to ‘decode’ a valid string back into an integer, or determine that the supplied string is invalid.
For small
M@user1494736 is right: just make a lookup table that happens to be bijective, and tailor it to your needs.For large
Mstoring the table becomes impractical; we need an algorithm to calculate the mapping. You mentioned we don’t need strong cryptography; this frees us to choose something more light-weight than standard ciphers. That said, they are quite fast and I expect you could use one easily with a 3rd-party library. But let’s suppose you prefer to avoid this for some reason.One way to make a poor man’s cipher is to
xora constant onto your plaintext (to change the Hamming weight), and then apply a bit permutation (to destroy locality.) Both of these steps are invertible. It sounds like you want to display your cipher letters as ASCII. So, we may want a final invertible transform (an addition) to ensure the cipher values are printable.You mentioned
Mmight be as large as a few million. So, let’s take your plaintext letters (and so ciphertext letters) to be 32-bit values. Finding a random 32-bit value toxoris easy. How about the bit permutation?A few million plaintext letters translates into each letter being around
22or23bits wide; this leaves at least8(logical) upper bits cleared in every plaintext letter. The permutation can take advantage of this to help ensure the ciphertext bytes are within the ASCII printable range. By sending that upper plaintext byte to the upper two bits of each of the four cipher bytes, we ensure each cipher byte takes values0to63. The final step could then add48to make the range48to111, well within the ASCII printable range.Playing off this observation, we could envision our plaintext letter as a
4x4grid of bit pairs, and make our permutation a rotation of this grid:Or put another way:
Notice that a piece of each plaintext byte appears in every ciphertext byte: this is what will ensure the encoding “looks random”.