SoftKey.cs 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace Team.Utility
  6. {
  7. //公共函数说明
  8. //***查找加密锁
  9. //int FindPort(int start, ref string OutKeyPath);
  10. //查找指定的加密锁(使用普通算法一)
  11. //int FindPort_2(int start, int in_data, int verf_data, ref string OutKeyPath);
  12. //***获到锁的版本
  13. //int NT_GetIDVersion(ref short version, string KeyPath);
  14. //获到锁的扩展版本
  15. //int NT_GetIDVersionEx(ref short version, string KeyPath);
  16. //***获到锁的ID
  17. //int GetID(ref int id_1, ref int id_2, string KeyPath);
  18. //***从加密锁中读取一批字节
  19. //int YReadEx(byte[] OutData, short Address, short mylen, string HKey, string LKey, string KeyPath);
  20. //***从加密锁中读取一个字节数据,一般不使用
  21. //int YRead(ref byte OutData, short Address,string HKey, string LKey, string KeyPath);
  22. //***写一批字节到加密锁中
  23. //int YWriteEx(byte[] InData, short Address, short mylen, string HKey, string LKey, string KeyPath);
  24. //***写一个字节的数据到加密锁中,一般不使用
  25. //int YWrite(byte InData, short Address, string HKey, string LKey, string KeyPath);
  26. //***从加密锁中读字符串
  27. //int YReadString(ref string outstring, short Address, short mylen, string HKey, string LKey, string KeyPath);
  28. //***写字符串到加密锁中
  29. //int YWriteString(string InString, short Address, string HKey, string LKey, string KeyPath);
  30. //***算法函数
  31. //int sWriteEx(int in_data , ref int out_data , string KeyPath);
  32. //int sWrite_2Ex(int in_data , ref int out_data ,string KeyPath);
  33. //int sRead(ref int in_data, string KeyPath);
  34. //int sWrite(int out_data, string KeyPath);
  35. //int sWrite_2(int out_data, string KeyPath);
  36. //***设置写密码
  37. //int SetWritePassword(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string KeyPath);
  38. //***设置读密码
  39. //int SetReadPassword(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string KeyPath);
  40. //'设置增强算法密钥一
  41. //int SetCal_2(string Key , string KeyPath);
  42. //使用增强算法一对字符串进行加密
  43. //int EncString(string InString , ref string outstring , string KeyPath);
  44. //使用增强算法一对二进制数据进行加密
  45. // int Cal(byte[] InBuf, byte[] OutBuf, string KeyPath);
  46. //'设置增强算法密钥二
  47. //int SetCal_New(string Key , string KeyPath);
  48. //使用增强算法二对字符串进行加密
  49. //int EncString_New(string InString , ref string outstring , string KeyPath);
  50. //使用增强算法二对二进制数据进行加密
  51. // int Cal_New(byte[] InBuf, byte[] OutBuf, string KeyPath);
  52. //***初始化加密锁函数
  53. //int ReSet( string Path);
  54. //***获取字符串长度
  55. //int lstrlenA(string InString );
  56. public struct GUID
  57. {
  58. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  59. public byte[] Data1;
  60. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
  61. public byte[] Data2;
  62. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
  63. public byte[] Data3;
  64. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  65. public byte[] Data4;
  66. }
  67. public struct SP_INTERFACE_DEVICE_DATA
  68. {
  69. public int cbSize;
  70. public GUID InterfaceClassGuid;
  71. public int Flags;
  72. public IntPtr Reserved;
  73. }
  74. public struct SP_DEVINFO_DATA
  75. {
  76. public int cbSize;
  77. public GUID ClassGuid;
  78. public int DevInst;
  79. public IntPtr Reserved;
  80. }
  81. public struct SP_DEVICE_INTERFACE_DETAIL_DATA
  82. {
  83. public int cbSize;
  84. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
  85. public byte[] DevicePath;
  86. }
  87. public struct HIDD_ATTRIBUTES
  88. {
  89. public int Size;
  90. public ushort VendorID;
  91. public ushort ProductID;
  92. public ushort VersionNumber;
  93. }
  94. public struct HIDP_CAPS
  95. {
  96. public short Usage;
  97. public short UsagePage;
  98. public short InputReportByteLength;
  99. public short OutputReportByteLength;
  100. public short FeatureReportByteLength;
  101. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  102. public short[] Reserved;
  103. public short NumberLinkCollectionNodes;
  104. public short NumberInputButtonCaps;
  105. public short NumberInputValueCaps;
  106. public short NumberInputDataIndices;
  107. public short NumberOutputButtonCaps;
  108. public short NumberOutputValueCaps;
  109. public short NumberOutputDataIndices;
  110. public short NumberFeatureButtonCaps;
  111. public short NumberFeatureValueCaps;
  112. public short NumberFeatureDataIndices;
  113. }
  114. public class SoftKey
  115. {
  116. private const ushort VID = 0x3689;
  117. private const ushort PID = 0x8762;
  118. private const ushort PID_NEW = 0X2020;
  119. private const ushort VID_NEW = 0X3689;
  120. private const ushort PID_NEW_2 = 0X2020;
  121. private const ushort VID_NEW_2 = 0X2020;
  122. private const short DIGCF_PRESENT = 0x2;
  123. private const short DIGCF_DEVICEINTERFACE = 0x10;
  124. private const short INVALID_HANDLE_VALUE = (-1);
  125. private const short ERROR_NO_MORE_ITEMS = 259;
  126. private const uint GENERIC_READ = 0x80000000;
  127. private const int GENERIC_WRITE = 0x40000000;
  128. private const uint FILE_SHARE_READ = 0x1;
  129. private const uint FILE_SHARE_WRITE = 0x2;
  130. private const uint OPEN_EXISTING = 3;
  131. private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
  132. private const uint INFINITE = 0xFFFF;
  133. private const short MAX_LEN = 495;
  134. public const int FAILEDGENKEYPAIR = -21;
  135. public const int FAILENC = -22;
  136. public const int FAILDEC = -23;
  137. public const int FAILPINPWD = -24;
  138. public const int USBStatusFail = -50; //USB操作失败,可能是没有找到相关的指令
  139. public const int SM2_ADDBYTE = 97; //加密后的数据会增加的长度
  140. public const int MAX_ENCLEN = 128; //最大的加密长度分组
  141. public const int MAX_DECLEN = (MAX_ENCLEN + SM2_ADDBYTE); //最大的解密长度分组
  142. public const int SM2_USENAME_LEN = 80; // '最大的用户名长度
  143. public const int ECC_MAXLEN = 32;
  144. public const int PIN_LEN = 16;
  145. private const byte GETVERSION = 0x01;
  146. private const byte GETID = 0x02;
  147. private const byte GETVEREX = 0x05;
  148. private const byte CAL_TEA = 0x08;
  149. private const byte SET_TEAKEY = 0x09;
  150. private const byte READBYTE = 0x10;
  151. private const byte WRITEBYTE = 0x11;
  152. private const byte YTREADBUF = 0x12;
  153. private const byte YTWRITEBUF = 0x13;
  154. private const byte MYRESET = 0x20;
  155. private const byte YTREBOOT = 0x24;
  156. private const byte SET_ECC_PARA = 0x30;
  157. private const byte GET_ECC_PARA = 0x31;
  158. private const byte SET_ECC_KEY = 0x32;
  159. private const byte GET_ECC_KEY = 0x33;
  160. private const byte MYENC = 0x34;
  161. private const byte MYDEC = 0X35;
  162. private const byte SET_PIN = 0X36;
  163. private const byte GEN_KEYPAIR = 0x37;
  164. private const byte YTSIGN = 0x51;
  165. private const byte YTVERIFY = 0x52;
  166. private const byte GET_CHIPID = 0x53;
  167. private const byte YTSIGN_2 = 0x53;
  168. [DllImport("kernel32.dll")]
  169. public static extern int lstrlenA(string InString);
  170. [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
  171. public static extern void CopyStringToByte(byte[] pDest, string pSourceg, int ByteLenr);
  172. [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
  173. public static extern void CopyByteToString(StringBuilder pDest, byte[] pSource, int ByteLenr);
  174. [DllImport("HID.dll")]
  175. private static extern bool HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
  176. [DllImport("HID.dll")]
  177. private static extern int HidD_GetHidGuid(ref GUID HidGuid);
  178. [DllImport("HID.dll")]
  179. private static extern bool HidD_GetPreparsedData(IntPtr HidDeviceObject, ref IntPtr PreparsedData);
  180. [DllImport("HID.dll")]
  181. private static extern int HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
  182. [DllImport("HID.dll")]
  183. private static extern bool HidD_FreePreparsedData(IntPtr PreparsedData);
  184. [DllImport("HID.dll")]
  185. private static extern bool HidD_SetFeature(IntPtr HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
  186. [DllImport("HID.dll")]
  187. private static extern bool HidD_GetFeature(IntPtr HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
  188. [DllImport("SetupApi.dll")]
  189. private static extern IntPtr
  190. SetupDiGetClassDevsA(ref GUID ClassGuid, int Enumerator, int hwndParent, int Flags);
  191. [DllImport("SetupApi.dll")]
  192. private static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
  193. [DllImport("SetupApi.dll")]
  194. private static extern bool SetupDiGetDeviceInterfaceDetailA(IntPtr DeviceInfoSet,
  195. ref SP_INTERFACE_DEVICE_DATA DeviceInterfaceData,
  196. ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize,
  197. ref int RequiredSize, int DeviceInfoData);
  198. [DllImport("SetupApi.dll")]
  199. private static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, int DeviceInfoData,
  200. ref GUID InterfaceClassGuid, int MemberIndex, ref SP_INTERFACE_DEVICE_DATA DeviceInterfaceData);
  201. [DllImport("kernel32.dll", EntryPoint = "CreateFileA")]
  202. private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode,
  203. uint lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, uint hTemplateFile);
  204. [DllImport("kernel32.dll")]
  205. private static extern int CloseHandle(IntPtr hObject);
  206. [DllImport("kernel32.dll")]
  207. private static extern int GetLastError();
  208. [DllImport("kernel32.dll", EntryPoint = "CreateSemaphoreA")]
  209. private static extern IntPtr CreateSemaphore(int lpSemaphoreAttributes, int lInitialCount, int lMaximumCount,
  210. string lpName);
  211. [DllImport("kernel32.dll")]
  212. private static extern int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
  213. [DllImport("kernel32.dll")]
  214. private static extern int ReleaseSemaphore(IntPtr hSemaphore, int lReleaseCount, int lpPreviousCount);
  215. //以下函数用于将字节数组转化为宽字符串
  216. private static string ByteConvertString(byte[] buffer)
  217. {
  218. char[] null_string = {'\0', '\0'};
  219. System.Text.Encoding encoding = System.Text.Encoding.Default;
  220. return encoding.GetString(buffer).TrimEnd(null_string);
  221. }
  222. //以下用于将16进制字符串转化为无符号长整型
  223. private uint HexToInt(string s)
  224. {
  225. string[] hexch =
  226. {
  227. "0", "1", "2", "3", "4", "5", "6", "7",
  228. "8", "9", "A", "B", "C", "D", "E", "F"
  229. };
  230. s = s.ToUpper();
  231. int i, j;
  232. int r, n, k;
  233. string ch;
  234. k = 1;
  235. r = 0;
  236. for (i = s.Length; i > 0; i--)
  237. {
  238. ch = s.Substring(i - 1, 1);
  239. n = 0;
  240. for (j = 0; j < 16; j++)
  241. if (ch == hexch[j])
  242. n = j;
  243. r += (n * k);
  244. k *= 16;
  245. }
  246. return unchecked((uint) r);
  247. }
  248. private int HexStringToByteArray(string InString, ref byte[] b)
  249. {
  250. int nlen;
  251. int retutn_len;
  252. int n, i;
  253. string temp;
  254. nlen = InString.Length;
  255. if (nlen < 16) retutn_len = 16;
  256. retutn_len = nlen / 2;
  257. b = new byte[retutn_len];
  258. i = 0;
  259. for (n = 0; n < nlen; n = n + 2)
  260. {
  261. temp = InString.Substring(n, 2);
  262. b[i] = (byte) HexToInt(temp);
  263. i = i + 1;
  264. }
  265. return retutn_len;
  266. }
  267. public void EnCode(byte[] inb, byte[] outb, string Key)
  268. {
  269. uint cnDelta, y, z, a, b, c, d, temp_2;
  270. uint[] buf = new uint[16];
  271. int n, i, nlen;
  272. uint sum;
  273. //UInt32 temp, temp_1;
  274. string temp_string;
  275. cnDelta = 2654435769;
  276. sum = 0;
  277. nlen = Key.Length;
  278. i = 0;
  279. for (n = 1; n <= nlen; n = n + 2)
  280. {
  281. temp_string = Key.Substring(n - 1, 2);
  282. buf[i] = HexToInt(temp_string);
  283. i = i + 1;
  284. }
  285. a = 0;
  286. b = 0;
  287. c = 0;
  288. d = 0;
  289. for (n = 0; n <= 3; n++)
  290. {
  291. a = (buf[n] << (n * 8)) | a;
  292. b = (buf[n + 4] << (n * 8)) | b;
  293. c = (buf[n + 4 + 4] << (n * 8)) | c;
  294. d = (buf[n + 4 + 4 + 4] << (n * 8)) | d;
  295. }
  296. y = 0;
  297. z = 0;
  298. for (n = 0; n <= 3; n++)
  299. {
  300. temp_2 = inb[n];
  301. y = (temp_2 << (n * 8)) | y;
  302. temp_2 = inb[n + 4];
  303. z = (temp_2 << (n * 8)) | z;
  304. }
  305. n = 32;
  306. while (n > 0)
  307. {
  308. sum = cnDelta + sum;
  309. /*temp = (z << 4) & 0xFFFFFFFF;
  310. temp = (temp + a) & 0xFFFFFFFF;
  311. temp_1 = (z + sum) & 0xFFFFFFFF;
  312. temp = (temp ^ temp_1) & 0xFFFFFFFF;
  313. temp_1 = (z >> 5) & 0xFFFFFFFF;
  314. temp_1 = (temp_1 + b) & 0xFFFFFFFF;
  315. temp = (temp ^ temp_1) & 0xFFFFFFFF;
  316. temp = (temp + y) & 0xFFFFFFFF;
  317. y = temp & 0xFFFFFFFF;*/
  318. y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
  319. /*temp = (y << 4) & 0xFFFFFFFF;
  320. temp = (temp + c) & 0xFFFFFFFF;
  321. temp_1 = (y + sum) & 0xFFFFFFFF;
  322. temp = (temp ^ temp_1) & 0xFFFFFFFF;
  323. temp_1 = (y >> 5) & 0xFFFFFFFF;
  324. temp_1 = (temp_1 + d) & 0xFFFFFFFF;
  325. temp = (temp ^ temp_1) & 0xFFFFFFFF;
  326. temp = (z + temp) & 0xFFFFFFFF;
  327. z = temp & 0xFFFFFFFF;*/
  328. z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
  329. n = n - 1;
  330. }
  331. for (n = 0; n <= 3; n++)
  332. {
  333. outb[n] = System.Convert.ToByte((y >> (n * 8)) & 255);
  334. outb[n + 4] = System.Convert.ToByte((z >> (n * 8)) & 255);
  335. }
  336. }
  337. public void DeCode(byte[] inb, byte[] outb, string Key)
  338. {
  339. uint cnDelta, y, z, a, b, c, d, temp_2;
  340. uint[] buf = new uint[16];
  341. int n, i, nlen;
  342. uint sum;
  343. //UInt32 temp, temp_1;
  344. string temp_string;
  345. cnDelta = 2654435769;
  346. sum = 0xC6EF3720;
  347. nlen = Key.Length;
  348. i = 0;
  349. for (n = 1; n <= nlen; n = n + 2)
  350. {
  351. temp_string = Key.Substring(n - 1, 2);
  352. buf[i] = HexToInt(temp_string);
  353. i = i + 1;
  354. }
  355. a = 0;
  356. b = 0;
  357. c = 0;
  358. d = 0;
  359. for (n = 0; n <= 3; n++)
  360. {
  361. a = (buf[n] << (n * 8)) | a;
  362. b = (buf[n + 4] << (n * 8)) | b;
  363. c = (buf[n + 4 + 4] << (n * 8)) | c;
  364. d = (buf[n + 4 + 4 + 4] << (n * 8)) | d;
  365. }
  366. y = 0;
  367. z = 0;
  368. for (n = 0; n <= 3; n++)
  369. {
  370. temp_2 = inb[n];
  371. y = (temp_2 << (n * 8)) | y;
  372. temp_2 = inb[n + 4];
  373. z = (temp_2 << (n * 8)) | z;
  374. }
  375. n = 32;
  376. while (n-- > 0)
  377. {
  378. z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
  379. y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
  380. sum -= cnDelta;
  381. }
  382. for (n = 0; n <= 3; n++)
  383. {
  384. outb[n] = System.Convert.ToByte((y >> (n * 8)) & 255);
  385. outb[n + 4] = System.Convert.ToByte((z >> (n * 8)) & 255);
  386. }
  387. }
  388. public string StrEnc(string inString, string key) //使用增强算法,加密字符串
  389. {
  390. byte[] b, outb;
  391. byte[] temp = new byte[8], outtemp = new byte[8];
  392. int n, i, nlen, outlen;
  393. string outstring;
  394. nlen = lstrlenA(inString) + 1;
  395. if (nlen < 8)
  396. outlen = 8;
  397. else
  398. outlen = nlen;
  399. b = new byte[outlen];
  400. outb = new byte[outlen];
  401. CopyStringToByte(b, inString, nlen);
  402. b.CopyTo(outb, 0);
  403. for (n = 0; n <= outlen - 8; n = n + 8)
  404. {
  405. for (i = 0; i < 8; i++) temp[i] = b[i + n];
  406. EnCode(temp, outtemp, key);
  407. for (i = 0; i < 8; i++) outb[i + n] = outtemp[i];
  408. }
  409. outstring = "";
  410. for (n = 0; n <= outlen - 1; n++)
  411. {
  412. outstring = outstring + outb[n].ToString("X2");
  413. }
  414. return outstring;
  415. }
  416. public string StrDec(string inString, string key) //使用增强算法,加密字符串
  417. {
  418. byte[] b, outb;
  419. byte[] temp = new byte[8], outtemp = new byte[8];
  420. int n, i, nlen, outlen;
  421. string temp_string;
  422. StringBuilder c_str;
  423. nlen = inString.Length;
  424. if (nlen < 16) outlen = 16;
  425. outlen = nlen / 2;
  426. b = new byte[outlen];
  427. outb = new byte[outlen];
  428. i = 0;
  429. for (n = 1; n <= nlen; n = n + 2)
  430. {
  431. temp_string = inString.Substring(n - 1, 2);
  432. b[i] = System.Convert.ToByte(HexToInt(temp_string));
  433. i = i + 1;
  434. }
  435. b.CopyTo(outb, 0);
  436. for (n = 0; n <= outlen - 8; n = n + 8)
  437. {
  438. for (i = 0; i < 8; i++) temp[i] = b[i + n];
  439. DeCode(temp, outtemp, key);
  440. for (i = 0; i < 8; i++) outb[i + n] = outtemp[i];
  441. }
  442. c_str = new StringBuilder("", outlen);
  443. CopyByteToString(c_str, outb, outlen);
  444. return c_str.ToString();
  445. }
  446. private bool isfindmydevice(int pos, ref int count, ref string OutPath)
  447. {
  448. return Subisfindmydevice(pos, ref count, ref OutPath);
  449. }
  450. private bool Subisfindmydevice(int pos, ref int count, ref string OutPath)
  451. {
  452. IntPtr hardwareDeviceInfo;
  453. SP_INTERFACE_DEVICE_DATA DeviceInfoData = new SP_INTERFACE_DEVICE_DATA();
  454. int i;
  455. GUID HidGuid = new GUID();
  456. SP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
  457. int requiredLength;
  458. IntPtr d_handle;
  459. HIDD_ATTRIBUTES Attributes = new HIDD_ATTRIBUTES();
  460. i = 0;
  461. count = 0;
  462. HidD_GetHidGuid(ref HidGuid);
  463. hardwareDeviceInfo = SetupDiGetClassDevsA(ref HidGuid, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  464. if (hardwareDeviceInfo == (IntPtr) INVALID_HANDLE_VALUE) return false;
  465. DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData);
  466. while (SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0, ref HidGuid, i, ref DeviceInfoData))
  467. {
  468. if (GetLastError() == ERROR_NO_MORE_ITEMS) break;
  469. if (System.IntPtr.Size == 4)
  470. functionClassDeviceData.cbSize = Marshal.SizeOf(functionClassDeviceData) - 255; // 5;
  471. else
  472. functionClassDeviceData.cbSize = 8;
  473. requiredLength = 0;
  474. if (!SetupDiGetDeviceInterfaceDetailA(hardwareDeviceInfo, ref DeviceInfoData,
  475. ref functionClassDeviceData, 300, ref requiredLength, 0))
  476. {
  477. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  478. return false;
  479. }
  480. OutPath = ByteConvertString(functionClassDeviceData.DevicePath);
  481. d_handle = CreateFile(OutPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
  482. OPEN_EXISTING, 0, 0);
  483. if ((IntPtr) INVALID_HANDLE_VALUE != d_handle)
  484. {
  485. if (HidD_GetAttributes(d_handle, ref Attributes))
  486. {
  487. if ((Attributes.ProductID == PID) && (Attributes.VendorID == VID) ||
  488. (Attributes.ProductID == PID_NEW) && (Attributes.VendorID == VID_NEW) ||
  489. (Attributes.ProductID == PID_NEW_2) && (Attributes.VendorID == VID_NEW_2))
  490. {
  491. if (pos == count)
  492. {
  493. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  494. CloseHandle(d_handle);
  495. return true;
  496. }
  497. count = count + 1;
  498. }
  499. }
  500. CloseHandle(d_handle);
  501. }
  502. i = i + 1;
  503. }
  504. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  505. return false;
  506. }
  507. private bool GetFeature(IntPtr hDevice, byte[] array_out, int out_len)
  508. {
  509. bool FeatureStatus;
  510. bool Status;
  511. int i;
  512. byte[] FeatureReportBuffer = new byte[512];
  513. IntPtr Ppd = System.IntPtr.Zero;
  514. HIDP_CAPS Caps = new HIDP_CAPS();
  515. if (!HidD_GetPreparsedData(hDevice, ref Ppd)) return false;
  516. if (HidP_GetCaps(Ppd, ref Caps) <= 0)
  517. {
  518. HidD_FreePreparsedData(Ppd);
  519. return false;
  520. }
  521. Status = true;
  522. FeatureReportBuffer[0] = 1;
  523. FeatureStatus = HidD_GetFeature(hDevice, FeatureReportBuffer, Caps.FeatureReportByteLength);
  524. if (FeatureStatus)
  525. {
  526. for (i = 0; i < out_len; i++)
  527. {
  528. array_out[i] = FeatureReportBuffer[i];
  529. }
  530. }
  531. Status = Status && FeatureStatus;
  532. HidD_FreePreparsedData(Ppd);
  533. return Status;
  534. }
  535. private bool SetFeature(IntPtr hDevice, byte[] array_in, int in_len)
  536. {
  537. bool FeatureStatus;
  538. bool Status;
  539. int i;
  540. byte[] FeatureReportBuffer = new byte[512];
  541. IntPtr Ppd = System.IntPtr.Zero;
  542. HIDP_CAPS Caps = new HIDP_CAPS();
  543. if (!HidD_GetPreparsedData(hDevice, ref Ppd)) return false;
  544. if (HidP_GetCaps(Ppd, ref Caps) <= 0)
  545. {
  546. HidD_FreePreparsedData(Ppd);
  547. return false;
  548. }
  549. Status = true;
  550. FeatureReportBuffer[0] = 2;
  551. for (i = 0; i < in_len; i++)
  552. {
  553. FeatureReportBuffer[i + 1] = array_in[i + 1];
  554. }
  555. FeatureStatus = HidD_SetFeature(hDevice, FeatureReportBuffer, Caps.FeatureReportByteLength);
  556. Status = Status && FeatureStatus;
  557. HidD_FreePreparsedData(Ppd);
  558. return Status;
  559. }
  560. private int NT_FindPort(int start, ref string OutPath)
  561. {
  562. int count = 0;
  563. if (!isfindmydevice(start, ref count, ref OutPath))
  564. {
  565. return -92;
  566. }
  567. return 0;
  568. }
  569. private int NT_FindPort_2(int start, int in_data, int verf_data, ref string OutPath)
  570. {
  571. int count = 0;
  572. int pos;
  573. int out_data = 0;
  574. int ret;
  575. for (pos = start; pos < 256; pos++)
  576. {
  577. if (!isfindmydevice(pos, ref count, ref OutPath)) return -92;
  578. ret = WriteDword(in_data, OutPath);
  579. if (ret != 0) continue;
  580. ret = ReadDword(ref out_data, OutPath);
  581. if (ret != 0) continue;
  582. if (out_data == verf_data)
  583. {
  584. return 0;
  585. }
  586. }
  587. return (-92);
  588. }
  589. private int OpenMydivece(ref IntPtr hUsbDevice, string Path)
  590. {
  591. string OutPath;
  592. bool biao;
  593. int count = 0;
  594. if (Path.Length < 1)
  595. {
  596. OutPath = "";
  597. biao = isfindmydevice(0, ref count, ref OutPath);
  598. if (!biao) return -92;
  599. hUsbDevice = CreateFile(OutPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
  600. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  601. if (hUsbDevice == (IntPtr) INVALID_HANDLE_VALUE) return -92;
  602. }
  603. else
  604. {
  605. hUsbDevice = CreateFile(Path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
  606. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  607. if (hUsbDevice == (IntPtr) INVALID_HANDLE_VALUE) return -92;
  608. }
  609. return 0;
  610. }
  611. private int NT_Read(ref byte ele1, ref byte ele2, ref byte ele3, ref byte ele4, string Path)
  612. {
  613. byte[] array_out = new byte[25];
  614. IntPtr hUsbDevice = IntPtr.Zero;
  615. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  616. if (!GetFeature(hUsbDevice, array_out, 5))
  617. {
  618. CloseHandle(hUsbDevice);
  619. return -93;
  620. }
  621. CloseHandle(hUsbDevice);
  622. ele1 = array_out[0];
  623. ele2 = array_out[1];
  624. ele3 = array_out[2];
  625. ele4 = array_out[3];
  626. return 0;
  627. }
  628. private int NT_Write(byte ele1, byte ele2, byte ele3, byte ele4, string Path)
  629. {
  630. byte[] array_in = new byte[25];
  631. IntPtr hUsbDevice = IntPtr.Zero;
  632. if (OpenMydivece(ref hUsbDevice, Path) != 0)
  633. {
  634. return -92;
  635. }
  636. array_in[1] = 3;
  637. array_in[2] = ele1;
  638. array_in[3] = ele2;
  639. array_in[4] = ele3;
  640. array_in[5] = ele4;
  641. if (!SetFeature(hUsbDevice, array_in, 5))
  642. {
  643. CloseHandle(hUsbDevice);
  644. return -93;
  645. }
  646. CloseHandle(hUsbDevice);
  647. return 0;
  648. }
  649. private int NT_Write_2(byte ele1, byte ele2, byte ele3, byte ele4, string Path)
  650. {
  651. byte[] array_in = new byte[25];
  652. IntPtr hUsbDevice = IntPtr.Zero;
  653. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  654. array_in[1] = 4;
  655. array_in[2] = ele1;
  656. array_in[3] = ele2;
  657. array_in[4] = ele3;
  658. array_in[5] = ele4;
  659. if (!SetFeature(hUsbDevice, array_in, 5))
  660. {
  661. CloseHandle(hUsbDevice);
  662. return -93;
  663. }
  664. CloseHandle(hUsbDevice);
  665. return 0;
  666. }
  667. private int GetIDVersion(ref short Version, string Path)
  668. {
  669. byte[] array_in = new byte[25];
  670. byte[] array_out = new byte[25];
  671. IntPtr hUsbDevice = IntPtr.Zero;
  672. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  673. array_in[1] = 1;
  674. if (!SetFeature(hUsbDevice, array_in, 1))
  675. {
  676. CloseHandle(hUsbDevice);
  677. return -93;
  678. }
  679. if (!GetFeature(hUsbDevice, array_out, 1))
  680. {
  681. CloseHandle(hUsbDevice);
  682. return -93;
  683. }
  684. CloseHandle(hUsbDevice);
  685. Version = array_out[0];
  686. return 0;
  687. }
  688. private int NT_GetID(ref int ID_1, ref int ID_2, string Path)
  689. {
  690. int[] t = new int[8];
  691. byte[] array_in = new byte[25];
  692. byte[] array_out = new byte[25];
  693. IntPtr hUsbDevice = IntPtr.Zero;
  694. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  695. array_in[1] = 2;
  696. if (!SetFeature(hUsbDevice, array_in, 1))
  697. {
  698. CloseHandle(hUsbDevice);
  699. return -93;
  700. }
  701. if (!GetFeature(hUsbDevice, array_out, 8))
  702. {
  703. CloseHandle(hUsbDevice);
  704. return -93;
  705. }
  706. CloseHandle(hUsbDevice);
  707. t[0] = array_out[0];
  708. t[1] = array_out[1];
  709. t[2] = array_out[2];
  710. t[3] = array_out[3];
  711. t[4] = array_out[4];
  712. t[5] = array_out[5];
  713. t[6] = array_out[6];
  714. t[7] = array_out[7];
  715. ID_1 = t[3] | (t[2] << 8) | (t[1] << 16) | (t[0] << 24);
  716. ID_2 = t[7] | (t[6] << 8) | (t[5] << 16) | (t[4] << 24);
  717. return 0;
  718. }
  719. private int Y_Read(byte[] OutData, int address, int nlen, byte[] password, string Path, int pos)
  720. {
  721. int addr_l;
  722. int addr_h;
  723. int n;
  724. byte[] array_in = new byte[512];
  725. byte[] array_out = new byte[512];
  726. if ((address > MAX_LEN) || (address < 0)) return -81;
  727. if ((nlen > 255)) return -87;
  728. if ((nlen + address) > MAX_LEN) return -88;
  729. addr_h = (address >> 8) * 2;
  730. addr_l = address & 255;
  731. IntPtr hUsbDevice = IntPtr.Zero;
  732. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  733. array_in[1] = 0x12;
  734. array_in[2] = (byte) addr_h;
  735. array_in[3] = (byte) addr_l;
  736. array_in[4] = (byte) nlen;
  737. for (n = 0; n <= 7; n++)
  738. {
  739. array_in[5 + n] = password[n];
  740. }
  741. if (!SetFeature(hUsbDevice, array_in, 13))
  742. {
  743. CloseHandle(hUsbDevice);
  744. return -93;
  745. }
  746. if (!GetFeature(hUsbDevice, array_out, nlen + 1))
  747. {
  748. CloseHandle(hUsbDevice);
  749. return -94;
  750. }
  751. CloseHandle(hUsbDevice);
  752. if (array_out[0] != 0)
  753. {
  754. return -83;
  755. }
  756. for (n = 0; n < nlen; n++)
  757. {
  758. OutData[n + pos] = array_out[n + 1];
  759. }
  760. return 0;
  761. }
  762. private int Y_Write(byte[] indata, int address, int nlen, byte[] password, string Path, int pos)
  763. {
  764. int addr_l;
  765. int addr_h;
  766. int n;
  767. byte[] array_in = new byte[512];
  768. byte[] array_out = new byte[512];
  769. if ((nlen > 255)) return -87;
  770. if ((address + nlen - 1) > (MAX_LEN + 17) || (address < 0)) return -81;
  771. addr_h = (address >> 8) * 2;
  772. addr_l = address & 255;
  773. IntPtr hUsbDevice = IntPtr.Zero;
  774. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  775. array_in[1] = 0x13;
  776. array_in[2] = (byte) addr_h;
  777. array_in[3] = (byte) addr_l;
  778. array_in[4] = (byte) nlen;
  779. for (n = 0; n <= 7; n++)
  780. {
  781. array_in[5 + n] = password[n];
  782. }
  783. for (n = 0; n < nlen; n++)
  784. {
  785. array_in[13 + n] = indata[n + pos];
  786. }
  787. if (!SetFeature(hUsbDevice, array_in, 13 + nlen))
  788. {
  789. CloseHandle(hUsbDevice);
  790. return -93;
  791. }
  792. if (!GetFeature(hUsbDevice, array_out, 2))
  793. {
  794. CloseHandle(hUsbDevice);
  795. return -94;
  796. }
  797. CloseHandle(hUsbDevice);
  798. if (array_out[0] != 0)
  799. {
  800. return -82;
  801. }
  802. return 0;
  803. }
  804. private int NT_Cal(byte[] InBuf, byte[] outbuf, string Path, int pos)
  805. {
  806. int n;
  807. byte[] array_in = new byte[25];
  808. byte[] array_out = new byte[25];
  809. IntPtr hUsbDevice = IntPtr.Zero;
  810. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  811. array_in[1] = 8;
  812. for (n = 2; n <= 9; n++)
  813. {
  814. array_in[n] = InBuf[n - 2 + pos];
  815. }
  816. if (!SetFeature(hUsbDevice, array_in, 9))
  817. {
  818. CloseHandle(hUsbDevice);
  819. return -93;
  820. }
  821. if (!GetFeature(hUsbDevice, array_out, 9))
  822. {
  823. CloseHandle(hUsbDevice);
  824. return -93;
  825. }
  826. CloseHandle(hUsbDevice);
  827. for (n = 0; n < 8; n++)
  828. {
  829. outbuf[n + pos] = array_out[n];
  830. }
  831. if (array_out[8] != 0x55)
  832. {
  833. return -20;
  834. }
  835. return 0;
  836. }
  837. private int NT_SetCal_2(byte[] indata, byte IsHi, string Path, short pos)
  838. {
  839. int n;
  840. byte[] array_in = new byte[25];
  841. byte[] array_out = new byte[25];
  842. IntPtr hUsbDevice = IntPtr.Zero;
  843. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  844. array_in[1] = 9;
  845. array_in[2] = IsHi;
  846. for (n = 0; n < 8; n++)
  847. {
  848. array_in[3 + n] = indata[n + pos];
  849. }
  850. if (!SetFeature(hUsbDevice, array_in, 11))
  851. {
  852. CloseHandle(hUsbDevice);
  853. return -93;
  854. }
  855. if (!GetFeature(hUsbDevice, array_out, 2))
  856. {
  857. CloseHandle(hUsbDevice);
  858. return -94;
  859. }
  860. CloseHandle(hUsbDevice);
  861. if (array_out[0] != 0)
  862. {
  863. return -82;
  864. }
  865. return 0;
  866. }
  867. private int ReadDword(ref int out_data, string Path)
  868. {
  869. byte b1 = 0;
  870. byte b2 = 0;
  871. byte b3 = 0;
  872. byte b4 = 0;
  873. int t1;
  874. int t2;
  875. int t3;
  876. int t4;
  877. int ret;
  878. ret = NT_Read(ref b1, ref b2, ref b3, ref b4, Path);
  879. t1 = b1;
  880. t2 = b2;
  881. t3 = b3;
  882. t4 = b4;
  883. out_data = t1 | (t2 << 8) | (t3 << 16) | (t4 << 24);
  884. return ret;
  885. }
  886. private int WriteDword(int in_data, string Path)
  887. {
  888. byte b1;
  889. byte b2;
  890. byte b3;
  891. byte b4;
  892. b1 = (byte) (in_data & 255);
  893. b2 = (byte) ((in_data >> 8) & 255);
  894. b3 = (byte) ((in_data >> 16) & 255);
  895. b4 = (byte) ((in_data >> 24) & 255);
  896. return NT_Write(b1, b2, b3, b4, Path);
  897. }
  898. private int WriteDword_2(int in_data, string Path)
  899. {
  900. byte b1;
  901. byte b2;
  902. byte b3;
  903. byte b4;
  904. b1 = (byte) (in_data & 255);
  905. b2 = (byte) ((in_data >> 8) & 255);
  906. b3 = (byte) ((in_data >> 16) & 255);
  907. b4 = (byte) ((in_data >> 24) & 255);
  908. return NT_Write_2(b1, b2, b3, b4, Path);
  909. }
  910. public int NT_GetIDVersion(ref short Version, string Path)
  911. {
  912. int ret;
  913. IntPtr hsignal;
  914. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  915. WaitForSingleObject(hsignal, INFINITE);
  916. ret = GetIDVersion(ref Version, Path);
  917. ReleaseSemaphore(hsignal, 1, 0);
  918. CloseHandle(hsignal);
  919. return ret;
  920. }
  921. public int GetID(ref int ID_1, ref int ID_2, string Path)
  922. {
  923. int ret;
  924. IntPtr hsignal;
  925. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  926. WaitForSingleObject(hsignal, INFINITE);
  927. ret = NT_GetID(ref ID_1, ref ID_2, Path);
  928. ReleaseSemaphore(hsignal, 1, 0);
  929. CloseHandle(hsignal);
  930. return ret;
  931. }
  932. public int sRead(ref int in_data, string Path)
  933. {
  934. int ret;
  935. IntPtr hsignal;
  936. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  937. WaitForSingleObject(hsignal, INFINITE);
  938. ret = ReadDword(ref in_data, Path);
  939. ReleaseSemaphore(hsignal, 1, 0);
  940. CloseHandle(hsignal);
  941. return ret;
  942. }
  943. public int sWrite(int out_data, string Path)
  944. {
  945. int ret;
  946. IntPtr hsignal;
  947. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  948. WaitForSingleObject(hsignal, INFINITE);
  949. ret = WriteDword(out_data, Path);
  950. ReleaseSemaphore(hsignal, 1, 0);
  951. CloseHandle(hsignal);
  952. return ret;
  953. }
  954. public int YWriteEx(byte[] indata, int address, int nlen, string HKey, string LKey, string Path)
  955. {
  956. int ret = 0;
  957. IntPtr hsignal;
  958. byte[] password = new byte[8];
  959. int n, trashLen = 0;
  960. int leave;
  961. int temp_leave;
  962. if ((address + nlen - 1 > MAX_LEN) || (address < 0)) return -81;
  963. ret = GetTrashBufLen(Path, ref trashLen);
  964. if (trashLen < 100) trashLen = 16;
  965. trashLen = trashLen - 8;
  966. if (ret != 0) return ret;
  967. myconvert(HKey, LKey, password);
  968. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  969. WaitForSingleObject(hsignal, INFINITE);
  970. temp_leave = address % trashLen;
  971. leave = trashLen - temp_leave;
  972. if (leave > nlen) leave = nlen;
  973. if (leave > 0)
  974. {
  975. for (n = 0; n < leave / trashLen; n++)
  976. {
  977. ret = Y_Write(indata, address + n * trashLen, trashLen, password, Path, trashLen * n);
  978. if (ret != 0)
  979. {
  980. ReleaseSemaphore(hsignal, 1, 0);
  981. CloseHandle(hsignal);
  982. return sub_YWriteEx(indata, address, nlen, HKey, LKey, Path);
  983. }
  984. }
  985. if (leave - trashLen * n > 0)
  986. {
  987. ret = Y_Write(indata, address + n * trashLen, leave - n * trashLen, password, Path, trashLen * n);
  988. if (ret != 0)
  989. {
  990. ReleaseSemaphore(hsignal, 1, 0);
  991. CloseHandle(hsignal);
  992. return sub_YWriteEx(indata, address, nlen, HKey, LKey, Path);
  993. }
  994. }
  995. }
  996. nlen = nlen - leave;
  997. address = address + leave;
  998. if (nlen > 0)
  999. {
  1000. for (n = 0; n < nlen / trashLen; n++)
  1001. {
  1002. ret = Y_Write(indata, address + n * trashLen, trashLen, password, Path, leave + trashLen * n);
  1003. if (ret != 0)
  1004. {
  1005. ReleaseSemaphore(hsignal, 1, 0);
  1006. CloseHandle(hsignal);
  1007. return sub_YWriteEx(indata, address, nlen, HKey, LKey, Path);
  1008. }
  1009. }
  1010. if (nlen - trashLen * n > 0)
  1011. {
  1012. ret = Y_Write(indata, address + n * trashLen, nlen - n * trashLen, password, Path,
  1013. leave + trashLen * n);
  1014. if (ret != 0)
  1015. {
  1016. ReleaseSemaphore(hsignal, 1, 0);
  1017. CloseHandle(hsignal);
  1018. return sub_YWriteEx(indata, address, nlen, HKey, LKey, Path);
  1019. }
  1020. }
  1021. }
  1022. ReleaseSemaphore(hsignal, 1, 0);
  1023. CloseHandle(hsignal);
  1024. return ret;
  1025. }
  1026. public int YReadEx(byte[] OutData, short address, short nlen, string HKey, string LKey, string Path)
  1027. {
  1028. int ret = 0;
  1029. IntPtr hsignal;
  1030. byte[] password = new byte[8];
  1031. int n, trashLen = 0;
  1032. if ((address + nlen - 1 > MAX_LEN) || (address < 0)) return (-81);
  1033. ret = GetTrashBufLen(Path, ref trashLen);
  1034. if (trashLen < 100) trashLen = 16;
  1035. if (ret != 0) return ret;
  1036. myconvert(HKey, LKey, password);
  1037. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1038. WaitForSingleObject(hsignal, INFINITE);
  1039. for (n = 0; n < nlen / trashLen; n++)
  1040. {
  1041. ret = Y_Read(OutData, address + n * trashLen, trashLen, password, Path, n * trashLen);
  1042. if (ret != 0)
  1043. {
  1044. ReleaseSemaphore(hsignal, 1, 0);
  1045. CloseHandle(hsignal);
  1046. return sub_YReadEx(OutData, address, nlen, HKey, LKey, Path);
  1047. }
  1048. }
  1049. if (nlen - trashLen * n > 0)
  1050. {
  1051. ret = Y_Read(OutData, address + n * trashLen, nlen - trashLen * n, password, Path, trashLen * n);
  1052. if (ret != 0)
  1053. {
  1054. ReleaseSemaphore(hsignal, 1, 0);
  1055. CloseHandle(hsignal);
  1056. return sub_YReadEx(OutData, address, nlen, HKey, LKey, Path);
  1057. }
  1058. }
  1059. ReleaseSemaphore(hsignal, 1, 0);
  1060. CloseHandle(hsignal);
  1061. return ret;
  1062. }
  1063. public int FindPort_2(int start, int in_data, int verf_data, ref string OutPath)
  1064. {
  1065. int ret;
  1066. IntPtr hsignal;
  1067. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1068. WaitForSingleObject(hsignal, INFINITE);
  1069. ret = NT_FindPort_2(start, in_data, verf_data, ref OutPath);
  1070. ReleaseSemaphore(hsignal, 1, 0);
  1071. CloseHandle(hsignal);
  1072. return ret;
  1073. }
  1074. public int FindPort(int start, ref string OutPath)
  1075. {
  1076. Stopwatch stopwatch = Stopwatch.StartNew();
  1077. int ret;
  1078. IntPtr hsignal;
  1079. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1080. WaitForSingleObject(hsignal, INFINITE);
  1081. ret = NT_FindPort(start, ref OutPath);
  1082. ReleaseSemaphore(hsignal, 1, 0);
  1083. CloseHandle(hsignal);
  1084. Debug.WriteLine(stopwatch.ElapsedMilliseconds);
  1085. return ret;
  1086. }
  1087. public int sWrite_2(int out_data, string Path)
  1088. {
  1089. int ret;
  1090. IntPtr hsignal;
  1091. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1092. WaitForSingleObject(hsignal, INFINITE);
  1093. ret = WriteDword_2(out_data, Path);
  1094. ReleaseSemaphore(hsignal, 1, 0);
  1095. CloseHandle(hsignal);
  1096. return ret;
  1097. }
  1098. private string AddZero(string InKey)
  1099. {
  1100. int nlen;
  1101. int n;
  1102. nlen = InKey.Length;
  1103. for (n = nlen; n <= 7; n++)
  1104. {
  1105. InKey = "0" + InKey;
  1106. }
  1107. return InKey;
  1108. }
  1109. private void myconvert(string HKey, string LKey, byte[] out_data)
  1110. {
  1111. HKey = AddZero(HKey);
  1112. LKey = AddZero(LKey);
  1113. int n;
  1114. for (n = 0; n <= 3; n++)
  1115. {
  1116. out_data[n] = (byte) HexToInt(HKey.Substring(n * 2, 2));
  1117. }
  1118. for (n = 0; n <= 3; n++)
  1119. {
  1120. out_data[n + 4] = (byte) HexToInt(LKey.Substring(n * 2, 2));
  1121. }
  1122. }
  1123. public int YRead(ref byte indata, int address, string HKey, string LKey, string Path)
  1124. {
  1125. int ret;
  1126. IntPtr hsignal;
  1127. byte[] ary1 = new byte[8];
  1128. if ((address > 495) || (address < 0)) return -81;
  1129. myconvert(HKey, LKey, ary1);
  1130. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1131. WaitForSingleObject(hsignal, INFINITE);
  1132. ret = sub_YRead(ref indata, address, ary1, Path);
  1133. ReleaseSemaphore(hsignal, 1, 0);
  1134. CloseHandle(hsignal);
  1135. return ret;
  1136. }
  1137. private int sub_YRead(ref byte OutData, int address, byte[] password, string Path)
  1138. {
  1139. int n;
  1140. byte[] array_in = new byte[25];
  1141. byte[] array_out = new byte[25];
  1142. IntPtr hUsbDevice = IntPtr.Zero;
  1143. byte opcode;
  1144. if ((address > 495) || (address < 0)) return -81;
  1145. opcode = 128;
  1146. if (address > 255)
  1147. {
  1148. opcode = 160;
  1149. address = address - 256;
  1150. }
  1151. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1152. array_in[1] = 16;
  1153. array_in[2] = opcode;
  1154. array_in[3] = (byte) address;
  1155. array_in[4] = (byte) address;
  1156. for (n = 0; n < 8; n++)
  1157. {
  1158. array_in[5 + n] = password[n];
  1159. }
  1160. if (!SetFeature(hUsbDevice, array_in, 13))
  1161. {
  1162. CloseHandle(hUsbDevice);
  1163. return -93;
  1164. }
  1165. if (!GetFeature(hUsbDevice, array_out, 2))
  1166. {
  1167. CloseHandle(hUsbDevice);
  1168. return -94;
  1169. }
  1170. CloseHandle(hUsbDevice);
  1171. if (array_out[0] != 83)
  1172. {
  1173. return -83;
  1174. }
  1175. OutData = array_out[1];
  1176. return 0;
  1177. }
  1178. public int YWrite(byte indata, int address, string HKey, string LKey, string Path)
  1179. {
  1180. int ret;
  1181. IntPtr hsignal;
  1182. byte[] ary1 = new byte[8];
  1183. if ((address > 495) || (address < 0)) return -81;
  1184. myconvert(HKey, LKey, ary1);
  1185. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1186. WaitForSingleObject(hsignal, INFINITE);
  1187. ret = sub_YWrite(indata, address, ary1, Path);
  1188. ReleaseSemaphore(hsignal, 1, 0);
  1189. CloseHandle(hsignal);
  1190. return ret;
  1191. }
  1192. private int sub_YWrite(byte indata, int address, byte[] password, string Path)
  1193. {
  1194. int n;
  1195. byte[] array_in = new byte[25];
  1196. byte[] array_out = new byte[25];
  1197. IntPtr hUsbDevice = IntPtr.Zero;
  1198. byte opcode;
  1199. if ((address > 511) || (address < 0)) return -81;
  1200. opcode = 64;
  1201. if (address > 255)
  1202. {
  1203. opcode = 96;
  1204. address = address - 256;
  1205. }
  1206. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1207. array_in[1] = 17;
  1208. array_in[2] = opcode;
  1209. array_in[3] = (byte) address;
  1210. array_in[4] = indata;
  1211. for (n = 0; n < 8; n++)
  1212. {
  1213. array_in[5 + n] = password[n];
  1214. }
  1215. if (!SetFeature(hUsbDevice, array_in, 13))
  1216. {
  1217. CloseHandle(hUsbDevice);
  1218. return -93;
  1219. }
  1220. if (!GetFeature(hUsbDevice, array_out, 2))
  1221. {
  1222. CloseHandle(hUsbDevice);
  1223. return -94;
  1224. }
  1225. CloseHandle(hUsbDevice);
  1226. if (array_out[1] != 1)
  1227. {
  1228. return -82;
  1229. }
  1230. return 0;
  1231. }
  1232. public int SetReadPassword(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string Path)
  1233. {
  1234. int ret;
  1235. IntPtr hsignal;
  1236. byte[] ary1 = new byte[8];
  1237. byte[] ary2 = new byte[8];
  1238. short address;
  1239. myconvert(W_HKey, W_LKey, ary1);
  1240. myconvert(new_HKey, new_LKey, ary2);
  1241. address = 496;
  1242. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1243. WaitForSingleObject(hsignal, INFINITE);
  1244. ret = Y_Write(ary2, address, 8, ary1, Path, 0);
  1245. ReleaseSemaphore(hsignal, 1, 0);
  1246. CloseHandle(hsignal);
  1247. return ret;
  1248. }
  1249. public int SetWritePassword(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string Path)
  1250. {
  1251. int ret;
  1252. IntPtr hsignal;
  1253. byte[] ary1 = new byte[8];
  1254. byte[] ary2 = new byte[8];
  1255. short address;
  1256. myconvert(W_HKey, W_LKey, ary1);
  1257. myconvert(new_HKey, new_LKey, ary2);
  1258. address = 504;
  1259. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1260. WaitForSingleObject(hsignal, INFINITE);
  1261. ret = Y_Write(ary2, address, 8, ary1, Path, 0);
  1262. ReleaseSemaphore(hsignal, 1, 0);
  1263. CloseHandle(hsignal);
  1264. return ret;
  1265. }
  1266. public int YWriteString(string InString, int address, string HKey, string LKey, string Path)
  1267. {
  1268. int ret = 0;
  1269. byte[] ary1 = new byte[8];
  1270. IntPtr hsignal;
  1271. int n, trashLen = 0;
  1272. int outlen;
  1273. int total_len;
  1274. int temp_leave;
  1275. int leave;
  1276. byte[] b;
  1277. if ((address < 0)) return -81;
  1278. ret = GetTrashBufLen(Path, ref trashLen);
  1279. if (trashLen < 100) trashLen = 16;
  1280. trashLen = trashLen - 8;
  1281. if (ret != 0) return ret;
  1282. myconvert(HKey, LKey, ary1);
  1283. outlen = lstrlenA(InString); //注意,这里不写入结束字符串,与原来的兼容,也可以写入结束字符串,与原来的不兼容,写入长度会增加1
  1284. b = new byte[outlen];
  1285. CopyStringToByte(b, InString, outlen);
  1286. total_len = address + outlen;
  1287. if (total_len > MAX_LEN) return -47;
  1288. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1289. WaitForSingleObject(hsignal, INFINITE);
  1290. temp_leave = address % trashLen;
  1291. leave = trashLen - temp_leave;
  1292. if (leave > outlen) leave = outlen;
  1293. if (leave > 0)
  1294. {
  1295. for (n = 0; n < (leave / trashLen); n++)
  1296. {
  1297. ret = Y_Write(b, address + n * trashLen, trashLen, ary1, Path, n * trashLen);
  1298. if (ret != 0)
  1299. {
  1300. ReleaseSemaphore(hsignal, 1, 0);
  1301. CloseHandle(hsignal);
  1302. return sub_YWrite_new((InString), address, HKey, LKey, Path);
  1303. }
  1304. }
  1305. if (leave - trashLen * n > 0)
  1306. {
  1307. ret = Y_Write(b, address + n * trashLen, leave - n * trashLen, ary1, Path, trashLen * n);
  1308. if (ret != 0)
  1309. {
  1310. ReleaseSemaphore(hsignal, 1, 0);
  1311. CloseHandle(hsignal);
  1312. return sub_YWrite_new((InString), address, HKey, LKey, Path);
  1313. }
  1314. }
  1315. }
  1316. outlen = outlen - leave;
  1317. address = address + leave;
  1318. if (outlen > 0)
  1319. {
  1320. for (n = 0; n < (outlen / trashLen); n++)
  1321. {
  1322. ret = Y_Write(b, address + n * trashLen, trashLen, ary1, Path, leave + n * trashLen);
  1323. if (ret != 0)
  1324. {
  1325. ReleaseSemaphore(hsignal, 1, 0);
  1326. CloseHandle(hsignal);
  1327. return ret;
  1328. }
  1329. }
  1330. if (outlen - trashLen * n > 0)
  1331. {
  1332. ret = Y_Write(b, address + n * trashLen, outlen - n * trashLen, ary1, Path, leave + trashLen * n);
  1333. if (ret != 0)
  1334. {
  1335. ReleaseSemaphore(hsignal, 1, 0);
  1336. CloseHandle(hsignal);
  1337. return ret;
  1338. }
  1339. }
  1340. }
  1341. ReleaseSemaphore(hsignal, 1, 0);
  1342. CloseHandle(hsignal);
  1343. return ret;
  1344. }
  1345. public int YReadString(ref string OutString, int address, int nlen, string HKey, string LKey, string Path)
  1346. {
  1347. int ret = 0;
  1348. byte[] ary1 = new byte[8];
  1349. IntPtr hsignal;
  1350. int n, trashLen = 0;
  1351. int total_len;
  1352. byte[] outb;
  1353. StringBuilder temp_OutString;
  1354. outb = new byte[nlen];
  1355. myconvert(HKey, LKey, ary1);
  1356. if (address < 0) return -81;
  1357. ret = GetTrashBufLen(Path, ref trashLen);
  1358. if (trashLen < 100) trashLen = 16;
  1359. if (ret != 0) return ret;
  1360. total_len = address + nlen;
  1361. if (total_len > MAX_LEN) return -47;
  1362. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1363. WaitForSingleObject(hsignal, INFINITE);
  1364. for (n = 0; n < (nlen / trashLen); n++)
  1365. {
  1366. ret = Y_Read(outb, address + n * trashLen, trashLen, ary1, Path, n * trashLen);
  1367. if (ret != 0)
  1368. {
  1369. ReleaseSemaphore(hsignal, 1, 0);
  1370. CloseHandle(hsignal);
  1371. return sub_YRead_new(ref OutString, address, nlen, HKey, LKey, Path);
  1372. }
  1373. }
  1374. if (nlen - trashLen * n > 0)
  1375. {
  1376. ret = Y_Read(outb, address + n * trashLen, nlen - trashLen * n, ary1, Path, trashLen * n);
  1377. if (ret != 0)
  1378. {
  1379. ReleaseSemaphore(hsignal, 1, 0);
  1380. CloseHandle(hsignal);
  1381. return sub_YRead_new(ref OutString, address, nlen, HKey, LKey, Path);
  1382. }
  1383. }
  1384. ReleaseSemaphore(hsignal, 1, 0);
  1385. CloseHandle(hsignal);
  1386. temp_OutString = new StringBuilder("", nlen);
  1387. //初始化数据为0,注意,这步一定是需要的
  1388. for (n = 0; n < nlen; n++)
  1389. {
  1390. temp_OutString.Append(0);
  1391. }
  1392. CopyByteToString(temp_OutString, outb, nlen);
  1393. OutString = temp_OutString.ToString();
  1394. return ret;
  1395. }
  1396. public int SetCal_2(string Key, string Path)
  1397. {
  1398. int ret;
  1399. IntPtr hsignal;
  1400. byte[] KeyBuf = new byte[16];
  1401. byte[] inb = new byte[8];
  1402. HexStringToByteArray(Key, ref KeyBuf);
  1403. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1404. WaitForSingleObject(hsignal, INFINITE);
  1405. ret = NT_SetCal_2(KeyBuf, 0, Path, 8);
  1406. if (ret != 0) goto error1;
  1407. ret = NT_SetCal_2(KeyBuf, 1, Path, 0);
  1408. error1:
  1409. ReleaseSemaphore(hsignal, 1, 0);
  1410. CloseHandle(hsignal);
  1411. return ret;
  1412. }
  1413. public int Cal(byte[] InBuf, byte[] outbuf, string Path)
  1414. {
  1415. int ret;
  1416. IntPtr hsignal;
  1417. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1418. WaitForSingleObject(hsignal, INFINITE);
  1419. ret = NT_Cal(InBuf, outbuf, Path, 0);
  1420. ReleaseSemaphore(hsignal, 1, 0);
  1421. CloseHandle(hsignal);
  1422. return ret;
  1423. }
  1424. public int EncString(string InString, ref string OutString, string Path)
  1425. {
  1426. IntPtr hsignal;
  1427. byte[] b;
  1428. byte[] outb;
  1429. int n;
  1430. int nlen, temp_len;
  1431. int ret = 0;
  1432. nlen = lstrlenA(InString) + 1;
  1433. temp_len = nlen;
  1434. if (nlen < 8)
  1435. {
  1436. nlen = 8;
  1437. }
  1438. b = new byte[nlen];
  1439. outb = new byte[nlen];
  1440. CopyStringToByte(b, InString, temp_len);
  1441. b.CopyTo(outb, 0);
  1442. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1443. WaitForSingleObject(hsignal, INFINITE);
  1444. for (n = 0; n <= (nlen - 8); n = n + 8)
  1445. {
  1446. ret = NT_Cal(b, outb, Path, n);
  1447. if (ret != 0) break;
  1448. }
  1449. ReleaseSemaphore(hsignal, 1, 0);
  1450. CloseHandle(hsignal);
  1451. OutString = "";
  1452. for (n = 0; n < nlen; n++)
  1453. {
  1454. OutString = OutString + outb[n].ToString("X2");
  1455. }
  1456. return ret;
  1457. }
  1458. public int sWriteEx(int in_data, ref int out_data, string Path)
  1459. {
  1460. int ret;
  1461. IntPtr hsignal;
  1462. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1463. WaitForSingleObject(hsignal, INFINITE);
  1464. ret = WriteDword(in_data, Path);
  1465. if (ret != 0) goto error1;
  1466. ret = ReadDword(ref out_data, Path);
  1467. error1:
  1468. ReleaseSemaphore(hsignal, 1, 0);
  1469. CloseHandle(hsignal);
  1470. return ret;
  1471. }
  1472. public int sWrite_2Ex(int in_data, ref int out_data, string Path)
  1473. {
  1474. int ret;
  1475. IntPtr hsignal;
  1476. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1477. WaitForSingleObject(hsignal, INFINITE);
  1478. ret = WriteDword_2(in_data, Path);
  1479. if (ret != 0) goto error1;
  1480. ret = ReadDword(ref out_data, Path);
  1481. error1:
  1482. ReleaseSemaphore(hsignal, 1, 0);
  1483. CloseHandle(hsignal);
  1484. return ret;
  1485. }
  1486. public int ReSet(string Path)
  1487. {
  1488. int ret;
  1489. IntPtr hsignal;
  1490. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1491. WaitForSingleObject(hsignal, INFINITE);
  1492. ret = NT_ReSet(Path);
  1493. ReleaseSemaphore(hsignal, 1, 0);
  1494. CloseHandle(hsignal);
  1495. return ret;
  1496. }
  1497. private int NT_ReSet(string Path)
  1498. {
  1499. byte[] array_in = new byte[25];
  1500. byte[] array_out = new byte[25];
  1501. IntPtr hUsbDevice = IntPtr.Zero;
  1502. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1503. array_in[1] = 32;
  1504. if (!SetFeature(hUsbDevice, array_in, 2))
  1505. {
  1506. CloseHandle(hUsbDevice);
  1507. return -93;
  1508. }
  1509. if (!GetFeature(hUsbDevice, array_out, 2))
  1510. {
  1511. CloseHandle(hUsbDevice);
  1512. return -93;
  1513. }
  1514. CloseHandle(hUsbDevice);
  1515. if (array_out[0] != 0)
  1516. {
  1517. return -82;
  1518. }
  1519. return 0;
  1520. }
  1521. public int SetCal_New(string Key, string Path)
  1522. {
  1523. int ret;
  1524. IntPtr hsignal;
  1525. byte[] KeyBuf = new byte[16];
  1526. byte[] inb = new byte[8];
  1527. HexStringToByteArray(Key, ref KeyBuf);
  1528. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1529. WaitForSingleObject(hsignal, INFINITE);
  1530. ret = NT_SetCal_New(KeyBuf, 0, Path, 8);
  1531. if (ret != 0) goto error1;
  1532. ret = NT_SetCal_New(KeyBuf, 1, Path, 0);
  1533. error1:
  1534. ReleaseSemaphore(hsignal, 1, 0);
  1535. CloseHandle(hsignal);
  1536. return ret;
  1537. }
  1538. public int Cal_New(byte[] InBuf, byte[] outbuf, string Path)
  1539. {
  1540. int ret;
  1541. IntPtr hsignal;
  1542. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1543. WaitForSingleObject(hsignal, INFINITE);
  1544. ret = NT_Cal_New(InBuf, outbuf, Path, 0);
  1545. ReleaseSemaphore(hsignal, 1, 0);
  1546. CloseHandle(hsignal);
  1547. return ret;
  1548. }
  1549. public int EncString_New(string InString, ref string OutString, string Path)
  1550. {
  1551. IntPtr hsignal;
  1552. byte[] b;
  1553. byte[] outb;
  1554. int n;
  1555. int nlen, temp_len;
  1556. int ret = 0;
  1557. nlen = lstrlenA(InString) + 1;
  1558. temp_len = nlen;
  1559. if (nlen < 8)
  1560. {
  1561. nlen = 8;
  1562. }
  1563. b = new byte[nlen];
  1564. outb = new byte[nlen];
  1565. CopyStringToByte(b, InString, temp_len);
  1566. b.CopyTo(outb, 0);
  1567. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1568. WaitForSingleObject(hsignal, INFINITE);
  1569. for (n = 0; n <= (nlen - 8); n = n + 8)
  1570. {
  1571. ret = NT_Cal_New(b, outb, Path, n);
  1572. if (ret != 0) break;
  1573. }
  1574. ReleaseSemaphore(hsignal, 1, 0);
  1575. CloseHandle(hsignal);
  1576. OutString = "";
  1577. for (n = 0; n < nlen; n++)
  1578. {
  1579. OutString = OutString + outb[n].ToString("X2");
  1580. }
  1581. return ret;
  1582. }
  1583. public int NT_GetVersionEx(ref short Version, string Path)
  1584. {
  1585. int ret;
  1586. IntPtr hsignal;
  1587. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1588. WaitForSingleObject(hsignal, INFINITE);
  1589. ret = F_GetVersionEx(ref Version, Path);
  1590. ReleaseSemaphore(hsignal, 1, 0);
  1591. CloseHandle(hsignal);
  1592. return ret;
  1593. }
  1594. private int NT_Cal_New(byte[] InBuf, byte[] outbuf, string Path, int pos)
  1595. {
  1596. int n;
  1597. byte[] array_in = new byte[25];
  1598. byte[] array_out = new byte[25];
  1599. IntPtr hUsbDevice = IntPtr.Zero;
  1600. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1601. array_in[1] = 12;
  1602. for (n = 2; n <= 9; n++)
  1603. {
  1604. array_in[n] = InBuf[n - 2 + pos];
  1605. }
  1606. if (!SetFeature(hUsbDevice, array_in, 9))
  1607. {
  1608. CloseHandle(hUsbDevice);
  1609. return -93;
  1610. }
  1611. if (!GetFeature(hUsbDevice, array_out, 9))
  1612. {
  1613. CloseHandle(hUsbDevice);
  1614. return -93;
  1615. }
  1616. CloseHandle(hUsbDevice);
  1617. for (n = 0; n < 8; n++)
  1618. {
  1619. outbuf[n + pos] = array_out[n];
  1620. }
  1621. if (array_out[8] != 0x55)
  1622. {
  1623. return -20;
  1624. }
  1625. return 0;
  1626. }
  1627. private int NT_SetCal_New(byte[] indata, byte IsHi, string Path, short pos)
  1628. {
  1629. int n;
  1630. byte[] array_in = new byte[25];
  1631. byte[] array_out = new byte[25];
  1632. IntPtr hUsbDevice = IntPtr.Zero;
  1633. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1634. array_in[1] = 13;
  1635. array_in[2] = IsHi;
  1636. for (n = 0; n < 8; n++)
  1637. {
  1638. array_in[3 + n] = indata[n + pos];
  1639. }
  1640. if (!SetFeature(hUsbDevice, array_in, 11))
  1641. {
  1642. CloseHandle(hUsbDevice);
  1643. return -93;
  1644. }
  1645. if (!GetFeature(hUsbDevice, array_out, 2))
  1646. {
  1647. CloseHandle(hUsbDevice);
  1648. return -94;
  1649. }
  1650. CloseHandle(hUsbDevice);
  1651. if (array_out[0] != 0)
  1652. {
  1653. return -82;
  1654. }
  1655. return 0;
  1656. }
  1657. private int F_GetVersionEx(ref short Version, string Path)
  1658. {
  1659. byte[] array_in = new byte[25];
  1660. byte[] array_out = new byte[25];
  1661. IntPtr hUsbDevice = IntPtr.Zero;
  1662. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1663. array_in[1] = 5;
  1664. if (!SetFeature(hUsbDevice, array_in, 1))
  1665. {
  1666. CloseHandle(hUsbDevice);
  1667. return -93;
  1668. }
  1669. if (!GetFeature(hUsbDevice, array_out, 1))
  1670. {
  1671. CloseHandle(hUsbDevice);
  1672. return -93;
  1673. }
  1674. CloseHandle(hUsbDevice);
  1675. Version = array_out[0];
  1676. return 0;
  1677. }
  1678. int sub_YRead_new(ref string OutString, int Address, int nlen, string HKey, string LKey, string Path)
  1679. {
  1680. int n, ret = 0;
  1681. byte[] outb;
  1682. StringBuilder temp_OutString;
  1683. outb = new byte[nlen];
  1684. for (n = 0; n < nlen; n++)
  1685. {
  1686. ret = YRead(ref outb[n], Address + n, HKey, LKey, Path);
  1687. if (ret != 0) return ret;
  1688. }
  1689. temp_OutString = new StringBuilder("", nlen);
  1690. //初始化数据为0,注意,这步一定是需要的
  1691. for (n = 0; n < nlen; n++)
  1692. {
  1693. temp_OutString.Append(0);
  1694. }
  1695. CopyByteToString(temp_OutString, outb, nlen);
  1696. OutString = temp_OutString.ToString();
  1697. return ret;
  1698. }
  1699. int sub_YWrite_new(string InString, int Address, string HKey, string LKey, string Path)
  1700. {
  1701. int n, ret = 0;
  1702. byte[] b;
  1703. int outlen = lstrlenA(InString); //注意,这里不写入结束字符串,与原来的兼容,也可以写入结束字符串,与原来的不兼容,写入长度会增加1
  1704. b = new byte[outlen];
  1705. CopyStringToByte(b, InString, outlen);
  1706. for (n = 0; n < outlen; n++)
  1707. {
  1708. ret = YWrite(b[n], Address + n, HKey, LKey, Path);
  1709. if (ret != 0) return ret;
  1710. }
  1711. return ret;
  1712. }
  1713. int sub_YReadEx(byte[] OutData, int Address, int nlen, string HKey, string LKey, string Path)
  1714. {
  1715. int n, ret = 0;
  1716. for (n = 0; n < nlen; n++)
  1717. {
  1718. ret = YRead(ref OutData[n], Address + n, HKey, LKey, Path);
  1719. if (ret != 0) return ret;
  1720. }
  1721. return ret;
  1722. }
  1723. int sub_YWriteEx(byte[] indata, int Address, int len, string HKey, string LKey, string Path)
  1724. {
  1725. int n, ret = 0;
  1726. for (n = 0; n < len; n++)
  1727. {
  1728. ret = YWrite(indata[n], Address + n, HKey, LKey, Path);
  1729. if (ret != 0) return ret;
  1730. }
  1731. return ret;
  1732. }
  1733. public int SetHidOnly(bool IsHidOnly, string Path)
  1734. {
  1735. int ret;
  1736. IntPtr hsignal;
  1737. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1738. WaitForSingleObject(hsignal, INFINITE);
  1739. ret = NT_SetHidOnly(IsHidOnly, Path);
  1740. ReleaseSemaphore(hsignal, 1, 0);
  1741. CloseHandle(hsignal);
  1742. return ret;
  1743. }
  1744. private int NT_SetHidOnly(bool IsHidOnly, string Path)
  1745. {
  1746. byte[] array_in = new byte[25];
  1747. byte[] array_out = new byte[25];
  1748. IntPtr hUsbDevice = IntPtr.Zero;
  1749. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1750. array_in[1] = 0x55;
  1751. if (IsHidOnly) array_in[2] = 0;
  1752. else array_in[2] = 0xff;
  1753. if (!SetFeature(hUsbDevice, array_in, 3))
  1754. {
  1755. CloseHandle(hUsbDevice);
  1756. return -93;
  1757. }
  1758. if (!GetFeature(hUsbDevice, array_out, 1))
  1759. {
  1760. CloseHandle(hUsbDevice);
  1761. return -93;
  1762. }
  1763. CloseHandle(hUsbDevice);
  1764. if (array_out[0] != 0)
  1765. {
  1766. return -82;
  1767. }
  1768. return 0;
  1769. }
  1770. public int SetUReadOnly(string Path)
  1771. {
  1772. int ret;
  1773. IntPtr hsignal;
  1774. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1775. WaitForSingleObject(hsignal, INFINITE);
  1776. ret = NT_SetUReadOnly(Path);
  1777. ReleaseSemaphore(hsignal, 1, 0);
  1778. CloseHandle(hsignal);
  1779. return ret;
  1780. }
  1781. private int NT_SetUReadOnly(string Path)
  1782. {
  1783. byte[] array_in = new byte[25];
  1784. byte[] array_out = new byte[25];
  1785. IntPtr hUsbDevice = IntPtr.Zero;
  1786. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1787. array_in[1] = 0x56;
  1788. if (!SetFeature(hUsbDevice, array_in, 3))
  1789. {
  1790. CloseHandle(hUsbDevice);
  1791. return -93;
  1792. }
  1793. if (!GetFeature(hUsbDevice, array_out, 1))
  1794. {
  1795. CloseHandle(hUsbDevice);
  1796. return -93;
  1797. }
  1798. CloseHandle(hUsbDevice);
  1799. if (array_out[0] != 0)
  1800. {
  1801. return -82;
  1802. }
  1803. return 0;
  1804. }
  1805. private int sub_GetTrashBufLen(IntPtr hDevice, ref int out_len)
  1806. {
  1807. IntPtr Ppd = System.IntPtr.Zero;
  1808. HIDP_CAPS Caps = new HIDP_CAPS();
  1809. if (!HidD_GetPreparsedData(hDevice, ref Ppd)) return -93;
  1810. if (HidP_GetCaps(Ppd, ref Caps) <= 0)
  1811. {
  1812. HidD_FreePreparsedData(Ppd);
  1813. return -93;
  1814. }
  1815. HidD_FreePreparsedData(Ppd);
  1816. out_len = Caps.FeatureReportByteLength - 5;
  1817. return 0;
  1818. }
  1819. private int GetTrashBufLen(string Path, ref int out_len)
  1820. {
  1821. IntPtr hUsbDevice = IntPtr.Zero;
  1822. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1823. int ret = sub_GetTrashBufLen(hUsbDevice, ref out_len);
  1824. CloseHandle(hUsbDevice);
  1825. return ret;
  1826. }
  1827. private int NT_Set_SM2_KeyPair(byte[] PriKey, byte[] PubKeyX, byte[] PubKeyY, byte[] sm2_UerName, string Path)
  1828. {
  1829. byte[] array_in = new byte[256];
  1830. byte[] array_out = new byte[25];
  1831. int n = 0;
  1832. IntPtr hUsbDevice = IntPtr.Zero;
  1833. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1834. array_in[1] = 0x32;
  1835. for (n = 0; n < ECC_MAXLEN; n++)
  1836. {
  1837. array_in[2 + n + ECC_MAXLEN * 0] = PriKey[n];
  1838. array_in[2 + n + ECC_MAXLEN * 1] = PubKeyX[n];
  1839. array_in[2 + n + ECC_MAXLEN * 2] = PubKeyY[n];
  1840. }
  1841. for (n = 0; n < SM2_USENAME_LEN; n++)
  1842. {
  1843. array_in[2 + n + ECC_MAXLEN * 3] = sm2_UerName[n];
  1844. }
  1845. if (!SetFeature(hUsbDevice, array_in, ECC_MAXLEN * 3 + SM2_USENAME_LEN + 2))
  1846. {
  1847. CloseHandle(hUsbDevice);
  1848. return -93;
  1849. }
  1850. if (!GetFeature(hUsbDevice, array_out, 2))
  1851. {
  1852. CloseHandle(hUsbDevice);
  1853. return -94;
  1854. }
  1855. CloseHandle(hUsbDevice);
  1856. if (array_out[0] != 0x20) return USBStatusFail;
  1857. return 0;
  1858. }
  1859. private int NT_GenKeyPair(byte[] PriKey, byte[] PubKey, string Path)
  1860. {
  1861. byte[] array_in = new byte[256];
  1862. byte[] array_out = new byte[256];
  1863. int n = 0;
  1864. IntPtr hUsbDevice = IntPtr.Zero;
  1865. array_out[0] = 0xfb;
  1866. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1867. array_in[1] = GEN_KEYPAIR;
  1868. if (!SetFeature(hUsbDevice, array_in, 2))
  1869. {
  1870. CloseHandle(hUsbDevice);
  1871. return -93;
  1872. }
  1873. if (!GetFeature(hUsbDevice, array_out, ECC_MAXLEN * 3 + 2))
  1874. {
  1875. CloseHandle(hUsbDevice);
  1876. return -94;
  1877. }
  1878. CloseHandle(hUsbDevice);
  1879. if (array_out[0] != 0x20)
  1880. {
  1881. return FAILEDGENKEYPAIR; //表示读取失败;
  1882. }
  1883. for (n = 0; n < ECC_MAXLEN; n++)
  1884. {
  1885. PriKey[n] = array_out[1 + ECC_MAXLEN * 0 + n];
  1886. }
  1887. for (n = 0; n < (ECC_MAXLEN * 2 + 1); n++)
  1888. {
  1889. PubKey[n] = array_out[1 + ECC_MAXLEN * 1 + n];
  1890. }
  1891. return 0;
  1892. }
  1893. private int NT_GetChipID(byte[] OutChipID, string Path)
  1894. {
  1895. int[] t = new int[8];
  1896. int n;
  1897. byte[] array_in = new byte[25];
  1898. byte[] array_out = new byte[25];
  1899. IntPtr hUsbDevice = IntPtr.Zero;
  1900. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1901. array_in[1] = GET_CHIPID;
  1902. if (!SetFeature(hUsbDevice, array_in, 1))
  1903. {
  1904. CloseHandle(hUsbDevice);
  1905. return -93;
  1906. }
  1907. if (!GetFeature(hUsbDevice, array_out, 17))
  1908. {
  1909. CloseHandle(hUsbDevice);
  1910. return -93;
  1911. }
  1912. CloseHandle(hUsbDevice);
  1913. if (array_out[0] != 0x20) return USBStatusFail;
  1914. for (n = 0; n < 16; n++)
  1915. {
  1916. OutChipID[n] = array_out[1 + n];
  1917. }
  1918. return 0;
  1919. }
  1920. private int NT_Get_SM2_PubKey(byte[] KGx, byte[] KGy, byte[] sm2_UerName, string Path)
  1921. {
  1922. byte[] array_in = new byte[256];
  1923. byte[] array_out = new byte[256];
  1924. int n = 0;
  1925. IntPtr hUsbDevice = IntPtr.Zero;
  1926. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1927. array_in[1] = 0x33;
  1928. if (!SetFeature(hUsbDevice, array_in, 2))
  1929. {
  1930. CloseHandle(hUsbDevice);
  1931. return -93;
  1932. }
  1933. if (!GetFeature(hUsbDevice, array_out, ECC_MAXLEN * 2 + SM2_USENAME_LEN + 2))
  1934. {
  1935. CloseHandle(hUsbDevice);
  1936. return -94;
  1937. }
  1938. CloseHandle(hUsbDevice);
  1939. if (array_out[0] != 0x20) return USBStatusFail;
  1940. for (n = 0; n < ECC_MAXLEN; n++)
  1941. {
  1942. KGx[n] = array_out[1 + ECC_MAXLEN * 0 + n];
  1943. KGy[n] = array_out[1 + ECC_MAXLEN * 1 + n];
  1944. }
  1945. for (n = 0; n < SM2_USENAME_LEN; n++)
  1946. {
  1947. sm2_UerName[n] = array_out[1 + ECC_MAXLEN * 2 + n];
  1948. }
  1949. return 0;
  1950. }
  1951. private int NT_Set_Pin(string old_pin, string new_pin, string Path)
  1952. {
  1953. byte[] array_in = new byte[256];
  1954. byte[] array_out = new byte[256];
  1955. int n = 0;
  1956. IntPtr hUsbDevice = IntPtr.Zero;
  1957. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1958. array_in[1] = SET_PIN;
  1959. byte[] b_oldpin = new byte[PIN_LEN];
  1960. CopyStringToByte(b_oldpin, old_pin, PIN_LEN);
  1961. byte[] b_newpin = new byte[PIN_LEN];
  1962. CopyStringToByte(b_newpin, new_pin, PIN_LEN);
  1963. for (n = 0; n < PIN_LEN; n++)
  1964. {
  1965. array_in[2 + PIN_LEN * 0 + n] = b_oldpin[n];
  1966. array_in[2 + PIN_LEN * 1 + n] = b_newpin[n];
  1967. }
  1968. if (!SetFeature(hUsbDevice, array_in, PIN_LEN * 2 + 2))
  1969. {
  1970. CloseHandle(hUsbDevice);
  1971. return -93;
  1972. }
  1973. if (!GetFeature(hUsbDevice, array_out, 2))
  1974. {
  1975. CloseHandle(hUsbDevice);
  1976. return -94;
  1977. }
  1978. CloseHandle(hUsbDevice);
  1979. if (array_out[0] != 0x20) return USBStatusFail;
  1980. if (array_out[1] != 0x20) return FAILPINPWD;
  1981. return 0;
  1982. }
  1983. private int NT_SM2_Enc(byte[] inbuf, byte[] outbuf, byte inlen, string Path)
  1984. {
  1985. byte[] array_in = new byte[256];
  1986. byte[] array_out = new byte[256];
  1987. int n = 0;
  1988. IntPtr hUsbDevice = IntPtr.Zero;
  1989. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1990. array_in[1] = MYENC;
  1991. array_in[2] = inlen;
  1992. for (n = 0; n < inlen; n++)
  1993. {
  1994. array_in[3 + n] = inbuf[n];
  1995. }
  1996. if (!SetFeature(hUsbDevice, array_in, inlen + 1 + 2))
  1997. {
  1998. CloseHandle(hUsbDevice);
  1999. return -93;
  2000. }
  2001. if (!GetFeature(hUsbDevice, array_out, inlen + SM2_ADDBYTE + 3))
  2002. {
  2003. CloseHandle(hUsbDevice);
  2004. return -94;
  2005. }
  2006. CloseHandle(hUsbDevice);
  2007. if (array_out[0] != 0x20) return USBStatusFail;
  2008. if (array_out[1] == 0) return FAILENC;
  2009. for (n = 0; n < (inlen + SM2_ADDBYTE); n++)
  2010. {
  2011. outbuf[n] = array_out[2 + n];
  2012. }
  2013. return 0;
  2014. }
  2015. private int NT_SM2_Dec(byte[] inbuf, byte[] outbuf, byte inlen, string pin, string Path)
  2016. {
  2017. byte[] array_in = new byte[256];
  2018. byte[] array_out = new byte[256];
  2019. int n = 0;
  2020. IntPtr hUsbDevice = IntPtr.Zero;
  2021. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  2022. array_in[1] = MYDEC;
  2023. byte[] b_pin = new byte[PIN_LEN];
  2024. CopyStringToByte(b_pin, pin, PIN_LEN);
  2025. for (n = 0; n < PIN_LEN; n++)
  2026. {
  2027. array_in[2 + PIN_LEN * 0 + n] = b_pin[n];
  2028. }
  2029. array_in[2 + PIN_LEN] = inlen;
  2030. for (n = 0; n < inlen; n++)
  2031. {
  2032. array_in[2 + PIN_LEN + 1 + n] = inbuf[n];
  2033. }
  2034. if (!SetFeature(hUsbDevice, array_in, inlen + 1 + 2 + PIN_LEN))
  2035. {
  2036. CloseHandle(hUsbDevice);
  2037. return -93;
  2038. }
  2039. if (!GetFeature(hUsbDevice, array_out, inlen - SM2_ADDBYTE + 4))
  2040. {
  2041. CloseHandle(hUsbDevice);
  2042. return -94;
  2043. }
  2044. CloseHandle(hUsbDevice);
  2045. if (array_out[2] != 0x20) return FAILPINPWD;
  2046. if (array_out[1] == 0) return FAILENC;
  2047. if (array_out[0] != 0x20) return USBStatusFail;
  2048. for (n = 0; n < (inlen - SM2_ADDBYTE); n++)
  2049. {
  2050. outbuf[n] = array_out[3 + n];
  2051. }
  2052. return 0;
  2053. }
  2054. private int NT_Sign(byte[] inbuf, byte[] outbuf, string pin, string Path)
  2055. {
  2056. byte[] array_in = new byte[256];
  2057. byte[] array_out = new byte[256];
  2058. int n = 0;
  2059. IntPtr hUsbDevice = IntPtr.Zero;
  2060. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  2061. array_in[1] = YTSIGN;
  2062. byte[] b_pin = new byte[PIN_LEN];
  2063. CopyStringToByte(b_pin, pin, PIN_LEN);
  2064. for (n = 0; n < PIN_LEN; n++)
  2065. {
  2066. array_in[2 + PIN_LEN * 0 + n] = b_pin[n];
  2067. }
  2068. for (n = 0; n < 32; n++)
  2069. {
  2070. array_in[2 + PIN_LEN + n] = inbuf[n];
  2071. }
  2072. if (!SetFeature(hUsbDevice, array_in, 32 + 2 + PIN_LEN))
  2073. {
  2074. CloseHandle(hUsbDevice);
  2075. return -93;
  2076. }
  2077. if (!GetFeature(hUsbDevice, array_out, 64 + 3))
  2078. {
  2079. CloseHandle(hUsbDevice);
  2080. return -94;
  2081. }
  2082. CloseHandle(hUsbDevice);
  2083. if (array_out[1] != 0x20) return FAILPINPWD;
  2084. if (array_out[0] != 0x20) return USBStatusFail;
  2085. for (n = 0; n < 64; n++)
  2086. {
  2087. outbuf[n] = array_out[2 + n];
  2088. }
  2089. return 0;
  2090. }
  2091. private int NT_Sign_2(byte[] inbuf, byte[] outbuf, string pin, string Path)
  2092. {
  2093. byte[] array_in = new byte[256];
  2094. byte[] array_out = new byte[256];
  2095. int n = 0;
  2096. IntPtr hUsbDevice = IntPtr.Zero;
  2097. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  2098. array_in[1] = YTSIGN_2;
  2099. byte[] b_pin = new byte[PIN_LEN];
  2100. CopyStringToByte(b_pin, pin, PIN_LEN);
  2101. for (n = 0; n < PIN_LEN; n++)
  2102. {
  2103. array_in[2 + PIN_LEN * 0 + n] = b_pin[n];
  2104. }
  2105. for (n = 0; n < 32; n++)
  2106. {
  2107. array_in[2 + PIN_LEN + n] = inbuf[n];
  2108. }
  2109. if (!SetFeature(hUsbDevice, array_in, 32 + 2 + PIN_LEN))
  2110. {
  2111. CloseHandle(hUsbDevice);
  2112. return -93;
  2113. }
  2114. if (!GetFeature(hUsbDevice, array_out, 64 + 3))
  2115. {
  2116. CloseHandle(hUsbDevice);
  2117. return -94;
  2118. }
  2119. CloseHandle(hUsbDevice);
  2120. if (array_out[1] != 0x20) return FAILPINPWD;
  2121. if (array_out[0] != 0x20) return USBStatusFail;
  2122. for (n = 0; n < 64; n++)
  2123. {
  2124. outbuf[n] = array_out[2 + n];
  2125. }
  2126. return 0;
  2127. }
  2128. private int NT_Verfiy(byte[] inbuf, byte[] InSignBuf, ref bool outbiao, string Path)
  2129. {
  2130. byte[] array_in = new byte[256];
  2131. byte[] array_out = new byte[256];
  2132. int n = 0;
  2133. IntPtr hUsbDevice = IntPtr.Zero;
  2134. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  2135. array_in[1] = YTVERIFY;
  2136. for (n = 0; n < 32; n++)
  2137. {
  2138. array_in[2 + n] = inbuf[n];
  2139. }
  2140. for (n = 0; n < 64; n++)
  2141. {
  2142. array_in[2 + 32 + n] = InSignBuf[n];
  2143. }
  2144. if (!SetFeature(hUsbDevice, array_in, 32 + 2 + 64))
  2145. {
  2146. CloseHandle(hUsbDevice);
  2147. return -93;
  2148. }
  2149. if (!GetFeature(hUsbDevice, array_out, 3))
  2150. {
  2151. CloseHandle(hUsbDevice);
  2152. return -94;
  2153. }
  2154. CloseHandle(hUsbDevice);
  2155. outbiao = (array_out[1] != 0);
  2156. if (array_out[0] != 0x20) return USBStatusFail;
  2157. return 0;
  2158. }
  2159. private string ByteArrayToHexString(byte[] in_data, int nlen)
  2160. {
  2161. string OutString = "";
  2162. int n;
  2163. for (n = 0; n < nlen; n++)
  2164. {
  2165. OutString = OutString + in_data[n].ToString("X2");
  2166. }
  2167. return OutString;
  2168. }
  2169. public int YT_GenKeyPair(ref string PriKey, ref string PubKeyX, ref string PubKeyY, string InPath)
  2170. {
  2171. int ret, n;
  2172. byte[] b_PriKey = new byte[ECC_MAXLEN], b_PubKey = new byte[ECC_MAXLEN * 2 + 1]; //其中第一个字节是标志位,忽略
  2173. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2174. WaitForSingleObject(hsignal, INFINITE);
  2175. ret = NT_GenKeyPair(b_PriKey, b_PubKey, InPath);
  2176. ReleaseSemaphore(hsignal, 1, 0);
  2177. CloseHandle(hsignal);
  2178. PriKey = ByteArrayToHexString(b_PriKey, ECC_MAXLEN);
  2179. PubKeyX = "";
  2180. PubKeyY = "";
  2181. for (n = 0; n < ECC_MAXLEN; n++)
  2182. {
  2183. PubKeyX = PubKeyX + b_PubKey[n + 1].ToString("X2");
  2184. PubKeyY = PubKeyY + b_PubKey[n + 1 + ECC_MAXLEN].ToString("X2");
  2185. }
  2186. return ret;
  2187. }
  2188. public int Set_SM2_KeyPair(string PriKey, string PubKeyX, string PubKeyY, string SM2_UserName, string InPath)
  2189. {
  2190. int ret;
  2191. byte[] b_PriKey = new byte[ECC_MAXLEN],
  2192. b_PubKeyX = new byte[ECC_MAXLEN],
  2193. b_PubKeyY = new byte[ECC_MAXLEN],
  2194. b_SM2UserName = new byte[SM2_USENAME_LEN];
  2195. HexStringToByteArray(PriKey, ref b_PriKey);
  2196. HexStringToByteArray(PubKeyX, ref b_PubKeyX);
  2197. HexStringToByteArray(PubKeyY, ref b_PubKeyY);
  2198. CopyStringToByte(b_SM2UserName, SM2_UserName, SM2_USENAME_LEN);
  2199. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2200. WaitForSingleObject(hsignal, INFINITE);
  2201. ret = NT_Set_SM2_KeyPair(b_PriKey, b_PubKeyX, b_PubKeyY, b_SM2UserName, InPath);
  2202. ReleaseSemaphore(hsignal, 1, 0);
  2203. CloseHandle(hsignal);
  2204. return ret;
  2205. }
  2206. public int Get_SM2_PubKey(ref string PubKeyX, ref string PubKeyY, ref string sm2UserName, string InPath)
  2207. {
  2208. int ret;
  2209. byte[] b_PubKeyX = new byte[ECC_MAXLEN],
  2210. b_PubKeyY = new byte[ECC_MAXLEN],
  2211. b_sm2UserName = new byte[SM2_USENAME_LEN];
  2212. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2213. WaitForSingleObject(hsignal, INFINITE);
  2214. ret = NT_Get_SM2_PubKey(b_PubKeyX, b_PubKeyY, b_sm2UserName, InPath);
  2215. ReleaseSemaphore(hsignal, 1, 0);
  2216. CloseHandle(hsignal);
  2217. PubKeyX = ByteArrayToHexString(b_PubKeyX, ECC_MAXLEN);
  2218. PubKeyY = ByteArrayToHexString(b_PubKeyY, ECC_MAXLEN);
  2219. StringBuilder c_str = new StringBuilder("", SM2_USENAME_LEN);
  2220. CopyByteToString(c_str, b_sm2UserName, SM2_USENAME_LEN);
  2221. sm2UserName = c_str.ToString();
  2222. return ret;
  2223. }
  2224. public int GetChipID(ref string OutChipID, string InPath)
  2225. {
  2226. int ret;
  2227. byte[] b_OutChipID = new byte[16];
  2228. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2229. WaitForSingleObject(hsignal, INFINITE);
  2230. ret = NT_GetChipID(b_OutChipID, InPath);
  2231. ReleaseSemaphore(hsignal, 1, 0);
  2232. CloseHandle(hsignal);
  2233. OutChipID = ByteArrayToHexString(b_OutChipID, 16);
  2234. return ret;
  2235. }
  2236. public int SM2_EncBuf(byte[] InBuf, byte[] OutBuf, int inlen, string InPath)
  2237. {
  2238. int ret = 0, n, temp_inlen, incount = 0, outcount = 0;
  2239. byte[] temp_InBuf = new byte[MAX_ENCLEN + SM2_ADDBYTE], temp_OutBuf = new byte[MAX_ENCLEN + SM2_ADDBYTE];
  2240. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2241. WaitForSingleObject(hsignal, INFINITE);
  2242. while (inlen > 0)
  2243. {
  2244. if (inlen > MAX_ENCLEN)
  2245. temp_inlen = MAX_ENCLEN;
  2246. else
  2247. temp_inlen = inlen;
  2248. for (n = 0; n < temp_inlen; n++)
  2249. {
  2250. temp_InBuf[n] = InBuf[incount + n];
  2251. }
  2252. ret = NT_SM2_Enc(temp_InBuf, temp_OutBuf, (byte) temp_inlen, InPath);
  2253. for (n = 0; n < (temp_inlen + SM2_ADDBYTE); n++)
  2254. {
  2255. OutBuf[outcount + n] = temp_OutBuf[n];
  2256. }
  2257. if (ret != 0) goto err;
  2258. inlen = inlen - MAX_ENCLEN;
  2259. incount = incount + MAX_ENCLEN;
  2260. outcount = outcount + MAX_DECLEN;
  2261. }
  2262. err:
  2263. ReleaseSemaphore(hsignal, 1, 0);
  2264. CloseHandle(hsignal);
  2265. return ret;
  2266. }
  2267. public int SM2_DecBuf(byte[] InBuf, byte[] OutBuf, int inlen, string pin, string InPath)
  2268. {
  2269. int ret = 0, temp_inlen, n, incount = 0, outcount = 0;
  2270. byte[] temp_InBuf = new byte[MAX_ENCLEN + SM2_ADDBYTE], temp_OutBuf = new byte[MAX_ENCLEN + SM2_ADDBYTE];
  2271. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2272. WaitForSingleObject(hsignal, INFINITE);
  2273. while (inlen > 0)
  2274. {
  2275. if (inlen > MAX_DECLEN)
  2276. temp_inlen = MAX_DECLEN;
  2277. else
  2278. temp_inlen = inlen;
  2279. for (n = 0; n < temp_inlen; n++)
  2280. {
  2281. temp_InBuf[n] = InBuf[incount + n];
  2282. }
  2283. ret = NT_SM2_Dec(InBuf, temp_OutBuf, (byte) temp_inlen, pin, InPath);
  2284. for (n = 0; n < (temp_inlen - SM2_ADDBYTE); n++)
  2285. {
  2286. OutBuf[outcount + n] = temp_OutBuf[n];
  2287. }
  2288. if (ret != 0) goto err;
  2289. inlen = inlen - MAX_DECLEN;
  2290. incount = incount + MAX_DECLEN;
  2291. outcount = outcount + MAX_ENCLEN;
  2292. }
  2293. err:
  2294. ReleaseSemaphore(hsignal, 1, 0);
  2295. CloseHandle(hsignal);
  2296. return ret;
  2297. }
  2298. public int SM2_EncString(string InString, ref string OutString, string InPath)
  2299. {
  2300. int n, incount = 0, outcount = 0;
  2301. byte[] temp_InBuf = new byte[MAX_ENCLEN + SM2_ADDBYTE], temp_OutBuf = new byte[MAX_ENCLEN + SM2_ADDBYTE];
  2302. int inlen = lstrlenA(InString) + 1;
  2303. int outlen = (inlen / (MAX_ENCLEN) + 1) * SM2_ADDBYTE + inlen;
  2304. byte[] OutBuf = new byte[outlen];
  2305. byte[] InBuf = new byte[inlen];
  2306. CopyStringToByte(InBuf, InString, inlen);
  2307. int ret = 0, temp_inlen;
  2308. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2309. WaitForSingleObject(hsignal, INFINITE);
  2310. while (inlen > 0)
  2311. {
  2312. if (inlen > MAX_ENCLEN)
  2313. temp_inlen = MAX_ENCLEN;
  2314. else
  2315. temp_inlen = inlen;
  2316. for (n = 0; n < temp_inlen; n++)
  2317. {
  2318. temp_InBuf[n] = InBuf[incount + n];
  2319. }
  2320. ret = NT_SM2_Enc(temp_InBuf, temp_OutBuf, (byte) temp_inlen, InPath);
  2321. for (n = 0; n < (temp_inlen + SM2_ADDBYTE); n++)
  2322. {
  2323. OutBuf[outcount + n] = temp_OutBuf[n];
  2324. }
  2325. if (ret != 0) goto err;
  2326. inlen = inlen - MAX_ENCLEN;
  2327. incount = incount + MAX_ENCLEN;
  2328. outcount = outcount + MAX_DECLEN;
  2329. }
  2330. err:
  2331. OutString = ByteArrayToHexString(OutBuf, outlen);
  2332. ReleaseSemaphore(hsignal, 1, 0);
  2333. CloseHandle(hsignal);
  2334. return ret;
  2335. }
  2336. public int SM2_DecString(string InString, ref string OutString, string pin, string InPath)
  2337. {
  2338. int n, incount = 0, outcount = 0;
  2339. byte[] temp_InBuf = new byte[MAX_ENCLEN + SM2_ADDBYTE], temp_OutBuf = new byte[MAX_ENCLEN + SM2_ADDBYTE];
  2340. int inlen = lstrlenA(InString) / 2;
  2341. int outlen = inlen - (inlen / (MAX_DECLEN) + 1) * SM2_ADDBYTE;
  2342. byte[] InBuf = new byte[inlen];
  2343. byte[] OutBuf = new byte[outlen];
  2344. int ret = 0, temp_inlen;
  2345. HexStringToByteArray(InString, ref InBuf);
  2346. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2347. WaitForSingleObject(hsignal, INFINITE);
  2348. while (inlen > 0)
  2349. {
  2350. if (inlen > MAX_DECLEN)
  2351. temp_inlen = MAX_DECLEN;
  2352. else
  2353. temp_inlen = inlen;
  2354. for (n = 0; n < temp_inlen; n++)
  2355. {
  2356. temp_InBuf[n] = InBuf[incount + n];
  2357. }
  2358. ret = NT_SM2_Dec(temp_InBuf, temp_OutBuf, (byte) temp_inlen, pin, InPath);
  2359. for (n = 0; n < (temp_inlen - SM2_ADDBYTE); n++)
  2360. {
  2361. OutBuf[outcount + n] = temp_OutBuf[n];
  2362. }
  2363. if (ret != 0) goto err;
  2364. inlen = inlen - MAX_DECLEN;
  2365. incount = incount + MAX_DECLEN;
  2366. outcount = outcount + MAX_ENCLEN;
  2367. }
  2368. err:
  2369. ReleaseSemaphore(hsignal, 1, 0);
  2370. CloseHandle(hsignal);
  2371. StringBuilder c_str = new StringBuilder("", outlen);
  2372. CopyByteToString(c_str, OutBuf, outlen);
  2373. OutString = c_str.ToString();
  2374. return ret;
  2375. }
  2376. public int YtSetPin(string old_pin, string new_pin, string InPath)
  2377. {
  2378. int ret;
  2379. IntPtr hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2380. WaitForSingleObject(hsignal, INFINITE);
  2381. ret = NT_Set_Pin(old_pin, new_pin, InPath);
  2382. ReleaseSemaphore(hsignal, 1, 0);
  2383. CloseHandle(hsignal);
  2384. return ret;
  2385. }
  2386. private int NT_SetID(byte[] InBuf, string Path)
  2387. {
  2388. int n;
  2389. byte[] array_in = new byte[25];
  2390. byte[] array_out = new byte[25];
  2391. IntPtr hUsbDevice = IntPtr.Zero;
  2392. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  2393. array_in[1] = 7;
  2394. for (n = 2; n <= 9; n++)
  2395. {
  2396. array_in[n] = InBuf[n - 2];
  2397. }
  2398. if (!SetFeature(hUsbDevice, array_in, 9))
  2399. {
  2400. CloseHandle(hUsbDevice);
  2401. return -93;
  2402. }
  2403. if (!GetFeature(hUsbDevice, array_out, 9))
  2404. {
  2405. CloseHandle(hUsbDevice);
  2406. return -93;
  2407. }
  2408. CloseHandle(hUsbDevice);
  2409. if (array_out[0] != 0x0)
  2410. {
  2411. return -82;
  2412. }
  2413. return 0;
  2414. }
  2415. public int SetID(string Seed, string Path)
  2416. {
  2417. int ret;
  2418. IntPtr hsignal;
  2419. int n;
  2420. byte[] KeyBuf = new byte[8];
  2421. for (n = 0; n < 8; n++)
  2422. {
  2423. KeyBuf[n] = 0;
  2424. }
  2425. HexStringToByteArray(Seed, ref KeyBuf);
  2426. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2427. WaitForSingleObject(hsignal, INFINITE);
  2428. ret = NT_SetID(KeyBuf, Path);
  2429. ReleaseSemaphore(hsignal, 1, 0);
  2430. CloseHandle(hsignal);
  2431. return ret;
  2432. }
  2433. private int NT_GetProduceDate(ref string OutDate, string Path)
  2434. {
  2435. int n;
  2436. byte[] array_in = new byte[25];
  2437. byte[] array_out = new byte[25];
  2438. IntPtr hUsbDevice = IntPtr.Zero;
  2439. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  2440. array_in[1] = 15;
  2441. if (!SetFeature(hUsbDevice, array_in, 1))
  2442. {
  2443. CloseHandle(hUsbDevice);
  2444. return -93;
  2445. }
  2446. if (!GetFeature(hUsbDevice, array_out, 8))
  2447. {
  2448. CloseHandle(hUsbDevice);
  2449. return -93;
  2450. }
  2451. CloseHandle(hUsbDevice);
  2452. OutDate = "";
  2453. for (n = 0; n < 8; n++)
  2454. {
  2455. OutDate = OutDate + array_out[n].ToString("X2");
  2456. }
  2457. return 0;
  2458. }
  2459. //返回锁的出厂编码
  2460. public int GetProduceDate(ref string PDate, string Path)
  2461. {
  2462. int ret;
  2463. IntPtr hsignal;
  2464. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2465. WaitForSingleObject(hsignal, INFINITE);
  2466. ret = NT_GetProduceDate(ref PDate, Path);
  2467. ReleaseSemaphore(hsignal, 1, 0);
  2468. CloseHandle(hsignal);
  2469. return ret;
  2470. }
  2471. public string SnToProduceDate(string InSn)
  2472. {
  2473. string OutString;
  2474. OutString = (2000 + HexToInt(InSn.Substring(0, 2))).ToString() + "年";
  2475. OutString = OutString + (HexToInt(InSn.Substring(2, 2))).ToString() + "月";
  2476. OutString = OutString + (HexToInt(InSn.Substring(4, 2))).ToString() + "日";
  2477. OutString = OutString + (HexToInt(InSn.Substring(6, 2))).ToString() + "时";
  2478. OutString = OutString + (HexToInt(InSn.Substring(8, 2))).ToString() + "分";
  2479. OutString = OutString + (HexToInt(InSn.Substring(10, 2))).ToString() + "秒--";
  2480. OutString = OutString + "序号:" + HexToInt(InSn.Substring(12, 4)).ToString();
  2481. return OutString;
  2482. }
  2483. private int y_setcal(byte[] indata, int address, int nlen, byte[] password, string Path)
  2484. {
  2485. int n;
  2486. byte[] array_in = new byte[25];
  2487. byte[] array_out = new byte[25];
  2488. IntPtr hUsbDevice = IntPtr.Zero;
  2489. if (nlen > 8) return -87;
  2490. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  2491. array_in[1] = 6;
  2492. array_in[2] = 0;
  2493. array_in[3] = 0;
  2494. array_in[4] = (byte) nlen;
  2495. for (n = 0; n <= 7; n++)
  2496. {
  2497. array_in[5 + n] = password[n];
  2498. }
  2499. for (n = 0; n < nlen; n++)
  2500. {
  2501. array_in[13 + n] = indata[n];
  2502. }
  2503. if (!SetFeature(hUsbDevice, array_in, 13 + nlen))
  2504. {
  2505. CloseHandle(hUsbDevice);
  2506. return -93;
  2507. }
  2508. if (!GetFeature(hUsbDevice, array_out, 2))
  2509. {
  2510. CloseHandle(hUsbDevice);
  2511. return -93;
  2512. }
  2513. CloseHandle(hUsbDevice);
  2514. if (array_out[0] != 0)
  2515. {
  2516. return -82;
  2517. }
  2518. return 0;
  2519. }
  2520. public int SetCal(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string Path)
  2521. {
  2522. int ret;
  2523. IntPtr hsignal;
  2524. byte[] ary1 = new byte[8];
  2525. byte[] ary2 = new byte[8];
  2526. short address;
  2527. myconvert(W_HKey, W_LKey, ary1);
  2528. myconvert(new_HKey, new_LKey, ary2);
  2529. address = 0;
  2530. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  2531. WaitForSingleObject(hsignal, INFINITE);
  2532. ret = y_setcal(ary2, address, 8, ary1, Path);
  2533. ReleaseSemaphore(hsignal, 1, 0);
  2534. CloseHandle(hsignal);
  2535. return ret;
  2536. }
  2537. public int CheckKeyByFindort_2()
  2538. {
  2539. //使用普通算法一查找指定的加密锁
  2540. string DevicePath = ""; //用于储存加密锁所在的路径
  2541. return FindPort_2(0, 1, -517431188, ref DevicePath);
  2542. }
  2543. //使用带长度的方法从指定的地址读取字符串
  2544. private int ReadStringEx(int addr, ref string outstring, string DevicePath)
  2545. {
  2546. int nlen, ret;
  2547. byte[] buf = new byte[1];
  2548. //先从地址0读到以前写入的字符串的长度
  2549. ret = YReadEx(buf, (short) addr, (short) 1, "54618348", "4776A17F", DevicePath);
  2550. if (ret != 0) return ret;
  2551. nlen = buf[0];
  2552. //再读取相应长度的字符串
  2553. return YReadString(ref outstring, addr + 1, nlen, "54618348", "4776A17F", DevicePath);
  2554. }
  2555. //使用从储存器读取相应数据的方式检查是否存在指定的加密锁
  2556. public int CheckKeyByReadEprom()
  2557. {
  2558. int n, ret;
  2559. string DevicePath = ""; //用于储存加密锁所在的路径
  2560. string outstring = "";
  2561. //@NoUseCode_data return 1;//如果没有使用这个功能,直接返回1
  2562. for (n = 0; n < 255; n++)
  2563. {
  2564. ret = FindPort(n, ref DevicePath);
  2565. if (ret != 0) return ret;
  2566. ret = ReadStringEx(0, ref outstring, DevicePath);
  2567. if (ret == 0 && (string.Compare(outstring, "TEAM123", StringComparison.Ordinal) == 0)) return 0;
  2568. }
  2569. return -92;
  2570. }
  2571. //使用增强算法一检查加密锁,这个方法可以有效地防止仿真
  2572. public int CheckKeyByEncstring()
  2573. {
  2574. //推荐加密方案:生成随机数,让锁做加密运算,同时在程序中端使用代码做同样的加密运算,然后进行比较判断。
  2575. int n, ret;
  2576. string DevicePath = ""; //用于储存加密锁所在的路径
  2577. string InString;
  2578. //@NoUseKeyEx return 1;//如果没有使用这个功能,直接返回1
  2579. System.Random rnd = new System.Random();
  2580. InString = rnd.Next(0, 32767).ToString("X") + rnd.Next(0, 32767).ToString("X");
  2581. for (n = 0; n < 255; n++)
  2582. {
  2583. ret = FindPort(n, ref DevicePath);
  2584. if (ret != 0) return ret;
  2585. if (Sub_CheckKeyByEncstring(InString, DevicePath) == 0) return 0;
  2586. }
  2587. return -92;
  2588. }
  2589. private int Sub_CheckKeyByEncstring(string InString, string DevicePath)
  2590. {
  2591. //使用增强算法一对字符串进行加密
  2592. int ret;
  2593. string outstring = "";
  2594. string outstring_2;
  2595. ret = EncString(InString, ref outstring, DevicePath);
  2596. if (ret != 0) return ret;
  2597. outstring_2 = StrEnc(InString, "21B6D3E062EB66C48B800EEB08D0F37F");
  2598. if (outstring_2.CompareTo(outstring) == 0) //比较结果是否相符
  2599. {
  2600. ret = 0;
  2601. }
  2602. else
  2603. {
  2604. ret = -92;
  2605. }
  2606. return ret;
  2607. }
  2608. //使用增强算法二检查是否存在对应的加密锁
  2609. public int CheckKeyByEncstring_New()
  2610. {
  2611. int n, ret;
  2612. string DevicePath = ""; //用于储存加密锁所在的路径
  2613. string outstring = "";
  2614. System.Random rnd = new System.Random();
  2615. int myrnd = rnd.Next(0, 500);
  2616. string[] EncInString =
  2617. {
  2618. "14325", "15004", "22868", "16672", "5595", "27472", "2047", "4810", "10216", "16402", "2305", "12781",
  2619. "1029", "7972", "12691", "1758", "14651", "872", "6417", "13196", "10678", "28254", "17139", "5516",
  2620. "1216", "31275", "26837", "26638", "24768", "2960",
  2621. "25712", "1510", "30019", "9198", "14750", "19331", "29939", "21319", "16474", "6393", "1945", "19006",
  2622. "28348", "29689", "25630", "3094", "17151", "18903", "2703", "16558", "19838", "20867", "7699", "9866",
  2623. "23218", "3472", "24308", "9420", "15669", "28206",
  2624. "17567", "32052", "14273", "4915", "14235", "7720", "1840", "11789", "27539", "5287", "3197", "977",
  2625. "22866", "7763", "5824", "3887", "22721", "23406", "19764", "17016", "23274", "8412", "14644", "27041",
  2626. "17399", "27165", "28728", "11323", "25906", "27475",
  2627. "24028", "12152", "5893", "17218", "11738", "22568", "29434", "19786", "22026", "11456", "11708",
  2628. "4974", "16487", "22352", "4372", "24654", "5388", "18315", "5612", "28975", "9265", "29890", "7757",
  2629. "18418", "28721", "16360", "20323", "7448", "22407", "30094",
  2630. "2936", "29123", "24268", "28648", "3060", "16823", "6843", "22726", "7010", "21844", "14892", "2971",
  2631. "28524", "13213", "12314", "6008", "8925", "13363", "32159", "24847", "20382", "12633", "7158", "25779",
  2632. "18616", "23024", "1656", "6231", "29783", "9083",
  2633. "25758", "28042", "17861", "23579", "21508", "29568", "22378", "23700", "21091", "6412", "9425",
  2634. "23110", "18325", "3741", "25764", "22835", "12181", "18503", "687", "15871", "25723", "8124", "14808",
  2635. "32745", "2179", "5710", "20012", "31446", "4102", "14061",
  2636. "26323", "24902", "27517", "6190", "2074", "20060", "20868", "32321", "11624", "30681", "10452", "7417",
  2637. "841", "33", "4680", "31175", "16741", "26674", "4824", "28094", "9352", "8627", "23967", "18320",
  2638. "2203", "16611", "15832", "3471", "8871", "20132",
  2639. "8411", "24492", "23256", "14969", "16420", "31097", "18039", "27779", "27468", "21545", "16878",
  2640. "30598", "29197", "26766", "10952", "27362", "11841", "4378", "14765", "17562", "11747", "3039",
  2641. "31848", "847", "32010", "7309", "28644", "22595", "4085", "20088",
  2642. "14890", "31066", "19705", "22162", "1125", "4885", "13971", "5485", "29916", "23188", "13660", "14650",
  2643. "18685", "16283", "526", "32716", "3919", "16012", "12163", "6293", "382", "21097", "21685", "9894",
  2644. "32338", "1747", "22206", "25265", "9143", "10765",
  2645. "21209", "23818", "2230", "7928", "10335", "24481", "6009", "20247", "8906", "28680", "1866", "15500",
  2646. "17332", "29042", "30355", "13336", "11479", "26289", "9439", "7966", "10765", "21559", "19670",
  2647. "30111", "23376", "16917", "7748", "20543", "32442", "24062",
  2648. "22545", "6718", "23174", "15683", "586", "25470", "18189", "16087", "18453", "21122", "22728", "3404",
  2649. "29119", "13932", "10836", "16595", "32433", "14589", "18023", "8511", "24437", "12440", "12767",
  2650. "2050", "20406", "4671", "5555", "25133", "20541", "916",
  2651. "2303", "21106", "16627", "3287", "31482", "29477", "6885", "27372", "15618", "14390", "19001", "342",
  2652. "11407", "28983", "10301", "27700", "32059", "27484", "5464", "12875", "8896", "13173", "13077", "5883",
  2653. "23288", "30708", "15903", "3142", "22493", "2797",
  2654. "2111", "17615", "2442", "1040", "4702", "26072", "2104", "687", "18056", "22850", "7745", "4029",
  2655. "20966", "16272", "29214", "8139", "7874", "485", "20587", "13581", "28294", "1024", "5601", "8531",
  2656. "5053", "30276", "14682", "26639", "19471", "18357",
  2657. "30160", "16468", "14919", "14323", "5214", "14036", "81", "2369", "22076", "28640", "31027", "21120",
  2658. "32454", "2412", "4890", "28979", "32116", "7431", "18767", "7036", "14307", "25061", "15932", "4637",
  2659. "29641", "28376", "30059", "29302", "24836", "7250",
  2660. "11655", "10380", "17291", "1636", "23129", "256", "15738", "11968", "16074", "23314", "14601", "16058",
  2661. "9958", "31398", "29331", "26945", "16854", "31294", "8852", "27882", "7141", "23804", "18339", "13798",
  2662. "14459", "16546", "4219", "8198", "19491", "12177",
  2663. "26102", "22716", "5768", "4811", "5390", "24859", "23666", "17395", "6366", "12245", "461", "16203",
  2664. "1183", "29499", "17314", "30132", "3498", "29504", "4635", "16050", "13691", "4982", "2690", "27236",
  2665. "5035", "29966", "4417", "29582", "16371", "14555",
  2666. "3043", "20882", "23599", "24910", "15120", "27250", "15961", "21627", "13459", "9424", "14985",
  2667. "32273", "32087", "6636", "3323", "4453", "17253", "26538", "25607", "27494"
  2668. };
  2669. string[] EncOutString =
  2670. {
  2671. "81523BE49203CDDC", "3CB641EA3FF0577E", "2CEBDEA42762FBE1", "F22C6159C1723636", "B8CB9B743D317040",
  2672. "91EBB14366AE02AD", "D34478DF40793EDE", "B57EF295ABB39189", "4D46D7F69BA5D960", "082C74E4F107AF87",
  2673. "DADCF2B97B7AAF25", "7EC98C12C6C24046", "0B7B0A7DF447C3F5", "B3ED50FFA07C6B2E", "0A1CC2400645CA94",
  2674. "A5DC58E618279445", "034C114F0BBC6F78", "D144F87C87FB3FBB", "28E51DFB73C1AA20", "85DA1BD923805040",
  2675. "54F6E08B4B4F20B9", "4071869F278BDA07", "42978249F571F331", "A4B8CFBE3ED86BE3", "F55B0E35451A518D",
  2676. "669A63A14F49C085", "576D002A50BAE78E", "C73406AD9A409E74", "EF1A53B0B23B335A", "34954C54FCB0D03E",
  2677. "85CB7C78CE31F385", "1AD2298DF6C7E67E", "A0A1A5AC679806F8", "9FBF059AA66C2AD7", "E2B6AE62C8C97C56",
  2678. "C260427BC8B642FF", "B34AA659B16287BF", "32F2B71F11C0FEEE", "2227D78F36634562", "172FB9000B2EF10C",
  2679. "5EB0A7ED6BB29432", "6848371CF208EAEE", "4A6FAF220A7A410A", "7C16575BDC3EC20B", "D2946E1D06E2805B",
  2680. "FEE8F83A157AC090", "A3D9C078725181E1", "35C59D4BB42B0532", "D721FC412ED8E16A", "3A959C6CB8C1066A",
  2681. "ECBD5AD3AA734B45", "C343663C4DE1DFE6", "14605989AEAD3C71", "02F1A14972276BA4", "EE8A4F6457399922",
  2682. "408E2646AFBC49F4", "AEC653D3A83D4E82", "575AA99EE0F2CB0B", "C76AB4442781647F", "8FC7D9CE1781EF9D",
  2683. "8FA389063F09452D", "8E8E3B22A4B90D2D", "88061AC1565B782F", "11BF8F9CB819C846", "FD9DE94A18FCD675",
  2684. "876F2E426A0A7342", "D412E08087726A88", "4513F1ACCC5F9705", "AF6C239605E452B5", "41A0B58F92B5E3DB",
  2685. "CFA153542F50C1ED", "8D8E9427FED908FA", "383C8D0CCA05B4A6", "9B71186709B3D6ED", "8B1A2F96D73510CB",
  2686. "F43F462EE7A6A9FF", "9A8A1FB491FD78EE", "CA299EF4FEC8D485", "71943747049CF25E", "505BBB4940D7CB58",
  2687. "63A1BCF252674C4E", "C7511DAFD982D451", "0E49A5117C26070D", "A3E21D54AC60F232", "62449CDB9E65643B",
  2688. "40CB7C0445C1ED09", "36C7CF3615BC5512", "194616AEBECFCDB7", "E6B13C9B2B439CFE", "D75B396ABB65FD32",
  2689. "AFA77639C986097D", "2D1A0DBBDBF7BC5B", "91BBFA9FFC0100FA", "9E80B94D7C9487BD", "E2078F27CC5457D2",
  2690. "1C5321A0920C6D30", "C9136A83E0701149", "7908C991EB75B308", "3B7445996DA2BA56", "933DCDB471DFDFC0",
  2691. "EFC9DE4CC1EDA2AA", "FBB24DD1D5529290", "DA14A0E9F12F1A59", "5DD9F7E3F5E55F72", "D8486DEBBE7EF308",
  2692. "CF604E95E7CFF6AB", "56461C4E59EF83AE", "B8A10DCF7AFA759C", "0798FA796380EF5B", "548A51A1345BB41E",
  2693. "AA54664216843AED", "9D4EE9D0B73F7D41", "ADA379FDC851756D", "7093D58C7A7A16BD", "A22083C0A08AFAA6",
  2694. "F42299BE9E745D80", "36AB3963476B70AA", "C58141E6B5E21CF2", "5C21B78B23377A52", "36BCB65B7DBE1EB0",
  2695. "C463F294CBFD68E7", "651E29F879CAB5A9", "AA8E5E88340BBDE5", "759CE8DF881B903A", "E2300C547456BB19",
  2696. "9C5E13634DE58147", "A105D097C4BEC526", "549D1CC247C82DDB", "1B3E95E395213AEB", "A70AEBFD30CA3202",
  2697. "E6077D73F94670E0", "BBE048ED46B67C5C", "35E8BFD51078B385", "03502D98908C8CEE", "8949A102527CCD28",
  2698. "6042D0DEC2E4EB82", "6CC7980794D5ECA7", "95F5F421F6B1868E", "6F19E9DF75509BA8", "D398EF72E4001004",
  2699. "9421E892DCD4DC36", "763967F59F7EB711", "3483F5EB4B0714D4", "81B85F5573585C35", "B3E42D14238DBC12",
  2700. "AC7343EBEB4716AB", "D66C4A925870193B", "81370808197FAF85", "6C401DFF90A1E174", "947FE8F9B9D0FBD4",
  2701. "0B7FB3385FE6238C", "04C393E30AED5793", "FC84FB08E54335C8", "75A59C5800BF2F66", "378451E5E7FE1EFB",
  2702. "08CF1ECE5B1D2988", "7E81F790FB62116B", "E5A5E6CEC239E2B0", "6F72AC38B48E9BE5", "B265D058D6B5B192",
  2703. "537C2C4693979235", "BAA8A19281814678", "653C94B5847DEDFF", "E931A2ABF3D5FF7C", "5A9BADA9AE3D9F98",
  2704. "22D2B5B23519D581", "777EC40EBC7BFF56", "E8745945283C71E0", "1EEDE0D74FAB92A8", "D4A79CF838427DF9",
  2705. "556433D0F69AAD55", "FBE7E0FE026A1D15", "855F8000CDADF904", "36EE8EE935FAAAC1", "17DFAAA3B6560020",
  2706. "1503DE845ABFECF9", "BB7EE225E1827FEE", "993CD4DBCED9BB0C", "0DAA74F5F5E6C53F", "B6743DA1C6278772",
  2707. "75FB8979B98C0B65", "E3C3CC647E52EF2C", "3DABD4FDC8DB5CA1", "199A8F46DA4CF7E0", "04B18765000678B2",
  2708. "11BF5E172D66F748", "C0F2D99C7B5F8A9E", "C2482B0B9D275F84", "E934FFB6AC45C2A7", "DAAE613BEF6EC022",
  2709. "C19B9C388DA7878C", "ECAD65BF3D59C1DF", "93BF7A19CF2B4EFA", "42A900C673406E6F", "3750D3666BD81174",
  2710. "E5180B2F3B2D0C9B", "F7D83B2CAB92E8FD", "CF16927992131D05", "120DD671A29F5F1D", "FD5A8F3306AA10C5",
  2711. "62D2EA4FF45C15E3", "1ADA3FB680DA495C", "CE7BD6090CB3DE75", "F724AC0F0BD0BCFD", "C2F1449556DDDB12",
  2712. "43C3B7E17AE6B311", "5F6576733B5B5F3D", "1DF28CAE0D1BEEDD", "C5C55009F3E1E55F", "93F1CA6296C33DE1",
  2713. "91D6B5F1BE8EC5FC", "B5C6DFBD98C0158A", "B5584604C2C97223", "12B20C5CF67EE003", "0E01D8E4884EAC03",
  2714. "624D1016458E7228", "24D4813DFE12629A", "4546A408D8C22951", "80A9339C195561CB", "AD364AE98F16D8B3",
  2715. "3BE9F7F954FA729A", "3328C6A173277E21", "AAC8663BD47EDCA3", "A5EA76DCEF97423B", "EF11BC18E72809EE",
  2716. "6172889BB20C9269", "0DDC9BCDD25D8DF3", "BFE17CC8E9E62DB5", "98D8880316C3ECC6", "F2979DC363CECCC3",
  2717. "C9EE9BC534946FB3", "AA3FBD1BC77F7A0F", "5A1C5A3A717208D4", "770E6704C0C53197", "0B0DFBF4516603D2",
  2718. "9FC95C6256DB13D9", "58597AEE41BF56AE", "075AFB63468B3CEF", "F16978E8D1F94EA9", "517BCC68365507EF",
  2719. "9BAA18EF01322E1E", "84A6A24E82A3D8C8", "30AC991A8CC469EB", "09069EC5BFB12087", "EAC41D54B3D3EC89",
  2720. "1890E753A07DF165", "A66E16C21C8274DC", "9D4A3C237EC0B505", "4261F5918251F958", "54AC82F1F1AA475F",
  2721. "CD24D3092D4B2C42", "D2AA408273C5ABD3", "EED1A93584899E51", "4D80BBC0F2E882E6", "73E6B8DEEA6ACEBE",
  2722. "1FCAA9CFFE27E98C", "0A937994088B09B0", "9A772D4014F90109", "39AC44EB20195032", "AE370A95A4965E16",
  2723. "336862ECE271C4A1", "0F93D090DAB342FB", "1D82AD6FF449B0A2", "5CE6CECE7A0416B0", "89879B0BCEF327BB",
  2724. "2FE76976889698E3", "75D7389D0C309C41", "DC0336890E1352A7", "F980DE1E3BDB632C", "C3AE6F36629B639F",
  2725. "37F10A87CB8605DF", "F66D90B1774A5FAD", "605751521989C0BF", "EDA72FF8C1401994", "0910BF65DD64E2C3",
  2726. "41A507FB81165144", "88056785AA082EE3", "3B69AF7076B368FF", "F9DB9773EABCF394", "ABE9143444CFCB4E",
  2727. "834A5F8B20E1082F", "B4075A3F9761653F", "89F39A29CC487D45", "66A48EB6D8B43A34", "F7D41E5F276CACB1",
  2728. "90028E59071C1674", "B63E2C406E1F72F4", "FB2915F817C94EB3", "D96ED4102BC5254D", "7C744CFB7580B3AA",
  2729. "C3AE6F36629B639F", "80BCA18A4442A447", "6680052DEF50B720", "485D7BA34F53FC4D", "2C4AD1D35EE7C4C3",
  2730. "8157DC1EDA128271", "0019CC663A28C57C", "20B34A42316BE69C", "6489B2964CD074E8", "03922631C35FDAC0",
  2731. "03CCDD2196BFF31C", "F91345DC6D48079C", "6D8D588F1727A04B", "7C4506F74DDD6D5A", "38FBC9A9EDA47752",
  2732. "AC5EBEDEA2879D4E", "8F3152319ACD6CFC", "5E07009796B3F1D3", "42E7A16A43454FF2", "A126E29F49BE22FD",
  2733. "B3B09EFF420C32C2", "B19464A630414BAE", "E871B1A478009612", "4F74739DDDC5F494", "A81EC3F160B03C67",
  2734. "A657F28088CEDE79", "30C925FC13D57EE1", "06B90DC484A6AF3C", "5D876D6E347CA177", "961252177EED989C",
  2735. "4944CBD101F680BA", "0B8B8C4FC7E1B149", "71A6308FB43B00BB", "88056E9E5FE61A74", "F6BE5693DBCC69D1",
  2736. "FD2B6E9BED16F951", "DAC2EE09D17ECED4", "B6DE3CDFB0137582", "8092CA85C27343BA", "6EBE40C41E517E8F",
  2737. "624B66116FBED5FA", "A714D3A76859C779", "BD8B8D82C839FE80", "FF691F3DB8D09887", "F4B0613D0898D12D",
  2738. "3AA3C459F35D4CE4", "99B14BB7801F2E58", "43CCD3A5D9FF4A46", "6BCFBE87E050E8B4", "83BF725BD4C3E4D3",
  2739. "D421F68FDE727B81", "CF2411BE18C68F82", "AF674A858EC10D8E", "27E7DC60D5D983F2", "5E41B1C9D6ADC64C",
  2740. "D90B17A4AFD48E40", "E9F186EAF1F10B0D", "51E6C9C9E3F89561", "E0F8FD6364AFC4ED", "0B38833206E14A64",
  2741. "DDB4651CC4CA598C", "74D003DD7CB19B41", "339A157CCB801C1E", "B46ACBA4F76A8DD7", "61B72F57AF0C7911",
  2742. "C6B06725F45E304F", "2E091E74B2C6D54E", "C85A98CD6DFC92C3", "0B7470FBCD0B5CA8", "C4465B2500AF3954",
  2743. "7380D05E1BBDD294", "B7E16B2B3C09E4A1", "8CABC3DD7FF68F83", "57205F99E167C1DD", "598F245229B51FE7",
  2744. "9561FC7ABD756E91", "D772453FFB5E748D", "1EEDE0D74FAB92A8", "07D2C1C1209A7659", "F864DA7FEDFB556C",
  2745. "3D3E11E7C560AAEF", "1900C5FC60B5523B", "29CE902C699D49C8", "FDC66F8D24B9E436", "81819D7A5219A808",
  2746. "1A8D50273B2B98B7", "10512231DAF9CEC7", "6234B44558537B48", "3B7B815D72989E62", "3FD715B420CC9E76",
  2747. "BF6A3AF58D01C71E", "6D8AD54BFC388C1C", "C9D37C9411FD13CE", "39E0BF675E6E4E2C", "212A11B63AA287DA",
  2748. "42CF60C613ADB485", "850895D1DAB78C09", "260F10ADEB254F37", "E75DC0ED75D4D898", "C07251AF19B91319",
  2749. "B49A484CF5600B18", "116C587B04A07CCA", "0BCA8C36AA105779", "F4245674E7DA95D8", "921EE34B1CD8BF7E",
  2750. "C230DEBF480ACB5F", "B3BE00C8224258D4", "D1BBE471ACB13B61", "140C67E566F56F45", "55F187171BE507CC",
  2751. "02F06DB5F74F2959", "D3B4CC00BE953C29", "2E8F814967FF0CE9", "762C26FFDAA34B5D", "3502FB15FB52C57B",
  2752. "056DAFA5E05DCF5A", "B9737E6124ACCD8D", "9A8501FC3E3E2CF9", "8D9EBF2B1A9BCB84", "D869A9D6914A6B7A",
  2753. "7895ED9C52872ED5", "0151A0F1DB298114", "F4E6FD2DE3011785", "6EF9308D4EFCFEE2", "113706A076CBC65F",
  2754. "74DBA2899E54CE4B", "0146180294169F47", "8F047C3EAB91AB06", "4207B4C92AF7C0F2", "C1C01D6E2ED962F6",
  2755. "4930F7E4788BC118", "C3995549A933D069", "CCA9DD8336D9F871", "F5C0DCF135F329EA", "46A5042266F8B5BB",
  2756. "96052C9A937640E6", "44FD8EB770E13A5B", "978277F4FFEC4AFB", "9DC933AFE95710F4", "0B627B13DB8D7D98",
  2757. "250346033B4049A4", "128B05472E455458", "40EE138760A9B7E3", "0744D0C7165C8497", "6C13D761ADD9C1DE",
  2758. "15522950F078A703", "FF4C6F7D3FC84265", "985F04EE678F2D45", "81F170E31AA377C9", "C916204B0BE099D7",
  2759. "8F2FC51B0CAEF611", "2FB07D21C52B1020", "E3E1A2F227CC20DD", "00C5EAFA0305E874", "E61764B98FBACEBB",
  2760. "15D4B14CB43F22A2", "D737A3ECA6EAB2C0", "901B6294BF8FF883", "1BFD7C7A5BF6E554", "531FC4360B4F492B",
  2761. "A8AC6FF7C7EF5B22", "24E7670F56BF5C99", "8E4E9DF917646EAC", "3246F226416FF1D3", "C92CA77D518DD39C",
  2762. "BD5273D303E9B5A9", "A93E963FCC1207C8", "DAEF059AF82D5149", "13C0BD26C4EC09CC", "A18BCC64C189FE49",
  2763. "65EC6600B328ABBC", "4A71841B76A4A95B", "DC9532F6D551BA99", "BB2A097FC166A1DC", "BAE03296F65F9471",
  2764. "3B61B691FCAEAE9F", "AFF505046E63E6D3", "211D1BAA18BCF7B3", "097DDBE93EA61C17", "05E1176E694DC1C0",
  2765. "F6799A243C8EA050", "42D8F4BA026B0344", "1DBC44F85A188E99", "32F671A2E8BABE21", "77C6D82DC131DE35",
  2766. "56A6A3C51BC4960A", "5317665483C82CAB", "3923EBEAD41C10B3", "CC69577EB87DCF53", "749DD598BA5B9532",
  2767. "A759F194DEBC4814", "82C0982B62D9BCE8", "564993B58B3CF419", "97B6182A0843590D", "F6977B98F0E783CD",
  2768. "486E21D382A8806F", "4188A1754C842233", "671013F6E2D489E1", "146B6C03F13E172E", "C3DC60541C9D34F4",
  2769. "426026A17087AB51", "848FCEFEC72F2664", "4794CD385896D78D", "0EE5F1AC44874EA3", "2DC3B113C0487E80",
  2770. "183F2EE9C4291855", "6F3A7CF759BE98B3", "C7FBDEDCA6B69121", "E3864F275A73BC64", "8873D70CFFCAF40C"
  2771. };
  2772. //@NoUseNewKeyEx return 1;//如果没有使用这个功能,直接返回1
  2773. //@NoSupNewKeyEx return 2;//如果该锁不支持这个功能,直接返回2
  2774. for (n = 0; n < 255; n++)
  2775. {
  2776. ret = FindPort(n, ref DevicePath);
  2777. if (ret != 0) return ret;
  2778. ret = EncString_New(EncInString[myrnd], ref outstring, DevicePath);
  2779. if ((ret == 0) && (outstring.CompareTo(EncOutString[myrnd]) == 0)) return 0;
  2780. }
  2781. return -92;
  2782. }
  2783. }
  2784. }