3

任何人都知道我在哪里可以找到带有 system.security.cryptography 命名空间的示例代码——或者开发人员可以遵循的说明?

目的是为 asp.net 网站添加双重身份验证。在网站上,我想要求用户输入密码(类似于他们从钥匙扣获得密码)。在客户端,我想提供一个生成正确密码的 vb.net windows.forms 程序。

我想在小范围内使用 system.security.cryptography 命名空间来做到这一点。我正在寻找我不想弄乱设备或购买身份验证服务器设备的示例代码。

那里的大多数算法都需要高级数学学位,或者适用于其他平台,例如 Linux 或 PHP。我正在寻找.net 等价物。

4

1 回答 1

1

RFC4226(基于计数器的 OTP)或draft-mraihi-totp-timebased (基于时间的 OTP)的加密部分相对简单:

  1. 根据共享密钥和计数器/时间生成 HMAC
  2. 以安全的方式截断它

通常是用户管理和静态/动态同步使其变得复杂。

像这样的东西应该工作:

public static int CalculateHotp(byte[] key, byte[] counter)
{
    var hmacsha1 = new HMACSHA1(key);
    byte[] hmac_result = hmacsha1.ComputeHash(counter);
    int offset = hmac_result[19] & 0x0f;
    int bin_code = (hmac_result[offset]  & 0x7f) << 24
                   | (hmac_result[offset+1] & 0xff) << 16
                   | (hmac_result[offset+2] & 0xff) <<  8
                   | (hmac_result[offset+3] & 0xff);
    int hotp = bin_code % 1000000;
    return hotp;
}
于 2010-09-13T14:05:39.847 回答