实例介绍
【实例简介】OTP
【实例截图】
【核心代码】
winform版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; /* ============================================================================= * Function : ET_CheckPwdz201 * Description: OTP Z201(TOTP) 认证接口 * Parameter : * authkey 令牌密钥 * t 当前时间相对UTC Epoch秒数 * t0 起始参考时间相对UTC Epoch秒数(默认为0) * x TOTP变化周期(默认为60秒) * drift 漂移次数 * authwnd 认证范围, 通常是0-20 * lastsucc 前一次认证成功的相对UTC Epoch秒数(为防止重放攻击) * otp 需要认证的动态口令 * otplen 需要认证的动态口令长度, 通常是6 * currsucc 认证成功后的相对UTC Epoch秒数 * currdft 认证成功后的当前漂移次数 * * return : 0 - 成功, 其他值为错误. * *int __stdcall ET_CheckPwdz201(char *authkey, uint64_t t, uint64_t t0, * unsigned int x, int drift, int authwnd, uint64_t lastsucc, * const char *otp, int otplen, uint64_t *currsucc, int *currdft); */ /* ============================================================================= * Function : ET_Syncz201 * Description: OTP Z201(TOTP) 同步接口 * Parameter : * authkey 令牌密钥,已经加密过的,需要对其进行解密 * t 当前时间相对UTC Epoch秒数 * t0 起始参考时间相对UTC Epoch秒数(默认为0) * x TOTP变化周期(默认为60秒) * drift 漂移次数 * syncwnd 同步范围, 通常是0-20 * lastsucc 前一次认证成功的相对UTC Epoch秒数(为防止重放攻击) * otp1 需要同步的第一个动态口令 * otp1len 需要同步的第一个动态口令长度, 通常是6 * otp2 需要同步的第二个动态口令 * otp2len 需要同步的第二个动态口令长度, 通常是6 * currsucc 认证成功后的相对UTC Epoch秒数 * currdft 认证成功后的当前漂移次数 * * return : 0 - 成功, 其他值为错误. *int __stdcall ET_Syncz201(char *authkey, uint64_t t, uint64_t t0, * unsigned int x, int drift, int syncwnd, uint64_t lastsucc, * const char *otp1, int otp1len, const char *otp2, int otp2len, * uint64_t *currsucc, int *currdft); */ namespace Test { public partial class Form1 : Form { [DllImport( "ET_OTPVerify.dll" )] public static extern int ET_CheckPwdz201( string authkey, UInt64 t, UInt64 t0, uint x, int drift, int authwnd, UInt64 lastsucc, string otp, int otplen, ref UInt64 currsucc, ref int currdft); [DllImport( "ET_OTPVerify.dll" )] public static extern int ET_Syncz201( string authkey, UInt64 t, UInt64 t0, uint x, int drift, int syncwnd, UInt64 lastsucc, string otp1, int otp1len, string otp2, int otp2len, ref UInt64 currsucc, ref int currdft); UInt64 currsucc1 = 0; int currdft1 = 0; public Form1() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { string otp1, otp2; otp1 = this .textBox2.Text; otp2 = this .textBox3.Text; int iRe = 0; string authkey = "ll598C28E54BF3C58EC35E830655A139861407519FCBC92140" ; UInt64 currsucc = 0; int currdft = 0; //测试z201,时间OTP认证 TimeSpan tsTimeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1); ulong ulgTimeStamp = ( ulong )tsTimeSpan.TotalSeconds; iRe = ET_Syncz201(authkey, ulgTimeStamp, 0, 60, currdft1, 60, currsucc1, otp1, 6, otp2, 6, ref currsucc, ref currdft); if (iRe == 0) { MessageBox.Show( "同步成功!" ); currsucc1 = currsucc; currdft1 = currdft; } else { MessageBox.Show( "同步失败!" ); } } private void button3_Click( object sender, EventArgs e) { Application.Exit(); } private void button2_Click( object sender, EventArgs e) { string otp; otp = this .textBox1.Text; int iRe = 0; string authkey = "ll598C28E54BF3C58EC35E830655A139861407519FCBC92140" ; UInt64 currsucc = 0; int currdft = 0; TimeSpan tsTimeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1); ulong ulgTimeStamp = ( ulong )tsTimeSpan.TotalSeconds; iRe = ET_CheckPwdz201(authkey, ulgTimeStamp, 0, 60, currdft1, 40, currsucc1, otp, 6, ref currsucc, ref currdft); if (iRe == 0) { MessageBox.Show( "认证成功!" ); currsucc1 = currsucc; currdft1 = currdft; } else { MessageBox.Show( "认证失败!" ); } } private void textBox1_KeyPress( object sender, KeyPressEventArgs e) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8) e.Handled = true ; } private void textBox2_KeyPress( object sender, KeyPressEventArgs e) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8) e.Handled = true ; } private void textBox3_KeyPress( object sender, KeyPressEventArgs e) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8) e.Handled = true ; } private void Form1_Load( object sender, EventArgs e) { } } } |
asp.net版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | <%@ Page Language="C#" Description="ET99 Demo ---ASP.NET" %> <%@ Import Namespace="System"%> <%@ Import Namespace="System.IO"%> <%@ Import Namespace="System.Runtime.InteropServices"%> < script language = "c#" runat = server > //by Liang Zhengyi //Date: 2011/6/2 //otp Function /* ============================================================================= * Function : ET_CheckPwdz201 * Description: OTP Z201(TOTP) 认证接口 * Parameter : * authkey 令牌密钥(令牌提供商提供) //应存入数据库中,使用时select * t 当前时间相对UTC Epoch秒数 * t0 起始参考时间相对UTC Epoch秒数(默认为0) * x TOTP变化周期(默认为60秒) * drift 漂移值(用于时间校准) //数据库中取,第一次给0. * authwnd 认证范围, 通常是0-20 * lastsucc 前一次认证成功的相对UTC Epoch秒数(为防止重放攻击) //数据库中取,第一次给0. * otp 需要认证的动态口令 * otplen 需要认证的动态口令长度, 通常是6 * currsucc 认证成功后的相对UTC Epoch秒数 //认证成功后应写入数据库,供下次调用。 * currdft 认证成功后的当前漂移次数 //认证成功后应写入数据库,供下次调用。 * * return : 0 - 成功, 其他值为错误. * *int __stdcall ET_CheckPwdz201(char *authkey, uint64_t t, uint64_t t0, * unsigned int x, int drift, int authwnd, uint64_t lastsucc, * const char *otp, int otplen, uint64_t *currsucc, int *currdft); */ /* ============================================================================= * Function : ET_Syncz201 * Description: OTP Z201(TOTP) 同步接口 * Parameter : * authkey 令牌密钥,已经加密过的,需要对其进行解密 * t 当前时间相对UTC Epoch秒数 * t0 起始参考时间相对UTC Epoch秒数(默认为0) * x TOTP变化周期(默认为60秒) * drift 漂移值 * syncwnd 同步范围, 通常是0-40 * lastsucc 前一次认证成功的相对UTC Epoch秒数(为防止重放攻击) * otp1 需要同步的第一个动态口令 * otp1len 需要同步的第一个动态口令长度, 通常是6 * otp2 需要同步的第二个动态口令 * otp2len 需要同步的第二个动态口令长度, 通常是6 * currsucc 认证成功后的相对UTC Epoch秒数 //同步成功后应写入数据库,供下次调用。 * currdft 认证成功后的当前漂移次数 //同步成功后应写入数据库,供下次调用。 * * return : 0 - 成功, 其他值为错误. *int __stdcall ET_Syncz201(char *authkey, uint64_t t, uint64_t t0, * unsigned int x, int drift, int syncwnd, uint64_t lastsucc, * const char *otp1, int otp1len, const char *otp2, int otp2len, * uint64_t *currsucc, int *currdft); */ //请将ET_OTPVerify.dll拷贝到SYSTEM32目录,方便调用。 [DllImport("ET_OTPVerify.dll")] public static extern int ET_CheckPwdz201(string authkey, UInt64 t, UInt64 t0, uint x, int drift, int authwnd, UInt64 lastsucc, string otp, int otplen, ref UInt64 currsucc, ref int currdft); [DllImport("ET_OTPVerify.dll")] public static extern int ET_Syncz201(string authkey, UInt64 t, UInt64 t0, uint x, int drift, int syncwnd, UInt64 lastsucc, string otp1, int otp1len, string otp2, int otp2len, ref UInt64 currsucc, ref int currdft); UInt64 currsucc1 = 0; int currdft1 = 0; int test_auth(string otpkey, string otp) { int iRet = 0; string authkey = otpkey; //令牌密钥,应从服务器端的数据库中检索得到。此处为了方便测试,直接从客户端获取。 UInt64 currsucc = 0; int currdft = 0; TimeSpan tsTimeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1); ulong ulgTimeStamp = (ulong)tsTimeSpan.TotalSeconds; iRet = ET_CheckPwdz201(authkey, ulgTimeStamp, 0, 60, currdft1, 40, currsucc1, otp, 6, ref currsucc, ref currdft); if (iRet == 0) { Message.Text ="< br >认证成功!"; currsucc1 = currsucc; //认证成功后应将“成功值”写回数据库,供接口调用。失败不要写回数据库。 currdft1 = currdft; //认证成功后应将“漂移值”写回数据库,供接口调用。失败不要写回数据库。 Message.Text = "< br > otp: " otp; Message.Text = "< br > currsucc: " currsucc; Message.Text = "< br > currdft: " currdft; } else { Message.Text = "认证失败!"; } return iRet; } int test_sync(string otpkey, string otp1, string otp2) { int iRet = 0; string authkey = otpkey; //令牌密钥,应从服务器端的数据库中检索得到。 UInt64 currsucc = 0; int currdft = 0; TimeSpan tsTimeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1); ulong ulgTimeStamp = (ulong)tsTimeSpan.TotalSeconds; iRet = ET_Syncz201(authkey, ulgTimeStamp, 0, 60, currdft1, 60, currsucc1, otp1, 6, otp2, 6, ref currsucc, ref currdft); if (iRet == 0) { Message.Text ="< br >同步成功!"; currsucc1 = currsucc; //同步成功后应将“成功值”写回数据库,供接口调用。失败不要写回数据库。 currdft1 = currdft; //同步成功后应将“漂移值”写回数据库,供接口调用。失败不要写回数据库。 Message.Text = "< br > otp1: " otp1; Message.Text = "< br > otp2: " otp2; Message.Text = "< br > currsucc: " currsucc; Message.Text = "< br > currdft: " currdft; } else { Message.Text = "同步失败!"; } return iRet; } </ script > <% string demoType = Request.Form["demoType"]; if(demoType == "auth") { string otpkey = Request.Form["otpkey"]; string otp = Request.Form["otp"]; Message.Text = "otpkey: " otpkey "< br >"; int Result = test_auth(otpkey, otp); if (Result == 0) Message.Text = "< br > Congratulations! Authenticate OK!"; else Message.Text = "< br >Sorry ,maybe your password is not correct! " "< br >ErrorCode: " Result; } if(demoType == "syn") { string otpkey = Request.Form["otpkey"]; string otp1 = Request.Form["otp1"]; string otp2 = Request.Form["otp2"]; Message.Text = "otpkey: " otpkey "< br >"; int Result = test_sync(otpkey, otp1, otp2); if (Result == 0) Message.Text = "< br > Synchronous OK!"; else Message.Text = "< br >Sorry ,maybe your password is not correct! " "< br >ErrorCode: " Result; } %> < html > < body > < asp:label id = "Message" forecolor = "red" font-bold = "true" runat = server />< br > </ body > </ html > |
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论