跳到主要内容

C#生成GoEasy-OTP

C#生成GoEasy-OTP

    public string goEasyOTP(string secretKey)
{
string otp = null;
byte[] encrypted = null;
string currentTimeMills=string.Format("000{0}",(long)(DateTime.UtcNow-new
DateTime(1970,1,1,0,0,0,DateTimeKind.Utc)).TotalMilliseconds);
//"0001490325990593"
byte[] byteSecretKey = Encoding.ASCII.GetBytes(secretKey);
using (AesManaged aesAlg = new AesManaged(){Key = byteSecretKey,Mode = CipherMode.ECB,Padding = PaddingMode.None})
{
ICryptoTransform encryptor = aesAlg.CreateEncryptor();
using (MemoryStream msEncrypt = new MemoryStream())
{
using(CryptoStream csEncrypt = new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write))
{
using(StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(currentTimeMills);
}
encrypted = msEncrypt.ToArray();
}
}
}
otp = Convert.ToBase64String(encrypted);
return otp;
}

验证OTP生成结果

测试参数:

secret key:86726e4356dce2d3

系统毫秒数:1490325990593

测试结果:

GoEasy-otp:+rOKqbTZioistsdMrhon0A==