Rot13的数字 [英] Rot13 for numbers

查看:153
本文介绍了Rot13的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rot13 的想法是掩盖文字,例如防止扰流者。这并不意味着加密安全,只是确保只有确定他们想要阅读的人才会阅读它。

The idea of rot13 is to obscure text, for example to prevent spoilers. It's not meant to be cryptographically secure but to simply make sure that only people who are sure they want to read it will read it.

我想做一些类似的事情数量,涉及密封投标的申请。大概我想派一个我的号码,并相信他们选择自己的号码,不受我的影响,但是当他们准备就绪时,他们应该能够透露我的(纯客户端)。他们不应该要求我或任何第三方的进一步投入。

I'd like to do something similar for numbers, for an application involving sealed bids. Roughly I want to send someone my number and trust them to pick their own number, uninfluenced by mine, but then they should be able to reveal mine (purely client-side) when they're ready. They should not require further input from me or any third party.

(添加:请注意收件人被信任不要欺骗的假设。)

(Added: Note the assumption that the recipient is being trusted not to cheat.)

它不是简单的rot13因为某些数字,如1和2,将会经常复发,你可能会记得,例如,34.2是真的1。

It's not as simple as rot13 because certain numbers, like 1 and 2, will recur often enough that you might remember that, say, 34.2 is really 1.

这是我正在寻找具体来说:

Here's what I'm looking for specifically:

将实数映射到实数(或字符串)的函数seal()。它应该不是是确定性的 - 印章(7)不应该每次都映射到相同的东西。但是相应的函数unseal()应该是确定性的 - 对于所有的x来说,unseal(seal(x))应等于x。我不想打印或打开任何网络服务或甚至获得系统时间(因为我不想假定同步时钟)。 (添加:假设所有出价都会小于某个最大值,所有人都知道,百万)。

A function seal() that maps a real number to a real number (or a string). It should not be deterministic -- seal(7) should not map to the same thing every time. But the corresponding function unseal() should be deterministic -- unseal(seal(x)) should equal x for all x. I don't want seal or unseal to call any webservices or even get the system time (because I don't want to assume synchronized clocks). (Added: It's fine to assume that all bids will be less than some maximum, known to everyone, say a million.)

理性检查:

> seal(7)
482.2382   # some random-seeming number or string.
> seal(7)
71.9217    # a completely different random-seeming number or string.
> unseal(seal(7))
7          # we always recover the original number by unsealing.


推荐答案

这是一个灵感来自Svante答案的解决方案。 >

Here's a solution inspired by Svante's answer.

M = 9999  # Upper bound on bid.
seal(x) = M * randInt(9,99) + x
unseal(x) = x % M

理性检查:

> seal(7)
716017
> seal(7)
518497
> unseal(seal(7))
7

这需要调整才能允许负面投标:

This needs tweaking to allow negative bids though:

M = 9999  # Numbers between -M/2 and M/2 can be sealed.
seal(x) = M * randInt(9,99) + x
unseal(x) = 
  m = x % M; 
  if m > M/2 return m - M else return m

这个解决方案的一个好东西是多么微不足道收件人解码 - 只是模拟9999(如果这是5000或更多,那么它是一个负的出价,减去另一个9999)。这也是很好,隐藏的出价将最多6位数字。 (这是我所想到的很多安全性 - 如果出价可能会超过$ 5k,那么我会使用更安全的方法。尽管这个方法的最高出价当然可以设置得高一些。)

A nice thing about this solution is how trivial it is for the recipient to decode -- just mod by 9999 (and if that's 5000 or more then it was a negative bid so subtract another 9999). It's also nice that the obscured bid will be at most 6 digits long. (This is plenty security for what I have in mind -- if the bids can possibly exceed $5k then I'd use a more secure method. Though of course the max bid in this method can be set as high as you want.)

选择9到99之间的数字,并将其乘以9999,然后添加出价。
这将产生一个5或6位数的编码您的出价。
要解开它,除以9999,减去小数点左边的部分,然后乘以9999.
(这是儿童和数学家所知的找到余数除以9999或mod'ing by 9999)

Pick a number between 9 and 99 and multiply it by 9999, then add your bid. This will yield a 5 or 6-digit number that encodes your bid. To unseal it, divide by 9999, subtract the part to the left of the decimal point, then multiply by 9999. (This is known to children and mathematicians as "finding the remainder when dividing by 9999" or "mod'ing by 9999", respectively.)

这适用于小于9999的非负数出价(如果还不够,请使用99999或数量尽可能多)。
如果要允许负面出价,那么魔术9999数字需要是最大出价的两倍。
解码时,如果结果大于9999的一半,即5000或以上,则减去9999以获得实际(负)出价。

This works for nonnegative bids less than 9999 (if that's not enough, use 99999 or as many digits as you want). If you want to allow negative bids, then the magic 9999 number needs to be twice the biggest possible bid. And when decoding, if the result is greater than half of 9999, ie, 5000 or more, then subtract 9999 to get the actual (negative) bid.

再次注意,这是在荣誉系统上:没有什么技术上阻止你一看到它就开启对方的号码。

Again, note that this is on the honor system: there's nothing technically preventing you from unsealing the other person's number as soon as you see it.

这篇关于Rot13的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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