Obscure / encrypt an order number as another number: symmetrical, “random” appearance?

后端 未结 7 506
长情又很酷
长情又很酷 2020-12-13 02:44

Client has an simple increasing order number (1, 2, 3...). He wants end-users to receive an 8- or 9- digit (digits only -- no characters) \"random\" number. Obviously, this

相关标签:
7条回答
  • 2020-12-13 03:21

    Pick a 8 or 9 digit number at random, say 839712541. Then, take your order number's binary representation (for this example, I'm not using 2's complement), pad it out to the same number of bits (30), reverse it, and xor the flipped order number and the magic number. For example:

    1         = 000000000000000000000000000001
    
    Flip      = 100000000000000000000000000000
    839712541 = 110010000011001111111100011101
    XOR       = 010010000011001111111100011101 = 302841629
    
    2         = 000000000000000000000000000010
    
    Flip      = 010000000000000000000000000000
    839712541 = 110010000011001111111100011101
    XOR       = 100010000011001111111100011101 = 571277085
    

    To get the order numbers back, xor the output number with your magic number, convert to a bit string, and reverse.

    0 讨论(0)
提交回复
热议问题