首页 > 解决方案 > OtpSharp 不与谷歌身份验证器一起使用

问题描述

我一直在尝试在我正在开发的应用程序中使用 OtpSharp 和 Google Authenticator。但是,我不明白为什么 OtpSharp 生成的代码与 Google Authenticator 的代码不匹配。我什至尝试根据我的本地操作系统更正 OtpSharp 的时间输入,但没有任何运气。另一方面,python 中的 pyotp 库可以正常工作,无需任何特别的努力。这是我正在使用的代码:

var bSharedKey = System.Text.Encoding.Unicode.GetBytes("TESTTESTTESTTEST");
//var correction = new TimeCorrection(DateTime.UtcNow.ToLocalTime());
//var totp = new Totp(bSharedKey, timeCorrection: correction);
var totp = new Totp(bSharedKey);
var realOtp = totp.ComputeTotp();
long timestep = 0;
var OTPmatch = totp.VerifyTotp(passwords[1], out timestep);

标签: c#pythonone-time-passwordgoogle-authenticator

解决方案


问题是,不是为 pyotp 库(以及 Google Authenticator)提供任意 unicode 密钥,而是需要一个 Base32 字符串作为输入,我假设它后来被解码为字节数组并由库使用。

因此,我向 OtpSharp 提供了任意 unicode 字符串的字节字符串表示形式,并使用在线网站将 unicode 字符串解码为 base32 字符串,并在 Google Authenticator 中使用了 base32 字符串。

简单来说,Otpsharp 需要一个字节数组来初始化一个 totp 对象,而 pyotp 需要你提供一个 base32 字符串。


推荐阅读