测试Rockey 4 Smart加密锁的.Net C#语言代码

测试Rockey 4 Smart加密锁的.Net C#语言代码

public enum Ry4Cmd : ushort { RY_FIND = 1, //Find Ry4S RY_FIND_NEXT, //Find next RY_OPEN, //Open Ry4S RY_CLOSE, //Close Ry4S RY_READ, //Read Ry4S RY_WRITE, //Write Ry4S RY_RANDOM, //Generate random RY_SEED, //Generate seed RY_READ_USERID = 10, //Read UID RY_CHECK_MODULE = 12, //Check Module RY_CALCULATE1 = 14, //Calculate1 RY_CALCULATE2, //Calculate1 RY_CALCULATE3, //Calculate1 };
public enum Ry4ErrCode : uint { ERR_SUCCESS = 0, //No error ERR_NO_PARALLEL_PORT = 0x80300001, //(0x80300001)No parallel port ERR_NO_DRIVER, //(0x80300002)No drive ERR_NO_ROCKEY, //(0x80300003)No Ry4S ERR_INVALID_PASSWORD, //(0x80300004)Invalid password ERR_INVALID_PASSWORD_OR_ID, //(0x80300005)Invalid password or ID ERR_SETID, //(0x80300006)Set id error ERR_INVALID_ADDR_OR_SIZE, //(0x80300007)Invalid address or size ERR_UNKNOWN_COMMAND, //(0x80300008)Unkown command ERR_NOTBELEVEL3, //(0x80300009)Inner error ERR_READ, //(0x8030000A)Read error ERR_WRITE, //(0x8030000B)Write error ERR_RANDOM, //(0x8030000C)Generate random error ERR_SEED, //(0x8030000D)Generate seed error ERR_CALCULATE, //(0x8030000E)Calculate error ERR_NO_OPEN, //(0x8030000F)The Ry4S is not opened ERR_OPEN_OVERFLOW, //(0x80300010)Open Ry4S too more(>16) ERR_NOMORE, //(0x80300011)No more Ry4S ERR_NEED_FIND, //(0x80300012)Need Find before FindNext ERR_DECREASE, //(0x80300013)Dcrease error ERR_AR_BADCOMMAND, //(0x80300014)Band command ERR_AR_UNKNOWN_OPCODE, //(0x80300015)Unkown op code ERR_AR_WRONGBEGIN, //(0x80300016)There could not be constant in first instruction in arithmetic ERR_AR_WRONG_END, //(0x80300017)There could not be constant in last instruction in arithmetic ERR_AR_VALUEOVERFLOW, //(0x80300018)The constant in arithmetic overflow ERR_UNKNOWN = 0x8030ffff, //(0x8030FFFF)Unkown error ERR_RECEIVE_NULL = 0x80300100, //(0x80300100)Receive null ERR_PRNPORT_BUSY = 0x80300101 //(0x80300101)Parallel port busy };
public class RockeyAPI { //-------------Ry4S.dll--start-------------------- public const string XDLL = "Ry4S.dll"; //Rockey函数 //EXTERN_C __declspec(dllexport) WORD WINAPI Rockey(WORD function, WORD* handle, DWORD* lp1, DWORD* lp2, WORD* p1, WORD* p2, WORD* p3, WORD* p4, BYTE* buffer); [DllImport(XDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "Rockey")] public static extern ulong _Rockey(ushort function, ref ushort handle, ref uint lp1, ref uint lp2, ref ushort p1, ref ushort p2, ref ushort p3, ref ushort p4, ref object buffer); public static ulong Rockey(Ry4Cmd function, ref ushort handle, ref uint lp1, ref uint lp2, ref ushort p1, ref ushort p2, ref ushort p3, ref ushort p4, ref object buffer) { return _Rockey((ushort)function, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref buffer); } }
/// <summary> /// 加密锁Rockey4 Smart /// vp:hsg /// create date:2012-04-26 /// </summary> public class RockeyClass { // private uint lp1 = 0; private uint lp2 = 0; // private ushort p1; private ushort p2; private ushort p3; private ushort p4; // private byte[] buffer = new byte[1024]; private object obbuffer = new object(); private ushort handle = 0; // public string HID = ""; // public bool SetDemoPassword() { bool rbc = false; p1 = 0xc44c; p2 = 0xc8f8; p3 = 0x0799; p4 = 0xc43b; rbc = true; return rbc; } public bool SetPassword(ushort m_p1,ushort m_p2) { bool rbc=false; p1 = m_p1; p2 = m_p2; p3 = 0; p4 = 0; rbc=true; return rbc; } public string GetHex8(uint val) { return string.Format("{0:X8}", val); } public string GetHex4(uint val) { return string.Format("{0:X4}", val); } //查找加密锁 Rockey4 Smart public ulong Find() { //Find Ry4S obbuffer=(object)buffer; ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_FIND,ref handle,ref lp1,ref lp2,ref p1,ref p2,ref p3,ref p4,ref obbuffer); //strRet = string.Format("({0}): {1:X8}", i + 1, uiarrRy4ID[i]); this.HID =GetHex8(lp1); return ret; } //打开当前加密锁 public ulong Open() { ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_OPEN, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); return ret; } //关闭当前加密锁 public ulong Close() { ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_CLOSE, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); return ret; } //写入用户存储区 public ulong Write(ushort pos, ushort len,byte[] m_buffer1024) { p1 = pos; p2 = len; buffer = m_buffer1024; obbuffer = (object)m_buffer1024; ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_WRITE, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); return ret; } //读取用户存储区 public ulong Read(ushort pos, ushort len, ref byte[] m_buffer1024) { p1 = pos; p2 = len; buffer = m_buffer1024; obbuffer = (object)m_buffer1024; ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_READ, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); return ret; } //生成随机数 public ulong Random(ref ushort rdval) { ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_RANDOM, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); rdval = p1; return ret; } //生成种子码 public ulong Seed(uint m_lp2, ref ushort buf_p1, ref ushort buf_p2, ref ushort buf_p3, ref ushort buf_p4) { lp2 = m_lp2; p1 = buf_p1; p2 = buf_p2; p3 = buf_p3; p4 = buf_p4; ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_SEED, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); buf_p1 = p1; buf_p2 = p2; buf_p3 = p3; buf_p4 = p4; return ret; } //读取用户ID public ulong Read_UserID(ref string UID) { ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_READ_USERID, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); UID = this.GetHex8(lp1); return ret; } //检查模块状态 public ulong Check_Module(ushort mod_numid_0_64, ref ushort Validate, ref ushort Decreasable)//RY_CHECK_MODULE { p1 = mod_numid_0_64; ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_CHECK_MODULE, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); Validate = p2; Decreasable = p3; return ret; } //Calculate 1 public ulong Calculate_1(ref uint m_lp1,ref uint m_lp2, ref ushort m_p1,ref ushort m_p2,ref ushort m_p3,ref ushort m_p4) { //Calculate 1,A=1,B=2,C=3,D=4... //lp1 = 0; lp2 = 8; p1 = 1; p2 = 2; p3 = 3; p4 = 4; lp1 = m_lp1; lp2 = m_lp2; p1 = m_p1; p2 = m_p2; p3 = m_p3; p4 = m_p4; ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_CALCULATE1, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); m_p1=p1; m_p2=p2; m_p3=p3; m_p4=p4; return ret; } // //Calculate 2 public ulong Calculate_2(ref uint m_lp1, ref uint m_lp2, ref ushort m_p1, ref ushort m_p2, ref ushort m_p3, ref ushort m_p4) { //Calculate 2,Seed=0x12345678,A=1,B=2,C=3,D=4... //lp1 = 0; lp2 = 0x12345678; p1 = 1; p2 = 2; p3 = 3; p4 = 4; lp1 = m_lp1; lp2 = m_lp2; p1 = m_p1; p2 = m_p2; p3 = m_p3; p4 = m_p4; ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_CALCULATE2, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); m_p1 = p1; m_p2 = p2; m_p3 = p3; m_p4 = p4; return ret; } //Calculate 3 public ulong Calculate_3(ref uint m_lp1, ref uint m_lp2, ref ushort m_p1, ref ushort m_p2, ref ushort m_p3, ref ushort m_p4) { //Calculate 3,A=1,B=2,C=3,D=4... //lp1 = 0; lp2 = 8; p1 = 1; p2 = 2; p3 = 3; p4 = 4; lp1 = m_lp1; lp2 = m_lp2; p1 = m_p1; p2 = m_p2; p3 = m_p3; p4 = m_p4; ulong ret = RockeyAPI.Rockey(Ry4Cmd.RY_CALCULATE3, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); m_p1 = p1; m_p2 = p2; m_p3 = p3; m_p4 = p4; return ret; } //
class Program { static void Main(string[] args) { uint lp1 = 0x00000000; uint lp2 = 0x12345678; ushort p1 = 0x0000; ushort p2 = 0x0000; ushort p3 = 0x0000; ushort p4 = 0x0000; Net.Security.Rockey4Smart.RockeyClass r = new Net.Security.Rockey4Smart.RockeyClass(); // ShowMsg("设置密码"); r.SetDemoPassword(); ulong ret = r.Find(); if (ret>0) { //找到加密锁 ShowMsg("HID=" + r.HID); r.Open(); //获取UserID string UID = ""; r.Read_UserID(ref UID); ShowMsg("UID=" + UID); //获取随机数 ushort rdval=0; r.Random(ref rdval); ShowMsg("Random=" + r.GetHex4(rdval)); //获取种子码 lp2=0x12345678; r.Seed(lp2, ref p1, ref p2, ref p3, ref p4); ShowMsg(string.Format("Seed A,B,C,D={0},{1},{2},{3}", r.GetHex4(p1), r.GetHex4(p2), r.GetHex4(p3), r.GetHex4(p4))); //计算1 lp1 = 0; lp2 = 8; p1 = 1; p2 = 2; p3 = 3; p4 = 4; r.Calculate_1(ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4); ShowMsg(string.Format("计算1 A,B,C,D={0},{1},{2},{3}", r.GetHex4(p1), r.GetHex4(p2), r.GetHex4(p3), r.GetHex4(p4))); //计算2 lp1 = 0; lp2 = 0x12345678; p1 = 1; p2 = 2; p3 = 3; p4 = 5; r.Calculate_2(ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4); ShowMsg(string.Format("计算2 A,B,C,D={0},{1},{2},{3}", r.GetHex4(p1), r.GetHex4(p2), r.GetHex4(p3), r.GetHex4(p4))); //计算3 lp1 = 0; lp2 = 8; p1 = 1; p2 = 2; p3 = 3; p4 = 6; r.Calculate_3(ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4); ShowMsg(string.Format("计算3 A,B,C,D={0},{1},{2},{3}", r.GetHex4(p1), r.GetHex4(p2), r.GetHex4(p3), r.GetHex4(p4))); //关闭加密锁 r.Close(); } } }

转载于:https://www.cnblogs.com/sqlite3/archive/2012/04/26/2566706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值