生成可以稍后验证的随机代码

发布于 2024-12-02 05:11:12 字数 622 浏览 0 评论 0原文

我需要生成验证码并将其发送给客户,以便客户稍后可以提供该代码并由我进行验证。我希望能够在不将验证码存储在数据库中的情况下执行此操作,即代码应该是独立的。

如何生成代码并防止客户猜测有效代码是什么?显然,我不能完全排除幸运的猜测,但我希望此类事件的概率相当小,同时保持代码长度较小。代码可以包含数字和字母。

例如,一个非常基本的验证码将是这样的:随机生成数字1122。现在,计算验证码 112261122 并附加其校验和 6=1+1+2+2)。因此,如果客户发回 11226 我就能够验证它,但当然这太容易篡改了。有没有一种方法可以生成这样的代码?

编辑:问题是发送验证码的系统和验证验证码的系统是分开的,这两个系统之间没有信息共享。验证系统甚至无法知道随机生成的数字(示例中的 1122)。我再举个例子:在我的国家,有一个叫做“橙色星期三”的事情。如果您有通过短信接收的代码,您可以以一张的价格获得两张电影票(如果您是 Orange 客户,您可以发送短信并通过短信接收代码)。但是,此代码不与任何内容(电话号码或类似号码)绑定:我可以将该代码提供给其他人,它仍然有效。我需要这样的验证码。

I need to generate a verification code and send it to a customer, so that the customer can then provide the code later and I validate it. I want to be able to do this without storing the verification codes in a database, i.e., the codes should be self-containted.

How can I generate the code and prevent the customer from just guessing what a valid code is? Obviously, I can't completely rule out a lucky guess, but I want the probability of such an event to be reasonably small, while keeping the code length small. A code may contain digits and letters.

For example, a very basic verification code would be like this: randomly generate the number 1122. Now, compute the verification code 11226 ( 1122 and append its checksum 6=1+1+2+2). So, if the customer sends back 11226 back I would be able to validate it, but of course this is too easy to tamper with. Is there a method to generate such codes?

EDIT: The problem is that the system which sends the verification code and the system where the verification code is validated are separated, no information is shared between these two systems. There is no way that even the randomly generated number (1122 in the example) is known to the validation system. Let me give you another example: in my country, there is a thing called "Orange Wednesday". You can get 2 movie tickets for the price of one if you have a code received by SMS (you send a SMS and receive a code by SMS if you are Orange customer). However, this code is not tied to anything (phone number or similar): I can give the code to someone else and it is still valid. I would need this kind of verification code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过期以后 2024-12-09 05:11:12

您正在寻找的内容可以使用消息身份验证代码 (MAC) 来实现:

MAC 算法

需要进行的关键观察是:

  1. 生成和验证 MAC 代码需要密钥(只有您知道)。
  2. 生成的 MAC 代码是防修改的,即攻击者无法判断对 (message, MAC) 元组的给定修改可能产生什么影响。
  3. 除密钥外,无需在消息流之外存储任何信息。

在您的场景中,您可以使用任意消息,因为您唯一的要求是验证从客户收到的输入最初是由您生成的。
实现此目的

  • 您可以通过采用某种模式来 ,例如仅生成以 012345679xxxxxxx 开头的代码,或者简单地将日期编码为“dd.mm.yyyy hh:mm:ss”作为您的“任意消息”。
  • 使用足够长的随机数并存储您实际生成的代码。但是,您似乎正在寻找一种不依赖于数据库的解决方案。

What you are looking for can be implemented using Message Authentication Codes (MAC):

MAC algorithm

The key observations to make are:

  1. The secret key (which only you know) is required to generate and validate MAC codes.
  2. The MAC code produced is tinkering-proof, i.e. an attacker is not able to tell what impact a given modification to the (message, MAC) tuple may have.
  3. No information needs to be stored outside of the message flow, except for the secret key.

In your scenario, you can use any arbitrary message since your only requirement is to verify that the input you receive from a customer was originally generated by you.
You can do this by

  • settling for some kind of pattern, e.g. generate only codes that start with 012345679xxxxxxx, or simply take the date encoded as "dd.mm.yyyy hh:mm:ss" for your 'arbitrary message'.
  • use random numbers that are long enough and store which codes have actually been generated by you. However, you seemed to be looking for a solution that does not depend on databases.
神经大条 2024-12-09 05:11:12

您建议使用校验位。问题在于系统可以很容易地被“弄清楚”并且产生新的工作代码。拥有数据库可以让您跟踪已发送的代码,并确保返回给您的代码实际上已发送。还可以从上述维基百科文章的链接部分查看Luhn 算法

What you are suggesting is the use of a check digit. The problem is that the system can be "figured out" and new working codes produced fairly easily. Having a database would allow you to keep track of which codes have been sent out and to make sure that the codes coming back to you have actually been sent out. Check out also Luhn's algorithm, from the links section of the above Wikipedia article.

赠我空喜 2024-12-09 05:11:12

例如,您可以使用公钥加密技术对客户的姓名进行数字签名。在您的服务器上,您将存储一个私人签名密钥,客户会将其发送给您进行注册。到达那里后,您可以签署并将其发送回给客户。验证代码可以归结为根据您的公钥检查签名。当然,这对于阻止诸如欺骗其他客户信息之类的行为没有任何作用,但至少它可以证明对于任何给定的客户名称,无论您是否授权他们,并且您不需要存储所有客户的庞大数据库(虽然我不明白为什么你不想存储数据库,因为你可能至少需要所有这些数据用于法律/财务记录保存目的。)

You could digitally sign the customer's name (for example) using public key cryptography. On your server, you'd store a private signing key, which the customer would send to you for registration. Once there, you could sign it and send it back to customer. Validating the code would boil down to checking a signature against your public key. Of course this would do nothing to deter stuff like spoofing another customer's information but at least it would certify that for any given customer name, whether you did authorize them -- and you wouldn't need to store a huge database of all your customers (though why you don't want to store a database is beyond me, since you'd probably need all that data for legal/financial record keeping purposes at least.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文