private static byte[] DonglekeyWordToBuffer(string keyword) { if (keyword.Length != PASSWORDLEN * 2) { keyword = keyword.PadLeft(PASSWORDLEN * 2, '0'); } byte[] keys = new byte[PASSWORDLEN]; byte[] Lastkeys = new byte[PASSWORDLEN]; SENSE4_CONTEXT stS4Ctx = new SENSE4_CONTEXT(); uint dwResult = 0; uint pdwBytesReturned = 0; if (Common.OpenS4ByIndex(FIRST_S4_INDEX, ref stS4Ctx) != BYHX_SL_RetValue.SUCSESS) { return(Lastkeys); } //S4_GET_SERIAL_NUMBER // byte[] SerNumkey = new byte[8]; // dwResult = S4_API.S4Control(ref stS4Ctx,S4_API.S4_GET_SERIAL_NUMBER,null, 0, SerNumkey, (uint)SerNumkey.Length,ref pdwBytesReturned); if (dwResult == S4_API.S4_SUCCESS) { for (int i = 0; i < keys.Length; i++) { keys[i] = byte.Parse(keyword.Substring(i * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier); } // byte[] ceter = new byte[4]; // ceter = BitConverter.GetBytes(BitConverter.ToInt32(keys,0) ^ BitConverter.ToInt32(SerNumkey,0)); // Buffer.BlockCopy(ceter, 0, Lastkeys, 0, ceter.Length); // ceter = BitConverter.GetBytes(BitConverter.ToInt32(keys,4) ^ BitConverter.ToInt32(SerNumkey,4)); // Buffer.BlockCopy(ceter, 0, Lastkeys, 4, ceter.Length); } return(keys); }
// /// <summary> // /// 用TDES算法解密数据 // /// </summary> // /// <param name="input">密文</param> // /// <param name="output">明文</param> // /// <returns>成功与否</returns> // public static BYHX_SL_RetValue Decrypt_TDES(byte[] input, ref byte[] output) // { // IO_PACKAGE stDataPkgIn = new IO_PACKAGE(); // IO_PACKAGE stDataPkgOut = new IO_PACKAGE(); // stDataPkgIn.tag = (byte)SubFunction_ID.Decrypt_TDES; // stDataPkgIn.len = IO_PACKAGE_BUFF_SIZE; // stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE]; // stDataPkgOut.len = IO_PACKAGE_BUFF_SIZE; // stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE]; // Buffer.BlockCopy(input, 0, stDataPkgIn.buff, 0, input.Length); // BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut); //#if DEBUG // if (stDataPkgOut.tag != S4_API.S4_SUCCESS) // { // MessageBox.Show("CheckValidDateWithData Run failed!"); // } //#endif // Buffer.BlockCopy(stDataPkgOut.buff, 0, output, 0, stDataPkgOut.len); // return ret; // } // /// <summary> // /// 用TDES算法加密数据 // /// </summary> // /// <param name="input">明文</param> // /// <param name="output">密文</param> // /// <returns>成功与否</returns> // public static BYHX_SL_RetValue Encrypt_TDES(byte[] input, ref byte[] output) // { // IO_PACKAGE stDataPkgIn = new IO_PACKAGE(); // IO_PACKAGE stDataPkgOut = new IO_PACKAGE(); // stDataPkgIn.tag = (byte)SubFunction_ID.Encrypt_TDES; // stDataPkgIn.len = (byte)input.Length; // stDataPkgIn.buff = input; // stDataPkgOut.len = IO_PACKAGE_BUFF_SIZE; // stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE]; // BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut); //#if DEBUG // if (stDataPkgOut.tag != S4_API.S4_SUCCESS) // { // MessageBox.Show("CheckValidDateWithData Run failed!"); // } //#endif // Buffer.BlockCopy(stDataPkgOut.buff, 0, output, 0, output.Length); // return ret; // } /// <summary> /// 运行锁内可执行程序 /// </summary> /// <param name="inbuf">输入</param> /// <param name="outbuf">输出</param> /// <returns>true=调用成功;false=调用失败</returns> private static BYHX_SL_RetValue ExecuteExeById(IO_PACKAGE inbuf, ref IO_PACKAGE outbuf, int index) { SENSE4_CONTEXT stS4Ctx = new SENSE4_CONTEXT(); uint dwResult = 0; uint dwBytesReturned = 250; dwResult = (uint)Common.OpenS4ByIndex(index, ref stS4Ctx); if (dwResult != (uint)BYHX_SL_RetValue.SUCSESS) { return((BYHX_SL_RetValue)dwResult); } dwResult = S4_API.S4ChangeDir(ref stS4Ctx, S4_FolderName); //"\\"); if (dwResult != S4_API.S4_SUCCESS) { #if DEBUG string msg = string.Format("Change directory failed! <error code: {0}>\n", dwResult); MessageBox.Show(msg); #endif Common.ResetAndCloseS4(stS4Ctx); return(BYHX_SL_RetValue.ILLEGALDOG); } // Call S4VerifyPin(...) to verify User PIN so as to get the privilege to execute the program in EliteIV. dwResult = S4_API.S4VerifyPin(ref stS4Ctx, UserPin, (uint)UserPin.Length, S4_API.S4_USER_PIN); if (dwResult != S4_API.S4_SUCCESS) { #if DEBUG string msg = string.Format("Verify Pin failed! <error code: {0}>\n", dwResult); MessageBox.Show(msg); #endif Common.ResetAndCloseS4(stS4Ctx); return(BYHX_SL_RetValue.ILLEGALDOG); } byte[] input = inbuf.ToByteArray(); byte[] output = outbuf.ToByteArray(); dwBytesReturned = outbuf.len; dwResult = S4_API.S4Execute(ref stS4Ctx, S4_EXE_FILE_ID, input, IO_PACKAGE_HEADER_SIZE + (uint)inbuf.len, output, (uint)outbuf.len, ref dwBytesReturned); if (dwResult != S4_API.S4_SUCCESS) { #if DEBUG MessageBox.Show("Call S4Execute failed!\n"); #endif return(BYHX_SL_RetValue.ILLEGALDOG); } else { outbuf.tag = output[0]; outbuf.len = output[1]; Buffer.BlockCopy(output, IO_PACKAGE_HEADER_SIZE, outbuf.buff, 0, IO_PACKAGE_BUFF_SIZE); //MessageBox.Show("Success!\n"); } /* for better security,use the following instead of using S4close() directly */ Common.ResetAndCloseS4(stS4Ctx); return(BYHX_SL_RetValue.SUCSESS); }
/* * Reset and close specified EliteIV device * Parameters: * pstS4Ctx[out]: pointer to EliteIV context * * Return: * none. * * Remarks: * Use this function instead of S4Close() may enhance security in certain * circumstances. */ public static void ResetAndCloseS4(SENSE4_CONTEXT pstS4Ctx) { uint ret = 0; S4_API.S4Control(ref pstS4Ctx, S4_API.S4_RESET_DEVICE, null, 0, null, 0, ref ret); S4_API.S4Close(ref pstS4Ctx); return; }
public static SENSE4_CONTEXT[] EnumAllS4() { SENSE4_CONTEXT[] pstS4CtxList = null; uint dwCtxListSize = 0; uint dwResult = 0; uint dwDeviceNum = 0; dwResult = S4_API.S4Enum(null, ref dwCtxListSize); if (dwResult != S4_API.S4_SUCCESS && dwResult != S4_API.S4_INSUFFICIENT_BUFFER) { #if DEBUG string msg = string.Format("Enumerate EliteIV failed! <error code: 0x%08x>\n", dwResult); MessageBox.Show(msg); #endif return(null); } if (0 == dwCtxListSize) { //#if DEBUG // MessageBox.Show("EliteIV not found!\n"); //#endif return(null); } // allocate memory for EliteIV context list/array pstS4CtxList = new SENSE4_CONTEXT[dwCtxListSize / Marshal.SizeOf(typeof(SENSE4_CONTEXT))]; dwDeviceNum = (uint)(dwCtxListSize / Marshal.SizeOf(typeof(SENSE4_CONTEXT))); /* * This time, call S4Enum(...) to do the real Enumeration. * All the fields of the EliteIV context(s) pointed by pstS4CtxList will be filled * with the corresponding device information upon a successful function return. */ dwResult = S4_API.S4Enum(pstS4CtxList, ref dwCtxListSize); if (dwResult != S4_API.S4_SUCCESS) { #if DEBUG string msg = string.Format("Open EliteIV failed! <error code: {0}x%08x>\n", dwResult); MessageBox.Show(msg); #endif //free(pstS4CtxList); pstS4CtxList = null; } return(pstS4CtxList); }
public const int DBT_DEVICETYPESPECIFIC = 0x8005; // type specific event /* * Print an array in Hex. * Parameters: * fp[in] : file pointer * data[in]: data to be printed * len[in] : data length * * Return: * none. * * Remarks: * none. */ //void hexprint(FILE *fp, void *data, int len) //{ // unsigned char *pdata = (unsigned char *)data; // int i = 0; // for (; i<len ;i++) // { // if (!(i%16)) // fprintf(fp,"\n"); // fprintf(fp, "%02x:",*(pdata+i)); // } // fprintf(fp, "\n"); //} /* * Select and open specified EliteIV device * Parameters: * index[in] : device index * pstS4Ctx[out]: pointer to EliteIV context * * Return: * If the function succeeds, it will return 1(TRUE), otherwise * it returns 0(FALSE). * * Remarks: * none. */ public static BYHX_SL_RetValue OpenS4ByIndex(int index, ref SENSE4_CONTEXT pstS4Ctx) { if (index < 0) { return(BYHX_SL_RetValue.NOFOUNDDOG); } SENSE4_CONTEXT[] pstS4CtxList = null; uint dwCtxListSize = 0; uint dwResult = 0; uint dwDeviceNum = 0; //if (null == pstS4Ctx) //{ // MessageBox.Show("Invalid pointer!\n"); // return 0; //} /* * Use S4Enum(...) to Enumerate all the devices connected to the host. * Using null as the 1st parameter will instruct S4Enum to return the buffer size needed to hold * the corresponding EliteIV context in the 2nd parameter:dwCtxListSize. * Generally only two results possible on return: (dwCtxListSize == 0) which means no EliteIV device * present or (dwCtxListSize != 0) and that's saying there are dwCtxListSize/sizeof(SENSE4_CONTEXT) sets * of EliteIV devices connected currently. */ dwResult = S4_API.S4Enum(null, ref dwCtxListSize); if (dwResult != S4_API.S4_SUCCESS && dwResult != S4_API.S4_INSUFFICIENT_BUFFER) { #if DEBUG string msg = string.Format("Enumerate EliteIV failed! <error code: 0x%08x>\n", dwResult); MessageBox.Show(msg); #endif return(BYHX_SL_RetValue.NOFOUNDDOG); } if (0 == dwCtxListSize) { #if DEBUG MessageBox.Show("EliteIV not found!\n"); #endif return(BYHX_SL_RetValue.NOFOUNDDOG); } // allocate memory for EliteIV context list/array pstS4CtxList = new SENSE4_CONTEXT[dwCtxListSize / Marshal.SizeOf(typeof(SENSE4_CONTEXT))];; if (null == pstS4CtxList) { #if DEBUG MessageBox.Show("Not enough memory! \n"); #endif return(BYHX_SL_RetValue.NOFOUNDDOG); } dwDeviceNum = (uint)(dwCtxListSize / Marshal.SizeOf(typeof(SENSE4_CONTEXT))); if (index + 1 > dwDeviceNum) { #if DEBUG string msg = string.Format("Invalid index!<index should be within [0..%d]> \n", dwDeviceNum - 1); MessageBox.Show(msg); #endif //free(pstS4CtxList); pstS4CtxList = null; return(BYHX_SL_RetValue.NOFOUNDDOG); } /* * This time, call S4Enum(...) to do the real Enumeration. * All the fields of the EliteIV context(s) pointed by pstS4CtxList will be filled * with the corresponding device information upon a successful function return. */ dwResult = S4_API.S4Enum(pstS4CtxList, ref dwCtxListSize); if (dwResult != S4_API.S4_SUCCESS) { #if DEBUG string msg = string.Format("Open EliteIV failed! <error code: {0}x%08x>\n", dwResult); MessageBox.Show(msg); #endif //free(pstS4CtxList); pstS4CtxList = null; return(BYHX_SL_RetValue.ILLEGALDOG); } // use device with specified index pstS4Ctx = pstS4CtxList[index]; //free(pstS4CtxList); pstS4CtxList = null; // Call S4Open(...) to open the specified EliteIV device in "share mode". //S4OPENINFO s4openinfo =new S4OPENINFO(); //s4openinfo.dwS4OpenInfoSize = (short)Marshal.SizeOf(typeof(S4OPENINFO)); //s4openinfo.dwShareMode = (short)S4_API.S4_EXCLUSIZE_MODE; //dwResult = S4_API.S4OpenEx(ref pstS4Ctx, s4openinfo); dwResult = S4_API.S4Open(ref pstS4Ctx); if (dwResult != S4_API.S4_SUCCESS) { #if DEBUG string msg = string.Format("Open EliteIV failed! <error code: {0}x%08x>\n", dwResult); MessageBox.Show(msg); #endif return(BYHX_SL_RetValue.ILLEGALDOG); } return(BYHX_SL_RetValue.SUCSESS); }
public static extern uint S4Execute(ref SENSE4_CONTEXT s4Ctx, string FileID, byte[] InBuffer, uint InbufferSize, byte[] OutBuffer, uint OutBufferSize, ref uint BytesReturned);
public static extern uint S4WriteFile(ref SENSE4_CONTEXT s4Ctx, string FileID, uint Offset, byte[] Buffer, uint BufferSize, uint FileSize, ref uint BytesWritten, uint Flags, uint FileType);
private static extern uint S4ChangePin(ref SENSE4_CONTEXT s4Ctx, byte[] OldPin, uint OldPinLen, byte[] NewPin, uint NewPinLen, uint PinType);
public static extern uint S4VerifyPin(ref SENSE4_CONTEXT s4Ctx, byte[] Pin, uint PinLen, uint PinType);
public static extern uint S4EraseDir(ref SENSE4_CONTEXT s4Ctx, string DirID);
public static extern uint S4ChangeDir(ref SENSE4_CONTEXT s4Ctx, string Path);
public static extern uint S4CreateDir(ref SENSE4_CONTEXT s4Ctx, string DirID, uint DirSize, uint Flags);
public static extern uint S4Control(ref SENSE4_CONTEXT s4Ctx, uint ctlCode, byte[] inBuff, uint inBuffLen, byte[] outBuff, uint outBuffLen, ref uint BytesReturned);
public static extern uint S4Close(ref SENSE4_CONTEXT s4_context);
public static extern uint S4OpenEx(ref SENSE4_CONTEXT s4_context, S4OPENINFO pS4OpenInfo);
public static extern uint S4Open(ref SENSE4_CONTEXT s4_context);