将订单号隐藏/加密为另一个数字:对称、“随机";外貌? [英] Obscure / encrypt an order number as another number: symmetrical, "random" appearance?

查看:33
本文介绍了将订单号隐藏/加密为另一个数字:对称、“随机";外貌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户端有一个简单的递增顺序号(1、2、3...).他希望最终用户收到一个 8 位或 9 位(仅限数字——无字符)的随机"数字.显然,这个随机"数实际上必须是唯一且可逆的(它实际上是对实际订单号的加密).

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 "random" number actually has to be unique and reversible (it's really an encryption of the actualOrderNumber).

我的第一个想法是洗牌一些位.当我向客户展示样本序列时,他抱怨随后的 obfuscOrderNumbers 一直在增加,直到它们达到洗牌"点(低位开始发挥作用的点).他希望 obfuscOrderNumbers 看起来尽可能随机.

My first thought was to just shuffle some bits. When I showed the client a sample sequence, he complained that subsequent obfuscOrderNumbers were increasing until they hit a "shuffle" point (point where the lower-order bits came into play). He wants the obfuscOrderNumbers to be as random-seeming as possible.

我的下一个想法是确定性地为线性同余伪随机数生成器设置种子,然后获取 actualOrderNumber th 值.但在那种情况下,我需要担心冲突——客户想要一种保证在至少 10^7 个周期内不会发生冲突的算法.

My next thought was to deterministically seed a linear congruential pseudo-random-number generator and then take the actualOrderNumber th value. But in that case, I need to worry about collisions -- the client wants an algorithm that is guaranteed not to collide in at least 10^7 cycles.

我的第三个想法是嗯,只是加密该死的东西",但如果我使用股票加密库,我必须对其进行后处理才能获得仅 8 或 9 位数字的要求.

My third thought was "eh, just encrypt the darn thing," but if I use a stock encryption library, I'd have to post-process it to get the 8-or-9 digits only requirement.

我的第四个想法是将 actualOrderNumber 的位解释为一个格雷编码的整数并返回它.

My fourth thought was to interpret the bits of actualOrderNumber as a Gray-coded integer and return that.

我的第五个想法是:我可能想多了.我敢打赌 StackOverflow 上的某个人可以用几行代码做到这一点."

My fifth though was: "I am probably overthinking this. I bet someone on StackOverflow can do this in a couple lines of code."

推荐答案

随机选择一个 8 位或 9 位数字,比如 839712541.然后,取订单号的二进制表示(对于这个例子,我没有使用 2 的补码),将其填充到相同的位数 (30),将其反转,然后对翻转的顺序数和幻数进行异或.例如:

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.

这篇关于将订单号隐藏/加密为另一个数字:对称、“随机";外貌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆