Casun_SoftKey.cs 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. //公共函数说明
  6. //***查找加密锁
  7. //int FindPort(int start, ref string OutKeyPath);
  8. //查找指定的加密锁(使用普通算法一)
  9. //int FindPort_2(int start, int in_data, int verf_data, ref string OutKeyPath);
  10. //查找指定的加密锁(使用普通算法二)
  11. //int FindPort_3(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_New(int in_data , ref int out_data , string KeyPath);
  32. //int sWrite_2Ex_New(int in_data , ref int out_data ,string KeyPath);
  33. //int sWriteEx(int in_data , ref int out_data , string KeyPath);
  34. //int sWrite_2Ex(int in_data , ref int out_data ,string KeyPath);
  35. //int sRead(ref int in_data, string KeyPath);
  36. //int sWrite(int out_data, string KeyPath);
  37. //int sWrite_2(int out_data, string KeyPath);
  38. //***设置写密码
  39. //int SetWritePassword(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string KeyPath);
  40. //***设置读密码
  41. //int SetReadPassword(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string KeyPath);
  42. //'设置增强算法密钥一
  43. //int SetCal_2(string Key , string KeyPath);
  44. //使用增强算法一对字符串进行加密
  45. //int EncString(string InString , ref string outstring , string KeyPath);
  46. //使用增强算法一对二进制数据进行加密
  47. // int Cal(byte[] InBuf, byte[] OutBuf, string KeyPath);
  48. //'设置增强算法密钥二
  49. //int SetCal_New(string Key , string KeyPath);
  50. //使用增强算法二对字符串进行加密
  51. //int EncString_New(string InString , ref string outstring , string KeyPath);
  52. //使用增强算法二对二进制数据进行加密
  53. // int Cal_New(byte[] InBuf, byte[] OutBuf, string KeyPath);
  54. //***初始化加密锁函数
  55. //int ReSet( string Path);
  56. //***获取字符串长度
  57. //int lstrlenA(string InString );
  58. namespace Casun_SoftKey
  59. {
  60. public struct GUID
  61. {
  62. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  63. public byte[] Data1;
  64. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
  65. public byte[] Data2;
  66. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
  67. public byte[] Data3;
  68. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  69. public byte[] Data4;
  70. }
  71. public struct SP_INTERFACE_DEVICE_DATA
  72. {
  73. public int cbSize;
  74. public GUID InterfaceClassGuid;
  75. public int Flags;
  76. public int Reserved;
  77. }
  78. public struct SP_INTERFACE_DEVICE_DATA_64
  79. {
  80. public Int64 cbSize;
  81. public GUID InterfaceClassGuid;
  82. public int Flags;
  83. public int Reserved;
  84. }
  85. public struct SP_DEVINFO_DATA
  86. {
  87. public int cbSize;
  88. public GUID ClassGuid;
  89. public int DevInst;
  90. public int Reserved;
  91. }
  92. public struct SP_DEVICE_INTERFACE_DETAIL_DATA
  93. {
  94. public int cbSize;
  95. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
  96. public byte[] DevicePath;
  97. }
  98. public struct HIDD_ATTRIBUTES
  99. {
  100. public int Size;
  101. public ushort VendorID;
  102. public ushort ProductID;
  103. public ushort VersionNumber;
  104. }
  105. public struct HIDP_CAPS
  106. {
  107. public short Usage;
  108. public short UsagePage;
  109. public short InputReportByteLength;
  110. public short OutputReportByteLength;
  111. public short FeatureReportByteLength;
  112. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  113. public short[] Reserved;
  114. public short NumberLinkCollectionNodes;
  115. public short NumberInputButtonCaps;
  116. public short NumberInputValueCaps;
  117. public short NumberInputDataIndices;
  118. public short NumberOutputButtonCaps;
  119. public short NumberOutputValueCaps;
  120. public short NumberOutputDataIndices;
  121. public short NumberFeatureButtonCaps;
  122. public short NumberFeatureValueCaps;
  123. public short NumberFeatureDataIndices;
  124. }
  125. class SoftKey
  126. {
  127. private const ushort VID = 0x3689;
  128. private const ushort PID = 0x8762;
  129. private const short DIGCF_PRESENT = 0x2;
  130. private const short DIGCF_DEVICEINTERFACE = 0x10;
  131. private const short INVALID_HANDLE_VALUE = (-1);
  132. private const short ERROR_NO_MORE_ITEMS = 259;
  133. private const uint GENERIC_READ = 0x80000000;
  134. private const int GENERIC_WRITE = 0x40000000;
  135. private const uint FILE_SHARE_READ = 0x1;
  136. private const uint FILE_SHARE_WRITE = 0x2;
  137. private const uint OPEN_EXISTING = 3;
  138. private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
  139. private const uint INFINITE = 0xFFFF;
  140. private const short MAX_LEN = 495;
  141. [DllImport("kernel32.dll")]
  142. public static extern int lstrlenA(string InString);
  143. [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
  144. public static extern void CopyStringToByte(byte[] pDest, string pSourceg, int ByteLenr);
  145. [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
  146. public static extern void CopyByteToString(StringBuilder pDest, byte[] pSource, int ByteLenr);
  147. [DllImport("HID.dll")]
  148. private static extern bool HidD_GetAttributes(int HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
  149. [DllImport("HID.dll")]
  150. private static extern int HidD_GetHidGuid(ref GUID HidGuid);
  151. [DllImport("HID.dll")]
  152. private static extern bool HidD_GetPreparsedData(int HidDeviceObject, ref int PreparsedData);
  153. [DllImport("HID.dll")]
  154. private static extern int HidP_GetCaps(int PreparsedData, ref HIDP_CAPS Capabilities);
  155. [DllImport("HID.dll")]
  156. private static extern bool HidD_FreePreparsedData(int PreparsedData);
  157. [DllImport("HID.dll")]
  158. private static extern bool HidD_SetFeature(int HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
  159. [DllImport("HID.dll")]
  160. private static extern bool HidD_GetFeature(int HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
  161. [DllImport("SetupApi.dll")]
  162. private static extern int SetupDiGetClassDevsA(ref GUID ClassGuid, int Enumerator, int hwndParent, int Flags);
  163. [DllImport("SetupApi.dll")]
  164. private static extern bool SetupDiDestroyDeviceInfoList(int DeviceInfoSet);
  165. [DllImport("SetupApi.dll")]
  166. private static extern bool SetupDiGetDeviceInterfaceDetailA(int DeviceInfoSet, ref SP_INTERFACE_DEVICE_DATA DeviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, int DeviceInfoData);
  167. [DllImport("SetupApi.dll", EntryPoint = "SetupDiGetDeviceInterfaceDetailA")]
  168. private static extern bool SetupDiGetDeviceInterfaceDetailA_64(int DeviceInfoSet, ref SP_INTERFACE_DEVICE_DATA_64 DeviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, int DeviceInfoData);
  169. [DllImport("SetupApi.dll")]
  170. private static extern bool SetupDiEnumDeviceInterfaces(int DeviceInfoSet, int DeviceInfoData, ref GUID InterfaceClassGuid, int MemberIndex, ref SP_INTERFACE_DEVICE_DATA DeviceInterfaceData);
  171. [DllImport("SetupApi.dll", EntryPoint = "SetupDiEnumDeviceInterfaces")]
  172. private static extern bool SetupDiEnumDeviceInterfaces_64(int DeviceInfoSet, int DeviceInfoData, ref GUID InterfaceClassGuid, int MemberIndex, ref SP_INTERFACE_DEVICE_DATA_64 DeviceInterfaceData);
  173. [DllImport("kernel32.dll", EntryPoint = "CreateFileA")]
  174. private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, uint lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, uint hTemplateFile);
  175. [DllImport("kernel32.dll")]
  176. private static extern int CloseHandle(int hObject);
  177. [DllImport("kernel32.dll")]
  178. private static extern int GetLastError();
  179. [DllImport("kernel32.dll", EntryPoint = "CreateSemaphoreA")]
  180. private static extern int CreateSemaphore(int lpSemaphoreAttributes, int lInitialCount, int lMaximumCount, string lpName);
  181. [DllImport("kernel32.dll")]
  182. private static extern int WaitForSingleObject(int hHandle, uint dwMilliseconds);
  183. [DllImport("kernel32.dll")]
  184. private static extern int ReleaseSemaphore(int hSemaphore, int lReleaseCount, int lpPreviousCount);
  185. Boolean Is32;
  186. public SoftKey()
  187. {
  188. Is32 = (System.IntPtr.Size == 4);
  189. }
  190. //以下函数用于将字节数组转化为宽字符串
  191. private static string ByteConvertString(byte[] buffer)
  192. {
  193. char[] null_string = { '\0', '\0' };
  194. System.Text.Encoding encoding = System.Text.Encoding.Default;
  195. return encoding.GetString(buffer).TrimEnd(null_string);
  196. }
  197. //以下用于将16进制字符串转化为无符号长整型
  198. private uint HexToInt(string s)
  199. {
  200. string[] hexch = { "0", "1", "2", "3", "4", "5", "6", "7",
  201. "8", "9", "A", "B", "C", "D", "E", "F"};
  202. s = s.ToUpper();
  203. int i, j;
  204. int r, n, k;
  205. string ch;
  206. k = 1; r = 0;
  207. for (i = s.Length; i > 0; i--)
  208. {
  209. ch = s.Substring(i - 1, 1);
  210. n = 0;
  211. for (j = 0; j < 16; j++)
  212. if (ch == hexch[j])
  213. n = j;
  214. r += (n * k);
  215. k *= 16;
  216. }
  217. return unchecked((uint)r);
  218. }
  219. private int HexStringToByteArray(string InString, ref byte[] b)
  220. {
  221. int nlen;
  222. int retutn_len;
  223. int n, i;
  224. string temp;
  225. nlen = InString.Length;
  226. if (nlen < 16) retutn_len = 16;
  227. retutn_len = nlen / 2;
  228. b = new byte[retutn_len];
  229. i = 0;
  230. for (n = 0; n < nlen; n = n + 2)
  231. {
  232. temp = InString.Substring(n, 2);
  233. b[i] = (byte)HexToInt(temp);
  234. i = i + 1;
  235. }
  236. return retutn_len;
  237. }
  238. public void EnCode(byte[] inb, byte[] outb, string Key)
  239. {
  240. UInt32 cnDelta, y, z, a, b, c, d, temp_2;
  241. UInt32[] buf = new UInt32[16];
  242. int n, i, nlen;
  243. UInt32 sum;
  244. //UInt32 temp, temp_1;
  245. string temp_string;
  246. cnDelta = 2654435769;
  247. sum = 0;
  248. nlen = Key.Length;
  249. i = 0;
  250. for (n = 1; n <= nlen; n = n + 2)
  251. {
  252. temp_string = Key.Substring(n - 1, 2);
  253. buf[i] = HexToInt(temp_string);
  254. i = i + 1;
  255. }
  256. a = 0; b = 0; c = 0; d = 0;
  257. for (n = 0; n <= 3; n++)
  258. {
  259. a = (buf[n] << (n * 8)) | a;
  260. b = (buf[n + 4] << (n * 8)) | b;
  261. c = (buf[n + 4 + 4] << (n * 8)) | c;
  262. d = (buf[n + 4 + 4 + 4] << (n * 8)) | d;
  263. }
  264. y = 0;
  265. z = 0;
  266. for (n = 0; n <= 3; n++)
  267. {
  268. temp_2 = inb[n];
  269. y = (temp_2 << (n * 8)) | y;
  270. temp_2 = inb[n + 4];
  271. z = (temp_2 << (n * 8)) | z;
  272. }
  273. n = 32;
  274. while (n > 0)
  275. {
  276. sum = cnDelta + sum;
  277. /*temp = (z << 4) & 0xFFFFFFFF;
  278. temp = (temp + a) & 0xFFFFFFFF;
  279. temp_1 = (z + sum) & 0xFFFFFFFF;
  280. temp = (temp ^ temp_1) & 0xFFFFFFFF;
  281. temp_1 = (z >> 5) & 0xFFFFFFFF;
  282. temp_1 = (temp_1 + b) & 0xFFFFFFFF;
  283. temp = (temp ^ temp_1) & 0xFFFFFFFF;
  284. temp = (temp + y) & 0xFFFFFFFF;
  285. y = temp & 0xFFFFFFFF;*/
  286. y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
  287. /*temp = (y << 4) & 0xFFFFFFFF;
  288. temp = (temp + c) & 0xFFFFFFFF;
  289. temp_1 = (y + sum) & 0xFFFFFFFF;
  290. temp = (temp ^ temp_1) & 0xFFFFFFFF;
  291. temp_1 = (y >> 5) & 0xFFFFFFFF;
  292. temp_1 = (temp_1 + d) & 0xFFFFFFFF;
  293. temp = (temp ^ temp_1) & 0xFFFFFFFF;
  294. temp = (z + temp) & 0xFFFFFFFF;
  295. z = temp & 0xFFFFFFFF;*/
  296. z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
  297. n = n - 1;
  298. }
  299. for (n = 0; n <= 3; n++)
  300. {
  301. outb[n] = System.Convert.ToByte((y >> (n * 8)) & 255);
  302. outb[n + 4] = System.Convert.ToByte((z >> (n * 8)) & 255);
  303. }
  304. }
  305. public void DeCode(byte[] inb, byte[] outb, string Key)
  306. {
  307. UInt32 cnDelta, y, z, a, b, c, d, temp_2;
  308. UInt32[] buf = new UInt32[16];
  309. int n, i, nlen;
  310. UInt32 sum;
  311. //UInt32 temp, temp_1;
  312. string temp_string;
  313. cnDelta = 2654435769;
  314. sum = 0xC6EF3720;
  315. nlen = Key.Length;
  316. i = 0;
  317. for (n = 1; n <= nlen; n = n + 2)
  318. {
  319. temp_string = Key.Substring(n - 1, 2);
  320. buf[i] = HexToInt(temp_string);
  321. i = i + 1;
  322. }
  323. a = 0; b = 0; c = 0; d = 0;
  324. for (n = 0; n <= 3; n++)
  325. {
  326. a = (buf[n] << (n * 8)) | a;
  327. b = (buf[n + 4] << (n * 8)) | b;
  328. c = (buf[n + 4 + 4] << (n * 8)) | c;
  329. d = (buf[n + 4 + 4 + 4] << (n * 8)) | d;
  330. }
  331. y = 0;
  332. z = 0;
  333. for (n = 0; n <= 3; n++)
  334. {
  335. temp_2 = inb[n];
  336. y = (temp_2 << (n * 8)) | y;
  337. temp_2 = inb[n + 4];
  338. z = (temp_2 << (n * 8)) | z;
  339. }
  340. n = 32;
  341. while (n-- > 0)
  342. {
  343. z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
  344. y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
  345. sum -= cnDelta;
  346. }
  347. for (n = 0; n <= 3; n++)
  348. {
  349. outb[n] = System.Convert.ToByte((y >> (n * 8)) & 255);
  350. outb[n + 4] = System.Convert.ToByte((z >> (n * 8)) & 255);
  351. }
  352. }
  353. public string StrEnc(string InString, string Key)//使用增强算法,加密字符串
  354. {
  355. byte[] b, outb;
  356. byte[] temp = new byte[8], outtemp = new byte[8];
  357. int n, i, nlen, outlen;
  358. string outstring;
  359. nlen = lstrlenA(InString) + 1;
  360. if (nlen < 8)
  361. outlen = 8;
  362. else
  363. outlen = nlen;
  364. b = new byte[outlen];
  365. outb = new byte[outlen];
  366. CopyStringToByte(b, InString, nlen);
  367. b.CopyTo(outb, 0);
  368. for (n = 0; n <= outlen - 8; n = n + 8)
  369. {
  370. for (i = 0; i < 8; i++) temp[i] = b[i + n];
  371. EnCode(temp, outtemp, Key);
  372. for (i = 0; i < 8; i++) outb[i + n] = outtemp[i];
  373. }
  374. outstring = "";
  375. for (n = 0; n <= outlen - 1; n++)
  376. {
  377. outstring = outstring + outb[n].ToString("X2");
  378. }
  379. return outstring;
  380. }
  381. public string StrDec(string InString, string Key) //使用增强算法,加密字符串
  382. {
  383. byte[] b, outb;
  384. byte[] temp = new byte[8], outtemp = new byte[8];
  385. int n, i, nlen, outlen;
  386. string temp_string;
  387. StringBuilder c_str;
  388. nlen = InString.Length;
  389. if (nlen < 16) outlen = 16;
  390. outlen = nlen / 2;
  391. b = new byte[outlen];
  392. outb = new byte[outlen];
  393. i = 0;
  394. for (n = 1; n <= nlen; n = n + 2)
  395. {
  396. temp_string = InString.Substring(n - 1, 2);
  397. b[i] = System.Convert.ToByte(HexToInt(temp_string));
  398. i = i + 1;
  399. }
  400. b.CopyTo(outb, 0);
  401. for (n = 0; n <= outlen - 8; n = n + 8)
  402. {
  403. for (i = 0; i < 8; i++) temp[i] = b[i + n];
  404. DeCode(temp, outtemp, Key);
  405. for (i = 0; i < 8; i++) outb[i + n] = outtemp[i];
  406. }
  407. c_str = new StringBuilder("", outlen);
  408. CopyByteToString(c_str, outb, outlen);
  409. return c_str.ToString();
  410. }
  411. private bool isfindmydevice(int pos, ref int count, ref string OutPath)
  412. {
  413. if (Is32)
  414. return isfindmydevice_32(pos, ref count, ref OutPath);
  415. else
  416. return isfindmydevice_64(pos, ref count, ref OutPath);
  417. }
  418. private bool isfindmydevice_64(int pos, ref int count, ref string OutPath)
  419. {
  420. int hardwareDeviceInfo;
  421. SP_INTERFACE_DEVICE_DATA_64 DeviceInfoData = new SP_INTERFACE_DEVICE_DATA_64();
  422. int i;
  423. GUID HidGuid = new GUID();
  424. SP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
  425. int requiredLength;
  426. int d_handle;
  427. HIDD_ATTRIBUTES Attributes = new HIDD_ATTRIBUTES();
  428. i = 0;
  429. HidD_GetHidGuid(ref HidGuid);
  430. hardwareDeviceInfo = SetupDiGetClassDevsA(ref HidGuid, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  431. if (hardwareDeviceInfo == INVALID_HANDLE_VALUE) return false;
  432. DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData);
  433. while (SetupDiEnumDeviceInterfaces_64(hardwareDeviceInfo, 0, ref HidGuid, i, ref DeviceInfoData))
  434. {
  435. if (GetLastError() == ERROR_NO_MORE_ITEMS) break;
  436. functionClassDeviceData.cbSize = 8;
  437. requiredLength = 0;
  438. if (!SetupDiGetDeviceInterfaceDetailA_64(hardwareDeviceInfo, ref DeviceInfoData, ref functionClassDeviceData, 300, ref requiredLength, 0))
  439. {
  440. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  441. return false;
  442. }
  443. OutPath = ByteConvertString(functionClassDeviceData.DevicePath);
  444. d_handle = CreateFile(OutPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
  445. if (INVALID_HANDLE_VALUE != d_handle)
  446. {
  447. if (HidD_GetAttributes(d_handle, ref Attributes))
  448. {
  449. if ((Attributes.ProductID == PID) && (Attributes.VendorID == VID))
  450. {
  451. if (pos == count)
  452. {
  453. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  454. CloseHandle(d_handle);
  455. return true;
  456. }
  457. count = count + 1;
  458. }
  459. }
  460. CloseHandle(d_handle);
  461. }
  462. i = i + 1;
  463. }
  464. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  465. return false;
  466. }
  467. private bool isfindmydevice_32(int pos, ref int count, ref string OutPath)
  468. {
  469. int hardwareDeviceInfo;
  470. SP_INTERFACE_DEVICE_DATA DeviceInfoData = new SP_INTERFACE_DEVICE_DATA();
  471. int i;
  472. GUID HidGuid = new GUID();
  473. SP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
  474. int requiredLength;
  475. int d_handle;
  476. HIDD_ATTRIBUTES Attributes = new HIDD_ATTRIBUTES();
  477. i = 0;
  478. HidD_GetHidGuid(ref HidGuid);
  479. hardwareDeviceInfo = SetupDiGetClassDevsA(ref HidGuid, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  480. if (hardwareDeviceInfo == INVALID_HANDLE_VALUE) return false;
  481. DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData);
  482. while (SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0, ref HidGuid, i, ref DeviceInfoData))
  483. {
  484. if (GetLastError() == ERROR_NO_MORE_ITEMS) break;
  485. functionClassDeviceData.cbSize = Marshal.SizeOf(functionClassDeviceData) - 255;// 5;
  486. requiredLength = 0;
  487. if (!SetupDiGetDeviceInterfaceDetailA(hardwareDeviceInfo, ref DeviceInfoData, ref functionClassDeviceData, 300, ref requiredLength, 0))
  488. {
  489. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  490. return false;
  491. }
  492. OutPath = ByteConvertString(functionClassDeviceData.DevicePath);
  493. d_handle = CreateFile(OutPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
  494. if (INVALID_HANDLE_VALUE != d_handle)
  495. {
  496. if (HidD_GetAttributes(d_handle, ref Attributes))
  497. {
  498. if ((Attributes.ProductID == PID) && (Attributes.VendorID == VID))
  499. {
  500. if (pos == count)
  501. {
  502. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  503. CloseHandle(d_handle);
  504. return true;
  505. }
  506. count = count + 1;
  507. }
  508. }
  509. CloseHandle(d_handle);
  510. }
  511. i = i + 1;
  512. }
  513. SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
  514. return false;
  515. }
  516. private bool GetFeature(int hDevice, byte[] array_out, int out_len)
  517. {
  518. bool FeatureStatus;
  519. bool Status;
  520. int i;
  521. byte[] FeatureReportBuffer = new byte[512];
  522. int Ppd = 0;
  523. HIDP_CAPS Caps = new HIDP_CAPS();
  524. if (!HidD_GetPreparsedData(hDevice, ref Ppd)) return false;
  525. if (HidP_GetCaps(Ppd, ref Caps) <= 0)
  526. {
  527. HidD_FreePreparsedData(Ppd);
  528. return false;
  529. }
  530. Status = true;
  531. FeatureReportBuffer[0] = 1;
  532. FeatureStatus = HidD_GetFeature(hDevice, FeatureReportBuffer, Caps.FeatureReportByteLength);
  533. if (FeatureStatus)
  534. {
  535. for (i = 0; i < out_len; i++)
  536. {
  537. array_out[i] = FeatureReportBuffer[i];
  538. }
  539. }
  540. Status = Status && FeatureStatus;
  541. HidD_FreePreparsedData(Ppd);
  542. return Status;
  543. }
  544. private bool SetFeature(int hDevice, byte[] array_in, int in_len)
  545. {
  546. bool FeatureStatus;
  547. bool Status;
  548. int i;
  549. byte[] FeatureReportBuffer = new byte[512];
  550. int Ppd = 0;
  551. HIDP_CAPS Caps = new HIDP_CAPS();
  552. if (!HidD_GetPreparsedData(hDevice, ref Ppd)) return false;
  553. if (HidP_GetCaps(Ppd, ref Caps) <= 0)
  554. {
  555. HidD_FreePreparsedData(Ppd);
  556. return false;
  557. }
  558. Status = true;
  559. FeatureReportBuffer[0] = 2;
  560. for (i = 0; i < in_len; i++)
  561. {
  562. FeatureReportBuffer[i + 1] = array_in[i + 1];
  563. }
  564. FeatureStatus = HidD_SetFeature(hDevice, FeatureReportBuffer, Caps.FeatureReportByteLength);
  565. Status = Status && FeatureStatus;
  566. HidD_FreePreparsedData(Ppd);
  567. return Status;
  568. }
  569. private int NT_FindPort(int start, ref string OutPath)
  570. {
  571. int count = 0;
  572. if (!isfindmydevice(start, ref count, ref OutPath))
  573. {
  574. return -92;
  575. }
  576. return 0;
  577. }
  578. private int NT_FindPort_2(int start, int in_data, int verf_data, ref string OutPath)
  579. {
  580. int count = 0;
  581. int pos;
  582. int out_data = 0;
  583. int ret;
  584. for (pos = start; pos < 256; pos++)
  585. {
  586. if (!isfindmydevice(pos, ref count, ref OutPath)) return -92;
  587. ret = WriteDword(in_data, OutPath);
  588. if (ret != 0) return ret;
  589. ret = ReadDword(ref out_data, OutPath);
  590. if (ret != 0) return ret;
  591. if (out_data == verf_data) { return 0; }
  592. }
  593. return (-92);
  594. }
  595. private int OpenMydivece(ref int hUsbDevice, string Path)
  596. {
  597. string OutPath;
  598. bool biao;
  599. int count = 0;
  600. if (Path.Length < 1)
  601. {
  602. OutPath = "";
  603. biao = isfindmydevice(0, ref count, ref OutPath);
  604. if (!biao) return -92;
  605. hUsbDevice = CreateFile(OutPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  606. if (hUsbDevice == INVALID_HANDLE_VALUE) return -92;
  607. }
  608. else
  609. {
  610. hUsbDevice = CreateFile(Path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  611. if (hUsbDevice == INVALID_HANDLE_VALUE) return -92;
  612. }
  613. return 0;
  614. }
  615. private int NT_Read(ref byte ele1, ref byte ele2, ref byte ele3, ref byte ele4, string Path)
  616. {
  617. byte[] array_out = new byte[25];
  618. int hUsbDevice = 0;
  619. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  620. if (!GetFeature(hUsbDevice, array_out, 5)) { CloseHandle(hUsbDevice); return -93; }
  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. int hUsbDevice = 0;
  632. if (OpenMydivece(ref hUsbDevice, Path) != 0) { return -92; }
  633. array_in[1] = 3; array_in[2] = ele1; array_in[3] = ele2; array_in[4] = ele3; array_in[5] = ele4;
  634. if (!SetFeature(hUsbDevice, array_in, 5)) { CloseHandle(hUsbDevice); return -93; }
  635. CloseHandle(hUsbDevice);
  636. return 0;
  637. }
  638. private int NT_Write_2(byte ele1, byte ele2, byte ele3, byte ele4, string Path)
  639. {
  640. byte[] array_in = new byte[25];
  641. int hUsbDevice = 0;
  642. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  643. array_in[1] = 4; array_in[2] = ele1; array_in[3] = ele2; array_in[4] = ele3; array_in[5] = ele4;
  644. if (!SetFeature(hUsbDevice, array_in, 5)) { CloseHandle(hUsbDevice); return -93; }
  645. CloseHandle(hUsbDevice);
  646. return 0;
  647. }
  648. private int GetIDVersion(ref short Version, string Path)
  649. {
  650. byte[] array_in = new byte[25];
  651. byte[] array_out = new byte[25];
  652. int hUsbDevice = 0;
  653. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  654. array_in[1] = 1;
  655. if (!SetFeature(hUsbDevice, array_in, 1)) { CloseHandle(hUsbDevice); return -93; }
  656. if (!GetFeature(hUsbDevice, array_out, 1)) { CloseHandle(hUsbDevice); return -93; }
  657. CloseHandle(hUsbDevice);
  658. Version = array_out[0];
  659. return 0;
  660. }
  661. private int NT_GetID(ref int ID_1, ref int ID_2, string Path)
  662. {
  663. int[] t = new int[8];
  664. byte[] array_in = new byte[25];
  665. byte[] array_out = new byte[25];
  666. int hUsbDevice = 0;
  667. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  668. array_in[1] = 2;
  669. if (!SetFeature(hUsbDevice, array_in, 1)) { CloseHandle(hUsbDevice); return -93; }
  670. if (!GetFeature(hUsbDevice, array_out, 8)) { CloseHandle(hUsbDevice); return -93; }
  671. CloseHandle(hUsbDevice);
  672. t[0] = array_out[0]; t[1] = array_out[1]; t[2] = array_out[2]; t[3] = array_out[3];
  673. t[4] = array_out[4]; t[5] = array_out[5]; t[6] = array_out[6]; t[7] = array_out[7];
  674. ID_1 = t[3] | (t[2] << 8) | (t[1] << 16) | (t[0] << 24);
  675. ID_2 = t[7] | (t[6] << 8) | (t[5] << 16) | (t[4] << 24);
  676. return 0;
  677. }
  678. private int Y_Read(byte[] OutData, int address, int nlen, byte[] password, string Path, int pos)
  679. {
  680. int addr_l;
  681. int addr_h;
  682. int n;
  683. byte[] array_in = new byte[25];
  684. byte[] array_out = new byte[25];
  685. if ((address > MAX_LEN) || (address < 0)) return -81;
  686. if ((nlen > 16)) return -87;
  687. if ((nlen + address) > MAX_LEN) return -88;
  688. addr_h = (address >> 8) * 2;
  689. addr_l = address & 255;
  690. int hUsbDevice = 0;
  691. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  692. array_in[1] = 0x12;
  693. array_in[2] = (byte)addr_h;
  694. array_in[3] = (byte)addr_l;
  695. array_in[4] = (byte)nlen;
  696. for (n = 0; n <= 7; n++)
  697. {
  698. array_in[5 + n] = password[n];
  699. }
  700. if (!SetFeature(hUsbDevice, array_in, 13)) { CloseHandle(hUsbDevice); return -93; }
  701. if (!GetFeature(hUsbDevice, array_out, nlen + 1)) { CloseHandle(hUsbDevice); return -94; }
  702. CloseHandle(hUsbDevice);
  703. if (array_out[0] != 0)
  704. {
  705. return -83;
  706. }
  707. for (n = 0; n < nlen; n++)
  708. {
  709. OutData[n + pos] = array_out[n + 1];
  710. }
  711. return 0;
  712. }
  713. private int Y_Write(byte[] indata, int address, int nlen, byte[] password, string Path, int pos)
  714. {
  715. int addr_l;
  716. int addr_h;
  717. int n;
  718. byte[] array_in = new byte[25];
  719. byte[] array_out = new byte[25];
  720. if ((nlen > 8)) return -87;
  721. if ((address + nlen - 1) > (MAX_LEN + 17) || (address < 0)) return -81;
  722. addr_h = (address >> 8) * 2;
  723. addr_l = address & 255;
  724. int hUsbDevice = 0;
  725. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  726. array_in[1] = 0x13;
  727. array_in[2] = (byte)addr_h;
  728. array_in[3] = (byte)addr_l;
  729. array_in[4] = (byte)nlen;
  730. for (n = 0; n <= 7; n++)
  731. {
  732. array_in[5 + n] = password[n];
  733. }
  734. for (n = 0; n < nlen; n++)
  735. {
  736. array_in[13 + n] = indata[n + pos];
  737. }
  738. if (!SetFeature(hUsbDevice, array_in, 13 + nlen)) { CloseHandle(hUsbDevice); return -93; }
  739. if (!GetFeature(hUsbDevice, array_out, 2)) { CloseHandle(hUsbDevice); return -94; }
  740. CloseHandle(hUsbDevice);
  741. if (array_out[0] != 0)
  742. {
  743. return -82;
  744. }
  745. return 0;
  746. }
  747. private int NT_Cal(byte[] InBuf, byte[] outbuf, string Path, int pos)
  748. {
  749. int n;
  750. byte[] array_in = new byte[25];
  751. byte[] array_out = new byte[25];
  752. int hUsbDevice = 0;
  753. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  754. array_in[1] = 8;
  755. for (n = 2; n <= 9; n++)
  756. {
  757. array_in[n] = InBuf[n - 2 + pos];
  758. }
  759. if (!SetFeature(hUsbDevice, array_in, 9)) { CloseHandle(hUsbDevice); return -93; }
  760. if (!GetFeature(hUsbDevice, array_out, 9)) { CloseHandle(hUsbDevice); return -93; }
  761. CloseHandle(hUsbDevice);
  762. for (n = 0; n < 8; n++)
  763. {
  764. outbuf[n + pos] = array_out[n];
  765. }
  766. if (array_out[8] != 0x55)
  767. {
  768. return -20;
  769. }
  770. return 0;
  771. }
  772. private int NT_SetCal_2(byte[] indata, byte IsHi, string Path, short pos)
  773. {
  774. int n;
  775. byte[] array_in = new byte[25];
  776. byte[] array_out = new byte[25];
  777. int hUsbDevice = 0;
  778. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  779. array_in[1] = 9;
  780. array_in[2] = IsHi;
  781. for (n = 0; n < 8; n++)
  782. {
  783. array_in[3 + n] = indata[n + pos];
  784. }
  785. if (!SetFeature(hUsbDevice, array_in, 11)) { CloseHandle(hUsbDevice); return -93; }
  786. if (!GetFeature(hUsbDevice, array_out, 2)) { CloseHandle(hUsbDevice); return -94; }
  787. CloseHandle(hUsbDevice);
  788. if (array_out[0] != 0)
  789. {
  790. return -82;
  791. }
  792. return 0;
  793. }
  794. private int ReadDword(ref int out_data, string Path)
  795. {
  796. byte b1 = 0;
  797. byte b2 = 0;
  798. byte b3 = 0;
  799. byte b4 = 0;
  800. int t1;
  801. int t2;
  802. int t3;
  803. int t4;
  804. int ret;
  805. ret = NT_Read(ref b1, ref b2, ref b3, ref b4, Path);
  806. t1 = b1; t2 = b2; t3 = b3; t4 = b4;
  807. out_data = t1 | (t2 << 8) | (t3 << 16) | (t4 << 24);
  808. return ret;
  809. }
  810. private int WriteDword(int in_data, string Path)
  811. {
  812. byte b1;
  813. byte b2;
  814. byte b3;
  815. byte b4;
  816. b1 = (byte)(in_data & 255);
  817. b2 = (byte)((in_data >> 8) & 255);
  818. b3 = (byte)((in_data >> 16) & 255);
  819. b4 = (byte)((in_data >> 24) & 255);
  820. return NT_Write(b1, b2, b3, b4, Path);
  821. }
  822. private int WriteDword_2(int in_data, string Path)
  823. {
  824. byte b1;
  825. byte b2;
  826. byte b3;
  827. byte b4;
  828. b1 = (byte)(in_data & 255);
  829. b2 = (byte)((in_data >> 8) & 255);
  830. b3 = (byte)((in_data >> 16) & 255);
  831. b4 = (byte)((in_data >> 24) & 255);
  832. return NT_Write_2(b1, b2, b3, b4, Path);
  833. }
  834. public int NT_GetIDVersion(ref short Version, string Path)
  835. {
  836. int ret;
  837. int hsignal;
  838. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  839. WaitForSingleObject(hsignal, INFINITE);
  840. ret = GetIDVersion(ref Version, Path);
  841. ReleaseSemaphore(hsignal, 1, 0);
  842. CloseHandle(hsignal);
  843. return ret;
  844. }
  845. public int GetID(ref int ID_1, ref int ID_2, string Path)
  846. {
  847. int ret;
  848. int hsignal;
  849. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  850. WaitForSingleObject(hsignal, INFINITE);
  851. ret = NT_GetID(ref ID_1, ref ID_2, Path);
  852. ReleaseSemaphore(hsignal, 1, 0);
  853. CloseHandle(hsignal);
  854. return ret;
  855. }
  856. public int sRead(ref int in_data, string Path)
  857. {
  858. int ret;
  859. int hsignal;
  860. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  861. WaitForSingleObject(hsignal, INFINITE);
  862. ret = ReadDword(ref in_data, Path);
  863. ReleaseSemaphore(hsignal, 1, 0);
  864. CloseHandle(hsignal);
  865. return ret;
  866. }
  867. public int sWrite(int out_data, string Path)
  868. {
  869. int ret;
  870. int hsignal;
  871. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  872. WaitForSingleObject(hsignal, INFINITE);
  873. ret = WriteDword(out_data, Path);
  874. ReleaseSemaphore(hsignal, 1, 0);
  875. CloseHandle(hsignal);
  876. return ret;
  877. }
  878. public int YWriteEx(byte[] indata, int address, int nlen, string HKey, string LKey, string Path)
  879. {
  880. int ret = 0;
  881. int hsignal;
  882. byte[] password = new byte[8];
  883. int n;
  884. int leave;
  885. int temp_leave;
  886. if ((address + nlen - 1 > MAX_LEN) || (address < 0)) return -81;
  887. myconvert(HKey, LKey, password);
  888. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  889. WaitForSingleObject(hsignal, INFINITE);
  890. temp_leave = address % 16;
  891. leave = 16 - temp_leave;
  892. if (leave > nlen) leave = nlen;
  893. if (leave > 0)
  894. {
  895. for (n = 0; n < leave / 8; n++)
  896. {
  897. ret = Y_Write(indata, address + n * 8, 8, password, Path, 8 * n);
  898. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  899. }
  900. if (leave - 8 * n > 0)
  901. {
  902. ret = Y_Write(indata, address + n * 8, leave - n * 8, password, Path, 8 * n);
  903. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  904. }
  905. }
  906. nlen = nlen - leave; address = address + leave;
  907. if (nlen > 0)
  908. {
  909. for (n = 0; n < nlen / 8; n++)
  910. {
  911. ret = Y_Write(indata, address + n * 8, 8, password, Path, leave + 8 * n);
  912. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  913. }
  914. if (nlen - 8 * n > 0)
  915. {
  916. ret = Y_Write(indata, address + n * 8, nlen - n * 8, password, Path, leave + 8 * n);
  917. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  918. }
  919. }
  920. ReleaseSemaphore(hsignal, 1, 0);
  921. CloseHandle(hsignal);
  922. return ret;
  923. }
  924. public int YReadEx(byte[] OutData, short address, short nlen, string HKey, string LKey, string Path)
  925. {
  926. int ret = 0;
  927. int hsignal;
  928. byte[] password = new byte[8];
  929. int n;
  930. if ((address + nlen - 1 > MAX_LEN) || (address < 0)) return (-81);
  931. myconvert(HKey, LKey, password);
  932. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  933. WaitForSingleObject(hsignal, INFINITE);
  934. for (n = 0; n < nlen / 16; n++)
  935. {
  936. ret = Y_Read(OutData, address + n * 16, 16, password, Path, n * 16);
  937. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  938. }
  939. if (nlen - 16 * n > 0)
  940. {
  941. ret = Y_Read(OutData, address + n * 16, nlen - 16 * n, password, Path, 16 * n);
  942. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  943. }
  944. ReleaseSemaphore(hsignal, 1, 0);
  945. CloseHandle(hsignal);
  946. return ret;
  947. }
  948. public int FindPort_2(int start, int in_data, int verf_data, ref string OutPath)
  949. {
  950. int ret;
  951. int hsignal;
  952. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  953. WaitForSingleObject(hsignal, INFINITE);
  954. ret = NT_FindPort_2(start, in_data, verf_data, ref OutPath);
  955. ReleaseSemaphore(hsignal, 1, 0);
  956. CloseHandle(hsignal);
  957. return ret;
  958. }
  959. public int FindPort(int start, ref string OutPath)
  960. {
  961. int ret;
  962. int hsignal;
  963. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  964. WaitForSingleObject(hsignal, INFINITE);
  965. ret = NT_FindPort(start, ref OutPath);
  966. ReleaseSemaphore(hsignal, 1, 0);
  967. CloseHandle(hsignal);
  968. return ret;
  969. }
  970. public int sWrite_2(int out_data, string Path)
  971. {
  972. int ret;
  973. int hsignal;
  974. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  975. WaitForSingleObject(hsignal, INFINITE);
  976. ret = WriteDword_2(out_data, Path);
  977. ReleaseSemaphore(hsignal, 1, 0);
  978. CloseHandle(hsignal);
  979. return ret;
  980. }
  981. private string AddZero(string InKey)
  982. {
  983. int nlen;
  984. int n;
  985. nlen = InKey.Length;
  986. for (n = nlen; n <= 7; n++)
  987. {
  988. InKey = "0" + InKey;
  989. }
  990. return InKey;
  991. }
  992. private void myconvert(string HKey, string LKey, byte[] out_data)
  993. {
  994. HKey = AddZero(HKey);
  995. LKey = AddZero(LKey);
  996. int n;
  997. for (n = 0; n <= 3; n++)
  998. {
  999. out_data[n] = (byte)HexToInt(HKey.Substring(n * 2, 2));
  1000. }
  1001. for (n = 0; n <= 3; n++)
  1002. {
  1003. out_data[n + 4] = (byte)HexToInt(LKey.Substring(n * 2, 2));
  1004. }
  1005. }
  1006. public int YRead(ref byte indata, int address, string HKey, string LKey, string Path)
  1007. {
  1008. int ret;
  1009. int hsignal;
  1010. byte[] ary1 = new byte[8];
  1011. if ((address > 495) || (address < 0)) return -81;
  1012. myconvert(HKey, LKey, ary1);
  1013. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1014. WaitForSingleObject(hsignal, INFINITE);
  1015. ret = sub_YRead(ref indata, address, ary1, Path);
  1016. ReleaseSemaphore(hsignal, 1, 0);
  1017. CloseHandle(hsignal);
  1018. return ret;
  1019. }
  1020. private int sub_YRead(ref byte OutData, int address, byte[] password, string Path)
  1021. {
  1022. int n;
  1023. byte[] array_in = new byte[25];
  1024. byte[] array_out = new byte[25];
  1025. int hUsbDevice = 0;
  1026. byte opcode;
  1027. if ((address > 495) || (address < 0)) return -81;
  1028. opcode = 128;
  1029. if (address > 255)
  1030. {
  1031. opcode = 160;
  1032. address = address - 256;
  1033. }
  1034. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1035. array_in[1] = 16;
  1036. array_in[2] = opcode;
  1037. array_in[3] = (byte)address;
  1038. array_in[4] = (byte)address;
  1039. for (n = 0; n < 8; n++)
  1040. {
  1041. array_in[5 + n] = password[n];
  1042. }
  1043. if (!SetFeature(hUsbDevice, array_in, 13))
  1044. {
  1045. CloseHandle(hUsbDevice); return -93;
  1046. }
  1047. if (!GetFeature(hUsbDevice, array_out, 2))
  1048. {
  1049. CloseHandle(hUsbDevice); return -94;
  1050. }
  1051. CloseHandle(hUsbDevice);
  1052. if (array_out[0] != 83)
  1053. {
  1054. return -83;
  1055. }
  1056. OutData = array_out[1];
  1057. return 0;
  1058. }
  1059. public int YWrite(byte indata, int address, string HKey, string LKey, string Path)
  1060. {
  1061. int ret;
  1062. int hsignal;
  1063. byte[] ary1 = new byte[8];
  1064. if ((address > 495) || (address < 0)) return -81;
  1065. myconvert(HKey, LKey, ary1);
  1066. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1067. WaitForSingleObject(hsignal, INFINITE);
  1068. ret = sub_YWrite(indata, address, ary1, Path);
  1069. ReleaseSemaphore(hsignal, 1, 0);
  1070. CloseHandle(hsignal);
  1071. return ret;
  1072. }
  1073. private int sub_YWrite(byte indata, int address, byte[] password, string Path)
  1074. {
  1075. int n;
  1076. byte[] array_in = new byte[25];
  1077. byte[] array_out = new byte[25];
  1078. int hUsbDevice = 0;
  1079. byte opcode;
  1080. if ((address > 511) || (address < 0)) return -81;
  1081. opcode = 64;
  1082. if (address > 255)
  1083. {
  1084. opcode = 96;
  1085. address = address - 256;
  1086. }
  1087. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1088. array_in[1] = 17;
  1089. array_in[2] = opcode;
  1090. array_in[3] = (byte)address;
  1091. array_in[4] = indata;
  1092. for (n = 0; n < 8; n++)
  1093. {
  1094. array_in[5 + n] = password[n];
  1095. }
  1096. if (!SetFeature(hUsbDevice, array_in, 13))
  1097. {
  1098. CloseHandle(hUsbDevice); return -93;
  1099. }
  1100. if (GetFeature(hUsbDevice, array_out, 2))
  1101. {
  1102. CloseHandle(hUsbDevice); return -94;
  1103. }
  1104. CloseHandle(hUsbDevice);
  1105. if (array_out[1] != 1)
  1106. {
  1107. return -82;
  1108. }
  1109. return 0;
  1110. }
  1111. public int SetReadPassword(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string Path)
  1112. {
  1113. int ret;
  1114. int hsignal;
  1115. byte[] ary1 = new byte[8];
  1116. byte[] ary2 = new byte[8];
  1117. short address;
  1118. myconvert(W_HKey, W_LKey, ary1);
  1119. myconvert(new_HKey, new_LKey, ary2);
  1120. address = 496;
  1121. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1122. WaitForSingleObject(hsignal, INFINITE);
  1123. ret = Y_Write(ary2, address, 8, ary1, Path, 0);
  1124. ReleaseSemaphore(hsignal, 1, 0);
  1125. CloseHandle(hsignal);
  1126. return ret;
  1127. }
  1128. public int SetWritePassword(string W_HKey, string W_LKey, string new_HKey, string new_LKey, string Path)
  1129. {
  1130. int ret;
  1131. int hsignal;
  1132. byte[] ary1 = new byte[8];
  1133. byte[] ary2 = new byte[8];
  1134. short address;
  1135. myconvert(W_HKey, W_LKey, ary1);
  1136. myconvert(new_HKey, new_LKey, ary2);
  1137. address = 504;
  1138. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1139. WaitForSingleObject(hsignal, INFINITE);
  1140. ret = Y_Write(ary2, address, 8, ary1, Path, 0);
  1141. ReleaseSemaphore(hsignal, 1, 0);
  1142. CloseHandle(hsignal);
  1143. return ret;
  1144. }
  1145. public int YWriteString(string InString, int address, string HKey, string LKey, string Path)
  1146. {
  1147. int ret = 0;
  1148. byte[] ary1 = new byte[8];
  1149. int hsignal;
  1150. int n;
  1151. int outlen;
  1152. int total_len;
  1153. int temp_leave;
  1154. int leave;
  1155. byte[] b;
  1156. if ((address < 0)) return -81;
  1157. myconvert(HKey, LKey, ary1);
  1158. outlen = lstrlenA(InString); //注意,这里不写入结束字符串,与原来的兼容,也可以写入结束字符串,与原来的不兼容,写入长度会增加1
  1159. b = new byte[outlen];
  1160. CopyStringToByte(b, InString, outlen);
  1161. total_len = address + outlen;
  1162. if (total_len > MAX_LEN) return -47;
  1163. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1164. WaitForSingleObject(hsignal, INFINITE);
  1165. temp_leave = address % 16;
  1166. leave = 16 - temp_leave;
  1167. if (leave > outlen) leave = outlen;
  1168. if (leave > 0)
  1169. {
  1170. for (n = 0; n < (leave / 8); n++)
  1171. {
  1172. ret = Y_Write(b, address + n * 8, 8, ary1, Path, n * 8);
  1173. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  1174. }
  1175. if (leave - 8 * n > 0)
  1176. {
  1177. ret = Y_Write(b, address + n * 8, leave - n * 8, ary1, Path, 8 * n);
  1178. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  1179. }
  1180. }
  1181. outlen = outlen - leave;
  1182. address = address + leave;
  1183. if (outlen > 0)
  1184. {
  1185. for (n = 0; n < (outlen / 8); n++)
  1186. {
  1187. ret = Y_Write(b, address + n * 8, 8, ary1, Path, leave + n * 8);
  1188. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  1189. }
  1190. if (outlen - 8 * n > 0)
  1191. {
  1192. ret = Y_Write(b, address + n * 8, outlen - n * 8, ary1, Path, leave + 8 * n);
  1193. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  1194. }
  1195. }
  1196. ReleaseSemaphore(hsignal, 1, 0);
  1197. CloseHandle(hsignal);
  1198. return ret;
  1199. }
  1200. public int YReadString(ref string OutString, int address, int nlen, string HKey, string LKey, string Path)
  1201. {
  1202. int ret = 0;
  1203. byte[] ary1 = new byte[8];
  1204. int hsignal;
  1205. int n;
  1206. int total_len;
  1207. byte[] outb;
  1208. StringBuilder temp_OutString;
  1209. outb = new byte[nlen];
  1210. myconvert(HKey, LKey, ary1);
  1211. if (address < 0) return -81;
  1212. total_len = address + nlen;
  1213. if (total_len > MAX_LEN) return -47;
  1214. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1215. WaitForSingleObject(hsignal, INFINITE);
  1216. for (n = 0; n < (nlen / 16); n++)
  1217. {
  1218. ret = Y_Read(outb, address + n * 16, 16, ary1, Path, n * 16);
  1219. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  1220. }
  1221. if (nlen - 16 * n > 0)
  1222. {
  1223. ret = Y_Read(outb, address + n * 16, nlen - 16 * n, ary1, Path, 16 * n);
  1224. if (ret != 0) { ReleaseSemaphore(hsignal, 1, 0); CloseHandle(hsignal); return ret; }
  1225. }
  1226. ReleaseSemaphore(hsignal, 1, 0);
  1227. CloseHandle(hsignal);
  1228. temp_OutString = new StringBuilder("", nlen);
  1229. //初始化数据为0,注意,这步一定是需要的
  1230. for (n = 0; n < nlen; n++)
  1231. {
  1232. temp_OutString.Append(0);
  1233. }
  1234. CopyByteToString(temp_OutString, outb, nlen);
  1235. OutString = temp_OutString.ToString();
  1236. return ret;
  1237. }
  1238. public int SetCal_2(string Key, string Path)
  1239. {
  1240. int ret;
  1241. int hsignal;
  1242. byte[] KeyBuf = new byte[16];
  1243. byte[] inb = new byte[8];
  1244. HexStringToByteArray(Key, ref KeyBuf);
  1245. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1246. WaitForSingleObject(hsignal, INFINITE);
  1247. ret = NT_SetCal_2(KeyBuf, 0, Path, 8);
  1248. if (ret != 0) goto error1;
  1249. ret = NT_SetCal_2(KeyBuf, 1, Path, 0);
  1250. error1:
  1251. ReleaseSemaphore(hsignal, 1, 0);
  1252. CloseHandle(hsignal);
  1253. return ret;
  1254. }
  1255. public int Cal(byte[] InBuf, byte[] outbuf, string Path)
  1256. {
  1257. int ret;
  1258. int hsignal;
  1259. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1260. WaitForSingleObject(hsignal, INFINITE);
  1261. ret = NT_Cal(InBuf, outbuf, Path, 0);
  1262. ReleaseSemaphore(hsignal, 1, 0);
  1263. CloseHandle(hsignal);
  1264. return ret;
  1265. }
  1266. public int EncString(string InString, ref string OutString, string Path)
  1267. {
  1268. int hsignal;
  1269. byte[] b;
  1270. byte[] outb;
  1271. int n;
  1272. int nlen, temp_len;
  1273. int ret = 0;
  1274. nlen = lstrlenA(InString) + 1;
  1275. temp_len = nlen;
  1276. if (nlen < 8)
  1277. {
  1278. nlen = 8;
  1279. }
  1280. b = new byte[nlen];
  1281. outb = new byte[nlen];
  1282. CopyStringToByte(b, InString, temp_len);
  1283. b.CopyTo(outb, 0);
  1284. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1285. WaitForSingleObject(hsignal, INFINITE);
  1286. for (n = 0; n <= (nlen - 8); n = n + 8)
  1287. {
  1288. ret = NT_Cal(b, outb, Path, n);
  1289. if (ret != 0) break;
  1290. }
  1291. ReleaseSemaphore(hsignal, 1, 0);
  1292. CloseHandle(hsignal);
  1293. OutString = "";
  1294. for (n = 0; n < nlen; n++)
  1295. {
  1296. OutString = OutString + outb[n].ToString("X2");
  1297. }
  1298. return ret;
  1299. }
  1300. public int sWriteEx(int in_data, ref int out_data, string Path)
  1301. {
  1302. int ret;
  1303. int hsignal;
  1304. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1305. WaitForSingleObject(hsignal, INFINITE);
  1306. ret = WriteDword(in_data, Path);
  1307. if (ret != 0) goto error1;
  1308. ret = ReadDword(ref out_data, Path);
  1309. error1:
  1310. ReleaseSemaphore(hsignal, 1, 0);
  1311. CloseHandle(hsignal);
  1312. return ret;
  1313. }
  1314. public int sWrite_2Ex(int in_data, ref int out_data, string Path)
  1315. {
  1316. int ret;
  1317. int hsignal;
  1318. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1319. WaitForSingleObject(hsignal, INFINITE);
  1320. ret = WriteDword_2(in_data, Path);
  1321. if (ret != 0) goto error1;
  1322. ret = ReadDword(ref out_data, Path);
  1323. error1:
  1324. ReleaseSemaphore(hsignal, 1, 0);
  1325. CloseHandle(hsignal);
  1326. return ret;
  1327. }
  1328. public int ReSet(string Path)
  1329. {
  1330. int ret;
  1331. int hsignal;
  1332. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1333. WaitForSingleObject(hsignal, INFINITE);
  1334. ret = NT_ReSet(Path);
  1335. ReleaseSemaphore(hsignal, 1, 0);
  1336. CloseHandle(hsignal);
  1337. return ret;
  1338. }
  1339. private int NT_ReSet(string Path)
  1340. {
  1341. byte[] array_in = new byte[25];
  1342. byte[] array_out = new byte[25];
  1343. int hUsbDevice = 0;
  1344. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1345. array_in[1] = 32;
  1346. if (!SetFeature(hUsbDevice, array_in, 2))
  1347. {
  1348. CloseHandle(hUsbDevice); return -93;
  1349. }
  1350. if (!GetFeature(hUsbDevice, array_out, 2))
  1351. {
  1352. CloseHandle(hUsbDevice); return -93;
  1353. }
  1354. CloseHandle(hUsbDevice);
  1355. if (array_out[0] != 0)
  1356. {
  1357. return -82;
  1358. }
  1359. return 0;
  1360. }
  1361. public int sWriteEx_New(int in_data, ref int out_data, string Path)
  1362. {
  1363. int ret;
  1364. int hsignal;
  1365. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1366. WaitForSingleObject(hsignal, INFINITE);
  1367. ret = WriteDword_New(in_data, Path);
  1368. if (ret != 0) goto error1;
  1369. ret = ReadDword(ref out_data, Path);
  1370. error1:
  1371. ReleaseSemaphore(hsignal, 1, 0);
  1372. CloseHandle(hsignal);
  1373. return ret;
  1374. }
  1375. public int sWrite_2Ex_New(int in_data, ref int out_data, string Path)
  1376. {
  1377. int ret;
  1378. int hsignal;
  1379. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1380. WaitForSingleObject(hsignal, INFINITE);
  1381. ret = WriteDword_2_New(in_data, Path);
  1382. if (ret != 0) goto error1;
  1383. ret = ReadDword(ref out_data, Path);
  1384. error1:
  1385. ReleaseSemaphore(hsignal, 1, 0);
  1386. CloseHandle(hsignal);
  1387. return ret;
  1388. }
  1389. public int SetCal_New(string Key, string Path)
  1390. {
  1391. int ret;
  1392. int hsignal;
  1393. byte[] KeyBuf = new byte[16];
  1394. byte[] inb = new byte[8];
  1395. HexStringToByteArray(Key, ref KeyBuf);
  1396. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1397. WaitForSingleObject(hsignal, INFINITE);
  1398. ret = NT_SetCal_New(KeyBuf, 0, Path, 8);
  1399. if (ret != 0) goto error1;
  1400. ret = NT_SetCal_New(KeyBuf, 1, Path, 0);
  1401. error1:
  1402. ReleaseSemaphore(hsignal, 1, 0);
  1403. CloseHandle(hsignal);
  1404. return ret;
  1405. }
  1406. public int Cal_New(byte[] InBuf, byte[] outbuf, string Path)
  1407. {
  1408. int ret;
  1409. int hsignal;
  1410. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1411. WaitForSingleObject(hsignal, INFINITE);
  1412. ret = NT_Cal_New(InBuf, outbuf, Path, 0);
  1413. ReleaseSemaphore(hsignal, 1, 0);
  1414. CloseHandle(hsignal);
  1415. return ret;
  1416. }
  1417. public int EncString_New(string InString, ref string OutString, string Path)
  1418. {
  1419. int hsignal;
  1420. byte[] b;
  1421. byte[] outb;
  1422. int n;
  1423. int nlen, temp_len;
  1424. int ret = 0;
  1425. nlen = lstrlenA(InString) + 1;
  1426. temp_len = nlen;
  1427. if (nlen < 8)
  1428. {
  1429. nlen = 8;
  1430. }
  1431. b = new byte[nlen];
  1432. outb = new byte[nlen];
  1433. CopyStringToByte(b, InString, temp_len);
  1434. b.CopyTo(outb, 0);
  1435. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1436. WaitForSingleObject(hsignal, INFINITE);
  1437. for (n = 0; n <= (nlen - 8); n = n + 8)
  1438. {
  1439. ret = NT_Cal_New(b, outb, Path, n);
  1440. if (ret != 0) break;
  1441. }
  1442. ReleaseSemaphore(hsignal, 1, 0);
  1443. CloseHandle(hsignal);
  1444. OutString = "";
  1445. for (n = 0; n < nlen; n++)
  1446. {
  1447. OutString = OutString + outb[n].ToString("X2");
  1448. }
  1449. return ret;
  1450. }
  1451. public int FindPort_3(int start, int in_data, int verf_data, ref string OutPath)
  1452. {
  1453. int ret;
  1454. int hsignal;
  1455. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1456. WaitForSingleObject(hsignal, INFINITE);
  1457. ret = NT_FindPort_3(start, in_data, verf_data, ref OutPath);
  1458. ReleaseSemaphore(hsignal, 1, 0);
  1459. CloseHandle(hsignal);
  1460. return ret;
  1461. }
  1462. public int NT_GetVersionEx(ref short Version, string Path)
  1463. {
  1464. int ret;
  1465. int hsignal;
  1466. hsignal = CreateSemaphore(0, 1, 1, "ex_sim");
  1467. WaitForSingleObject(hsignal, INFINITE);
  1468. ret = F_GetVersionEx(ref Version, Path);
  1469. ReleaseSemaphore(hsignal, 1, 0);
  1470. CloseHandle(hsignal);
  1471. return ret;
  1472. }
  1473. private int WriteDword_New(int in_data, string Path)
  1474. {
  1475. byte b1;
  1476. byte b2;
  1477. byte b3;
  1478. byte b4;
  1479. b1 = (byte)(in_data & 255);
  1480. b2 = (byte)((in_data >> 8) & 255);
  1481. b3 = (byte)((in_data >> 16) & 255);
  1482. b4 = (byte)((in_data >> 24) & 255);
  1483. return NT_Write_New(b1, b2, b3, b4, Path);
  1484. }
  1485. private int WriteDword_2_New(int in_data, string Path)
  1486. {
  1487. byte b1;
  1488. byte b2;
  1489. byte b3;
  1490. byte b4;
  1491. b1 = (byte)(in_data & 255);
  1492. b2 = (byte)((in_data >> 8) & 255);
  1493. b3 = (byte)((in_data >> 16) & 255);
  1494. b4 = (byte)((in_data >> 24) & 255);
  1495. return NT_Write_2_New(b1, b2, b3, b4, Path);
  1496. }
  1497. private int NT_Cal_New(byte[] InBuf, byte[] outbuf, string Path, int pos)
  1498. {
  1499. int n;
  1500. byte[] array_in = new byte[25];
  1501. byte[] array_out = new byte[25];
  1502. int hUsbDevice = 0;
  1503. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1504. array_in[1] = 12;
  1505. for (n = 2; n <= 9; n++)
  1506. {
  1507. array_in[n] = InBuf[n - 2 + pos];
  1508. }
  1509. if (!SetFeature(hUsbDevice, array_in, 9)) { CloseHandle(hUsbDevice); return -93; }
  1510. if (!GetFeature(hUsbDevice, array_out, 9)) { CloseHandle(hUsbDevice); return -93; }
  1511. CloseHandle(hUsbDevice);
  1512. for (n = 0; n < 8; n++)
  1513. {
  1514. outbuf[n + pos] = array_out[n];
  1515. }
  1516. if (array_out[8] != 0x55)
  1517. {
  1518. return -20;
  1519. }
  1520. return 0;
  1521. }
  1522. private int NT_SetCal_New(byte[] indata, byte IsHi, string Path, short pos)
  1523. {
  1524. int n;
  1525. byte[] array_in = new byte[25];
  1526. byte[] array_out = new byte[25];
  1527. int hUsbDevice = 0;
  1528. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1529. array_in[1] = 13;
  1530. array_in[2] = IsHi;
  1531. for (n = 0; n < 8; n++)
  1532. {
  1533. array_in[3 + n] = indata[n + pos];
  1534. }
  1535. if (!SetFeature(hUsbDevice, array_in, 11)) { CloseHandle(hUsbDevice); return -93; }
  1536. if (!GetFeature(hUsbDevice, array_out, 2)) { CloseHandle(hUsbDevice); return -94; }
  1537. CloseHandle(hUsbDevice);
  1538. if (array_out[0] != 0)
  1539. {
  1540. return -82;
  1541. }
  1542. return 0;
  1543. }
  1544. private int F_GetVersionEx(ref short Version, string Path)
  1545. {
  1546. byte[] array_in = new byte[25];
  1547. byte[] array_out = new byte[25];
  1548. int hUsbDevice = 0;
  1549. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1550. array_in[1] = 5;
  1551. if (!SetFeature(hUsbDevice, array_in, 1)) { CloseHandle(hUsbDevice); return -93; }
  1552. if (!GetFeature(hUsbDevice, array_out, 1)) { CloseHandle(hUsbDevice); return -93; }
  1553. CloseHandle(hUsbDevice);
  1554. Version = array_out[0];
  1555. return 0;
  1556. }
  1557. private int NT_Write_New(byte ele1, byte ele2, byte ele3, byte ele4, string Path)
  1558. {
  1559. byte[] array_in = new byte[25];
  1560. int hUsbDevice = 0;
  1561. if (OpenMydivece(ref hUsbDevice, Path) != 0) { return -92; }
  1562. array_in[1] = 0x0a; array_in[2] = ele1; array_in[3] = ele2; array_in[4] = ele3; array_in[5] = ele4;
  1563. if (!SetFeature(hUsbDevice, array_in, 5)) { CloseHandle(hUsbDevice); return -93; }
  1564. CloseHandle(hUsbDevice);
  1565. return 0;
  1566. }
  1567. private int NT_Write_2_New(byte ele1, byte ele2, byte ele3, byte ele4, string Path)
  1568. {
  1569. byte[] array_in = new byte[25];
  1570. int hUsbDevice = 0;
  1571. if (OpenMydivece(ref hUsbDevice, Path) != 0) return -92;
  1572. array_in[1] = 0x0b; array_in[2] = ele1; array_in[3] = ele2; array_in[4] = ele3; array_in[5] = ele4;
  1573. if (!SetFeature(hUsbDevice, array_in, 5)) { CloseHandle(hUsbDevice); return -93; }
  1574. CloseHandle(hUsbDevice);
  1575. return 0;
  1576. }
  1577. private int NT_FindPort_3(int start, int in_data, int verf_data, ref string OutPath)
  1578. {
  1579. int count = 0;
  1580. int pos;
  1581. int out_data = 0;
  1582. int ret;
  1583. for (pos = start; pos < 256; pos++)
  1584. {
  1585. if (!isfindmydevice(pos, ref count, ref OutPath)) return -92;
  1586. ret = WriteDword_New(in_data, OutPath);
  1587. if (ret != 0) return ret;
  1588. ret = ReadDword(ref out_data, OutPath);
  1589. if (ret != 0) return ret;
  1590. if (out_data == verf_data) { return 0; }
  1591. }
  1592. return (-92);
  1593. }
  1594. public int CheckKeyByFindort_2()
  1595. {
  1596. //使用普通算法一查找指定的加密锁
  1597. string DevicePath = ""; //用于储存加密锁所在的路径
  1598. return FindPort_2(0, 1, -1785508517, ref DevicePath);
  1599. }
  1600. public int CheckKeyByFindort_3()
  1601. {
  1602. //使用普通算法二查找指定的加密锁
  1603. string DevicePath = ""; //用于储存加密锁所在的路径
  1604. //@NoUseCode_FindPort_3 return 2;//如果这个锁不支持这个功能,直接返回2
  1605. return FindPort_3(0, 1, 1613404515, ref DevicePath);
  1606. }
  1607. //使用带长度的方法从指定的地址读取字符串
  1608. private int ReadStringEx(int addr, ref string outstring, string DevicePath)
  1609. {
  1610. int nlen, ret;
  1611. byte[] buf = new byte[1];
  1612. //先从地址0读到以前写入的字符串的长度
  1613. ret = YReadEx(buf, (short)addr, (short)1, "5561F2BF", "F4563BFD", DevicePath);
  1614. if (ret != 0) return ret;
  1615. nlen = buf[0];
  1616. //再读取相应长度的字符串
  1617. return YReadString(ref outstring, addr + 1, nlen, "5561F2BF", "F4563BFD", DevicePath);
  1618. }
  1619. //使用从储存器读取相应数据的方式检查是否存在指定的加密锁
  1620. public int CheckKeyByReadEprom()
  1621. {
  1622. int n, ret;
  1623. string DevicePath = "";//用于储存加密锁所在的路径
  1624. string outstring = "";
  1625. //@NoUseCode_data return 1;//如果没有使用这个功能,直接返回1
  1626. for (n = 0; n < 255; n++)
  1627. {
  1628. ret = FindPort(n, ref DevicePath);
  1629. if (ret != 0) return ret;
  1630. ret = ReadStringEx(0, ref outstring, DevicePath);
  1631. if ((ret == 0) && (outstring.CompareTo("casun_software_v1.0") == 0)) return 0;
  1632. }
  1633. return -92;
  1634. }
  1635. //使用增强算法一检查加密锁,这个方法可以有效地防止仿真
  1636. public int CheckKeyByEncstring()
  1637. {
  1638. //推荐加密方案:生成随机数,让锁做加密运算,同时在程序中端使用代码做同样的加密运算,然后进行比较判断。
  1639. int n, ret;
  1640. string DevicePath = "";//用于储存加密锁所在的路径
  1641. string InString;
  1642. //@NoUseKeyEx return 1;//如果没有使用这个功能,直接返回1
  1643. System.Random rnd = new System.Random();
  1644. InString = rnd.Next(0, 32767).ToString("X") + rnd.Next(0, 32767).ToString("X");
  1645. for (n = 0; n < 255; n++)
  1646. {
  1647. ret = FindPort(n, ref DevicePath);
  1648. if (ret != 0) return ret;
  1649. if (Sub_CheckKeyByEncstring(InString, DevicePath) == 0) return 0;
  1650. }
  1651. return -92;
  1652. }
  1653. private int Sub_CheckKeyByEncstring(string InString, string DevicePath)
  1654. {
  1655. //使用增强算法一对字符串进行加密
  1656. int ret;
  1657. string outstring = "";
  1658. string outstring_2;
  1659. ret = EncString(InString, ref outstring, DevicePath);
  1660. if (ret != 0) return ret;
  1661. outstring_2 = StrEnc(InString, "38EAD53CD7DE1E7AAAB3570449AB8052");
  1662. if (outstring_2.CompareTo(outstring) == 0)//比较结果是否相符
  1663. {
  1664. ret = 0;
  1665. }
  1666. else
  1667. {
  1668. ret = -92;
  1669. }
  1670. return ret;
  1671. }
  1672. //使用增强算法二检查是否存在对应的加密锁
  1673. public int CheckKeyByEncstring_New()
  1674. {
  1675. int n, ret;
  1676. string DevicePath = "";//用于储存加密锁所在的路径
  1677. string outstring = "";
  1678. //@NoUseNewKeyEx return 1;//如果没有使用这个功能,直接返回1
  1679. //@NoSupNewKeyEx return 2;//如果该锁不支持这个功能,直接返回2
  1680. for (n = 0; n < 255; n++)
  1681. {
  1682. ret = FindPort(n, ref DevicePath);
  1683. if (ret != 0) return ret;
  1684. ret = EncString_New("123456", ref outstring, DevicePath);
  1685. if ((ret == 0) && (outstring.CompareTo("25FCF14ACE9FCF26") == 0)) return 0;
  1686. }
  1687. return -92;
  1688. }
  1689. }
  1690. }