ScHaspEncrypt.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. unit ScHaspEncrypt;
  2. interface
  3. uses
  4. Windows, SysUtils, Hasp4, Forms, Classes;
  5. function SimpleHaspCheck(var APortNum: Integer): Boolean;
  6. // 返回值大于0,成功;小于0,不成功
  7. function LoadMemory: Integer;
  8. function CheckHaspTimes(AIsOnOpen: Boolean = False): Integer;
  9. function CheckHaspHours: Integer;
  10. function CheckHaspEdition: Integer;
  11. function CheckHaspAdditionEdition: Integer;
  12. function CheckHaspData: Integer;
  13. procedure LoadHaspReportOprs;
  14. procedure LoadHaspReportFuncs;
  15. procedure EncrpytFile(Source, Target: string);
  16. function OpenHaspDog: Integer;
  17. procedure SaveHaspDog;
  18. function HaspUserAuthorize(AAuthorize: array of Byte): Boolean;
  19. function HaspUserUpdateLock(AUpdateData: array of Byte): Boolean;
  20. implementation
  21. uses
  22. Math, CommonMessages, ConstMethodUnit, ScHaspPWD, CryptUtils, ScConfig,
  23. ScEncryptEditions, ScEncryptUnit;
  24. var
  25. MemHaspID: Integer = 0;
  26. DogBuf: array [0..HASP_MemorySizeInByte - 1] of Byte;
  27. function SimpleHaspCheck(var APortNum: Integer): Boolean;
  28. var
  29. P1, P2, P3, P4: Integer;
  30. begin
  31. Randomize;
  32. Result := False;
  33. // ZeroMemory(@DogBuf[0], HASP_MemorySizeInByte * SizeOf(Byte));
  34. P1 := 0;
  35. P2 := 0;
  36. P3 := 0;
  37. P4 := 0;
  38. Hasp(LOCALHASP_ISHASP, 0, 0, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  39. if P1 = 0 then
  40. Exit;
  41. P1 := 0;
  42. P2 := 0;
  43. P3 := 0;
  44. P4 := 0;
  45. Hasp(LOCALHASP_HASPSTATUS, 0, 0, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  46. if P3 = 0 then
  47. Exit;
  48. APortNum := P3;
  49. Result := True;
  50. end;
  51. function LoadMemory: Integer;
  52. var
  53. P1, P2, P3, P4: Integer;
  54. I, PortNum: Integer;
  55. begin
  56. Randomize;
  57. Result := 0 - RandomRange(1, 1234);
  58. ZeroMemory(@DogBuf[0], HASP_MemorySizeInByte * SizeOf(Byte));
  59. if not SimpleHaspCheck(PortNum) then
  60. Exit;
  61. P1 := 0;
  62. P2 := HASP_MemorySizeInByte div 2;
  63. P3 := 0;
  64. P4 := LongInt(@DogBuf[0]);
  65. Hasp(MEMOHASP_READBLOCK, 0, 0, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  66. if P3 <> 0 then
  67. Exit
  68. else
  69. Result := - Result;
  70. P1 := 0;
  71. P2 := 0;
  72. P3 := 0;
  73. P4 := 0;
  74. Hasp(MEMOHASP_HASPID, 0, 0, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  75. if (P3 = 0) then
  76. begin
  77. MemHaspID:= p2;
  78. MemHaspID:= MemHaspID shl 16;
  79. MemHaspID:= MemHaspID or p1;
  80. end;
  81. strHaspID := IntToHex(MemHaspID, 8);
  82. end;
  83. function CheckHaspTimes(AIsOnOpen: Boolean): Integer;
  84. var
  85. iCheckData: Integer;
  86. P1, P2, P3, P4: Integer;
  87. iSeedCode, PortNum: Integer;
  88. iTimes, iRunTimes: Integer;
  89. bFromMemory: Boolean;
  90. begin
  91. Result := CS_NoDog;
  92. if not SimpleHaspCheck(PortNum) then
  93. Exit;
  94. Randomize;
  95. iCheckData := RandomRange(1023, 1044);
  96. case iCheckData of
  97. 1023..1033:
  98. begin
  99. if LoadMemory < 0 then
  100. Exit;
  101. bFromMemory := True;
  102. end
  103. else
  104. bFromMemory := False;
  105. end;
  106. if bFromMemory then
  107. begin
  108. iTimes := DogBuf[11] shl 8 + DogBuf[10];
  109. end
  110. else
  111. begin
  112. // 允许次数
  113. P1 := 5;
  114. P2 := 0;
  115. P3 := 0;
  116. P4 := 0;
  117. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  118. if P3 <> 0 then
  119. Exit;
  120. iTimes := P2;
  121. end;
  122. if iTimes = $FFFF then
  123. begin
  124. Result := CS_Success;
  125. Exit;
  126. end;
  127. bAuthorized := False;
  128. if bFromMemory then
  129. begin
  130. iRunTimes := DogBuf[13] shl 8 + DogBuf[12];
  131. end
  132. else
  133. begin
  134. // 实际次数
  135. P1 := 6;
  136. P2 := 0;
  137. P3 := 0;
  138. P4 := 0;
  139. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  140. if P3 <> 0 then
  141. Exit;
  142. iRunTimes := P2;
  143. end;
  144. Activations := iRunTimes;
  145. if AIsOnOpen then
  146. begin
  147. if iRunTimes >= iTimes then
  148. Result := CS_NeedAuthorize
  149. else
  150. Result := CS_WantAuthorize;
  151. end
  152. else
  153. begin
  154. if iRunTimes > iTimes then
  155. Result := CS_NeedAuthorize
  156. else
  157. Result := CS_WantAuthorize;
  158. end;
  159. end;
  160. function CheckHaspHours: Integer;
  161. var
  162. iCheckData: Integer;
  163. P1, P2, P3, P4: Integer;
  164. iSeedCode, PortNum: Integer;
  165. iHours, iRunHours: Integer;
  166. bFromMemory: Boolean;
  167. begin
  168. Result := CS_Error;
  169. if not SimpleHaspCheck(PortNum) then
  170. Exit;
  171. Randomize;
  172. iCheckData := RandomRange(5000, 5244);
  173. case iCheckData of
  174. 5103..5220:
  175. begin
  176. if LoadMemory < 0 then
  177. Exit;
  178. bFromMemory := True;
  179. end
  180. else
  181. bFromMemory := False;
  182. end;
  183. // 允许小时
  184. if bFromMemory then
  185. begin
  186. iHours := (DogBuf[03] shl 8 + DogBuf[02]) shl 16;
  187. end
  188. else
  189. begin
  190. P1 := 1;
  191. P2 := 0;
  192. P3 := 0;
  193. P4 := 0;
  194. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  195. if P3 <> 0 then
  196. Exit;
  197. iHours := P2 shl 16;
  198. end;
  199. if bFromMemory then
  200. begin
  201. iHours := iHours + DogBuf[05] shl 8 + DogBuf[04];
  202. end
  203. else
  204. begin
  205. P1 := 2;
  206. P2 := 0;
  207. P3 := 0;
  208. P4 := 0;
  209. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  210. if P3 <> 0 then
  211. Exit;
  212. iHours := iHours + P2;
  213. end;
  214. if iHours = -1 then
  215. begin
  216. Result := CS_Success;
  217. Exit;
  218. end;
  219. bAuthorized := False;
  220. // 实际小时
  221. if bFromMemory then
  222. begin
  223. iRunHours := (DogBuf[07] shl 8 + DogBuf[06]) shl 16;
  224. end
  225. else
  226. begin
  227. P1 := 3;
  228. P2 := 0;
  229. P3 := 0;
  230. P4 := 0;
  231. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  232. if P3 <> 0 then
  233. Exit;
  234. iRunHours := P2 shl 16;
  235. end;
  236. if bFromMemory then
  237. begin
  238. iRunHours := iRunHours + DogBuf[9] shl 8 + DogBuf[8];
  239. end
  240. else
  241. begin
  242. P1 := 4;
  243. P2 := 0;
  244. P3 := 0;
  245. P4 := 0;
  246. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  247. if P3 <> 0 then
  248. Exit;
  249. iRunHours := iRunHours + P2;
  250. end;
  251. LastTime := iRunHours;
  252. if iRunHours > iHours then
  253. Result := CS_NeedAuthorize
  254. else
  255. Result := CS_WantAuthorize;
  256. end;
  257. function CheckHaspAdditionEdition: Integer;
  258. var
  259. P1, P2, P3, P4: Integer;
  260. iSeedCode, PortNum: Integer;
  261. iEdition: Integer;
  262. arrData: array [0..3] of Byte;
  263. begin
  264. Result := CS_NoDog;
  265. if not SimpleHaspCheck(PortNum) then
  266. Exit;
  267. ZeroMemory(@arrData[0], Length(arrData));
  268. // 附加版本
  269. P1 := $16;
  270. P2 := Length(arrData) div 2;
  271. P3 := 0;
  272. P4 := LongInt(@arrData[0]);
  273. Hasp(MEMOHASP_READBLOCK, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  274. if P3 <> 0 then
  275. Exit;
  276. {$IFDEF _ScBills}
  277. if arrData[0] = $08 then
  278. {$ENDIF}
  279. {$IFDEF _ScBudget}
  280. {$IFDEF _ScEstimate}
  281. if arrData[2] = $24 then
  282. {$ELSE}
  283. if arrData[1] = $16 then
  284. {$ENDIF}
  285. {$ENDIF}
  286. Result := CS_Success
  287. else
  288. Result := CS_NeedUpdate;
  289. end;
  290. function CheckHaspEdition: Integer;
  291. var
  292. iCheckData: Integer;
  293. P1, P2, P3, P4: Integer;
  294. iSeedCode, PortNum: Integer;
  295. iEdition: Integer;
  296. begin
  297. Result := CS_NoDog;
  298. if not SimpleHaspCheck(PortNum) then
  299. Exit;
  300. Randomize;
  301. iCheckData := RandomRange(256, 357);
  302. case iCheckData of
  303. 310..314, 260..271:
  304. begin
  305. Result := CS_Ignore;
  306. Exit;
  307. end;
  308. end;
  309. // 版本
  310. P1 := 0;
  311. P2 := 0;
  312. P3 := 0;
  313. P4 := 0;
  314. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  315. if P3 <> 0 then
  316. Exit;
  317. iEdition := P2;
  318. DogEdition := iEdition;
  319. if CheckEncryptEdition(iEdition) then
  320. (* if iEdition in {$IFDEF _ScPersonal}
  321. [164]
  322. {$ENDIF}
  323. {$IFDEF _ScBillsForArch}
  324. [178]
  325. {$ELSE}
  326. {$IFDEF _ScBills}
  327. {$IFDEF _ScGanSu}
  328. [81]
  329. {$ELSE}
  330. [161, 162]
  331. {$ENDIF}
  332. {$ENDIF}
  333. {$IFDEF _ScBudget}
  334. {$IFDEF _ScGanSu}
  335. [81, 82]
  336. {$ELSE}
  337. [161, 163]
  338. {$ENDIF}
  339. {$ENDIF}
  340. {$ENDIF} then*)
  341. Result := CS_Success
  342. else
  343. Result := CS_DogTypeError;
  344. end;
  345. function CheckHaspData: Integer;
  346. var
  347. iCheckData, PortNum, iOutputLength: Integer;
  348. arrInput1: array [0..11] of Byte;
  349. arrInput2: array [0..11] of Byte;
  350. arrOutput1: array [0..3] of Byte;
  351. arrOutput2: array [0..3] of Byte;
  352. strHaspIDHex, strTemp: string;
  353. P1, P2, P3, P4: Integer;
  354. I: Integer;
  355. begin
  356. Result := CS_NoDog;
  357. strHaspIDHex := '';
  358. if not SimpleHaspCheck(PortNum) then
  359. Exit;
  360. { Randomize;
  361. iCheckData := RandomRange(11000, 11050);
  362. case iCheckData of
  363. 11011..11040:
  364. begin
  365. if LoadMemory < 0 then
  366. Exit;
  367. end;
  368. end;}
  369. if LoadMemory < 0 then
  370. Exit;
  371. // 加密校验码2
  372. CopyMemory(@arrInput1[0], @DogBuf[$10], Length(arrInput2) * SizeOf(Byte));
  373. Decrypt_BlowFish(EncryptKey, arrInput1, Length(arrInput1), arrOutput1, iOutputLength);
  374. for I := 0 to 3 do
  375. strHaspIDHex := strHaspIDHex + IntToHex(arrOutput1[I], 1);
  376. // 加密校验码2
  377. CopyMemory(@arrInput2[0], @DogBuf[$1C], Length(arrInput2) * SizeOf(Byte));
  378. Decrypt_BlowFish(EncryptKey, arrInput2, Length(arrInput2), arrOutput2, iOutputLength);
  379. for I := 0 to 3 do
  380. strHaspIDHex := strHaspIDHex + IntToHex(arrOutput2[I], 1);
  381. if MemHaspID = strToInt('$' + strHaspIDHex) then
  382. Result := CS_Success
  383. else
  384. Result := CS_VerifyError;
  385. end;
  386. procedure LoadHaspReportOprs;
  387. var
  388. P1, P2, P3, P4, iLength: Integer;
  389. fsFile: TFileStream;
  390. Buf: array [0..65535] of Byte;
  391. msFile: TMemoryStream;
  392. strOprs: string;
  393. PortNum: Integer;
  394. begin
  395. if not SimpleHaspCheck(PortNum) then
  396. Exit;
  397. { fsFile := TFileStream.Create(ConfigInfo.DataFilePath + 'HerFio.dat', fmOpenRead);
  398. try
  399. iLength := fsFile.Size;
  400. ZeroMemory(@Buf[0], 65535);
  401. fsFile.Read(Buf, iLength);
  402. finally
  403. fsFile.Free;
  404. end;
  405. P1 := 0;
  406. P2 := iLength;
  407. P3 := 0;
  408. P4 := LongInt(@Buf[0]);
  409. Hasp(LOCALHASP_DECODEDATA, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  410. SetString(strOprs, PChar(@Buf[0]), iLength);
  411. GetReportOprList.Delimiter := ',';
  412. GetReportOprList.DelimitedText := strOprs; }
  413. { msFile := TMemoryStream.Create;
  414. try
  415. msFile.Write(Buf, iLength);
  416. GetReportOprList.LoadFromStream(msFile);
  417. finally
  418. msFile.Free;
  419. end;}
  420. end;
  421. procedure LoadHaspReportFuncs;
  422. var
  423. P1, P2, P3, P4, iLength: Integer;
  424. fsFile: TFileStream;
  425. Buf: array [0..65535] of Byte;
  426. msFile: TMemoryStream;
  427. strOprs: string;
  428. PortNum: Integer;
  429. begin
  430. if not SimpleHaspCheck(PortNum) then
  431. Exit;
  432. { fsFile := TFileStream.Create(ConfigInfo.DataFilePath + 'HerFif.dat', fmOpenRead);
  433. try
  434. iLength := fsFile.Size;
  435. ZeroMemory(@Buf[0], 65535);
  436. fsFile.Read(Buf, iLength);
  437. finally
  438. fsFile.Free;
  439. end;
  440. P1 := 0;
  441. P2 := iLength;
  442. P3 := 0;
  443. P4 := LongInt(@Buf[0]);
  444. Hasp(LOCALHASP_DECODEDATA, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  445. SetString(strOprs, PChar(@Buf[0]), iLength);
  446. GetReportFuncList.Delimiter := ',';
  447. GetReportFuncList.DelimitedText := strOprs; }
  448. { msFile := TMemoryStream.Create;
  449. try
  450. msFile.Write(Buf, iLength);
  451. GetReportFuncList.LoadFromStream(msFile);
  452. finally
  453. msFile.Free;
  454. end;}
  455. end;
  456. procedure EncrpytFile(Source, Target: string);
  457. var
  458. P1, P2, P3, P4, iLength: Integer;
  459. fsFile: TFileStream;
  460. Buf: array [0..65535] of Byte;
  461. msFile: TFileStream;
  462. begin
  463. fsFile := TFileStream.Create(Source, fmOpenRead);
  464. try
  465. iLength := fsFile.Size;
  466. ZeroMemory(@Buf[0], 65535);
  467. fsFile.Read(Buf, iLength);
  468. finally
  469. fsFile.Free;
  470. end;
  471. P1 := 0;
  472. P2 := iLength;
  473. P3 := 0;
  474. P4 := LongInt(@Buf[0]);
  475. Hasp(LOCALHASP_ENCODEDATA, 0, 0, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  476. msFile := TFileStream.Create(Target, fmCreate);
  477. try
  478. msFile.Write(Buf, iLength);
  479. //GetReportOprList.LoadFromStream(msFile);
  480. finally
  481. msFile.Free;
  482. end;
  483. end;
  484. function OpenHaspDog: Integer;
  485. var
  486. P1, P2, P3, P4: Integer;
  487. PortNum: Integer;
  488. begin
  489. Result := CS_Success;
  490. P1:= 6; { The address to which the data word will be written }
  491. P2:= Activations + 1; { The value to be written }
  492. P3:= 0; { unused }
  493. P4:= 0; { unused }
  494. Hasp(MEMOHASP_WRITEMEMO, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  495. end;
  496. procedure SaveHaspDog;
  497. var
  498. iSeedCode, P1, P2, P3, P4, PortNum: Integer;
  499. iLastTime: LongWord;
  500. iRunHours: LongWord;
  501. begin
  502. { RunTime := GetTickCount div 1000;
  503. if RunTime < StartTime then
  504. begin
  505. iLastTime := High(LongWord) div 1000 + RunTime - StartTime;
  506. end
  507. else
  508. iLastTime := RunTime - StartTime;}
  509. RunTime := GetTickCount;
  510. if (RunTime - StartTime < 0) and (LastTime < 720000000) then
  511. begin
  512. iLastTime := High(LongWord) - StartTime + RunTime;
  513. end
  514. else
  515. iLastTime := RunTime - StartTime;
  516. // 先读出运行总时间
  517. P1 := 3;
  518. P2 := 0;
  519. P3 := 0;
  520. P4 := 0;
  521. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  522. if P3 <> 0 then
  523. iRunHours := P2 shl 16;
  524. P1 := 4;
  525. P2 := 0;
  526. P3 := 0;
  527. P4 := 0;
  528. Hasp(MEMOHASP_READMEMO, iSeedCode, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  529. if P3 <> 0 then
  530. iRunHours := iRunHours + P2;
  531. iLastTime := iLastTime + iRunHours;
  532. // 再加上本次运行时间
  533. P1 := 3; { The address to which the data word will be written }
  534. P2 := iLastTime shr 16; { The value to be written }
  535. P3 := 0; { unused }
  536. P4 := 0; { unused }
  537. Hasp(MEMOHASP_WRITEMEMO, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  538. P1 := 4; { The address to which the data word will be written }
  539. P2 := iLastTime and $0000FFFF; { The value to be written }
  540. P3 := 0; { unused }
  541. P4 := 0; { unused }
  542. Hasp(MEMOHASP_WRITEMEMO, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  543. end;
  544. function HaspUserAuthorize(AAuthorize: array of Byte): Boolean;
  545. var
  546. iOutputLength: Integer;
  547. arrInput1: array [0..11] of Byte;
  548. arrOutput1: array [0..3] of Byte;
  549. arrInput2: array [0..11] of Byte;
  550. arrOutput2: array [0..3] of Byte;
  551. I, PortNum: Integer;
  552. P1, P2, P3, P4: Integer;
  553. arrEncryptKey: array [0..Length_AuthorizeFile - 1] of Byte;
  554. arrKey: array [0..Length_AuthorizeData - 1] of Byte;
  555. bRightKey: Boolean;
  556. strHaspIDHex: string[8];
  557. pFile: PAuthorizeFile;
  558. begin
  559. Result := False;
  560. CopyMemory(@arrEncryptKey[0], @AAuthorize[0], Length_AuthorizeFile * SizeOf(Byte));
  561. Decrypt_BlowFish(AuthorizeKey, arrEncryptKey, Length(arrEncryptKey), arrKey, iOutputLength);
  562. New(pFile);
  563. try
  564. CopyMemory(pFile, @arrKey[0], Length_AuthorizeData * SizeOf(Byte));
  565. if not SameText(pFile^.Head, HASP_AuthorizeFileHead) then
  566. Exit;
  567. CopyMemory(@arrInput1[0], @(pFile^.Key1[0]), Length(arrInput1) * SizeOf(Byte));
  568. CopyMemory(@arrInput2[0], @(pFile^.Key2[0]), Length(arrInput2) * SizeOf(Byte));
  569. Decrypt_BlowFish(AuthorizeKey, arrInput1, Length(arrInput1), arrOutput1, iOutputLength);
  570. strHaspIDHex := '';
  571. for I := 0 to 3 do
  572. strHaspIDHex := strHaspIDHex + IntToHex(arrOutput1[I], 1);
  573. Decrypt_BlowFish(AuthorizeKey, arrInput2, Length(arrInput2), arrOutput2, iOutputLength);
  574. for I := 0 to 3 do
  575. strHaspIDHex := strHaspIDHex + IntToHex(arrOutput2[I], 1);
  576. bRightKey := MemHaspID = strToInt('$' + strHaspIDHex);
  577. if bRightKey then
  578. begin
  579. SimpleHaspCheck(PortNum);
  580. // 版本
  581. P1:= 0; { The address to which the data word will be written }
  582. P2:= pFile^.Edition; { The value to be written }
  583. P3:= 0; { unused }
  584. P4:= 0; { unused }
  585. Hasp(MEMOHASP_WRITEMEMO, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  586. if P3 <> 0 then
  587. begin
  588. bAuthorized := False;
  589. Exit;
  590. end;
  591. // 允许时间
  592. P1:= 1; { The address to which the data word will be written }
  593. P2:= pFile^.Hours shr 16; { The value to be written }
  594. P3:= 0; { unused }
  595. P4:= 0; { unused }
  596. Hasp(MEMOHASP_WRITEMEMO, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  597. if P3 <> 0 then
  598. begin
  599. bAuthorized := False;
  600. Exit;
  601. end;
  602. P1:= 2; { The address to which the data word will be written }
  603. P2:= pFile^.Hours and $0000FFFF; { The value to be written }
  604. P3:= 0; { unused }
  605. P4:= 0; { unused }
  606. Hasp(MEMOHASP_WRITEMEMO, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  607. if P3 <> 0 then
  608. begin
  609. bAuthorized := False;
  610. Exit;
  611. end;
  612. // 允许次数
  613. P1:= 5; { The address to which the data word will be written }
  614. P2:= pFile^.Times; { The value to be written }
  615. P3:= 0; { unused }
  616. P4:= 0; { unused }
  617. Hasp(MEMOHASP_WRITEMEMO, 0, PortNum, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  618. if P3 <> 0 then
  619. begin
  620. bAuthorized := False;
  621. Exit;
  622. end;
  623. bAuthorized := True;
  624. Result := True;
  625. end;
  626. finally
  627. Dispose(pFile);
  628. end;
  629. end;
  630. function HaspUserUpdateLock(AUpdateData: array of Byte): Boolean;
  631. var
  632. arrSN: array [0..7] of Byte;
  633. aEncryptKey: array [0..19] of Byte;
  634. aKey: array [0..15] of Byte;
  635. strS4SNDC, strS4SN: string[8];
  636. iFileSize, iDataSize, iSNSize: Integer;
  637. aData: array [0..15] of Byte;
  638. aUpdate: array [0..3] of Byte;
  639. I: Integer;
  640. iMemHaspID: Integer;
  641. P1, P2, P3, P4: Integer;
  642. iSeedCode, iPortNum: Integer;
  643. begin
  644. Result := False;
  645. iDataSize := 16;
  646. iSNSize := 8;
  647. iFileSize := iDataSize + 8;
  648. if iFileSize mod 8 <> 0 then
  649. iFileSize := 8 * (iFileSize div 8 + 1);
  650. ZeroMemory(@aEncryptKey[0], Length(aEncryptKey));
  651. ZeroMemory(@aKey[0], Length(aKey));
  652. ZeroMemory(@aData[0], Length(aData));
  653. ZeroMemory(@aUpdate[0], Length(aUpdate));
  654. CopyMemory(@aEncryptKey[0], @AUpdateData[0], iFileSize);
  655. Decrypt_BlowFish(AuthorizeKey, aEncryptKey, iFileSize, aKey, iFileSize);
  656. CopyMemory(@aData[0], @aKey[0], Length(aData));
  657. CopyMemory(@arrSN[0], @aData[0], iSNSize);
  658. aUpdate[0] := aData[9];
  659. aUpdate[1] := aData[12];
  660. aUpdate[2] := aData[14];
  661. strS4SNDC := '';
  662. for I := Low(arrSN) to High(arrSN) do
  663. strS4SNDC := strS4SNDC + IntToHex(arrSN[I], 1);
  664. // 读出序列号
  665. P1 := 0;
  666. P2 := 0;
  667. P3 := 0;
  668. P4 := 0;
  669. Hasp(MEMOHASP_HASPID, 0, 0, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  670. if (P3 = 0) then
  671. begin
  672. iMemHaspID:= p2;
  673. iMemHaspID:= iMemHaspID shl 16;
  674. iMemHaspID:= iMemHaspID or p1;
  675. end
  676. else
  677. Exit;
  678. strS4SN := IntToHex(iMemHaspID, 8);
  679. // 如果验证序列号相同,设置允许升级
  680. if SameText(strS4SN, strS4SNDC) then
  681. begin
  682. // 版本
  683. P1 := $16;
  684. P2 := Length(aUpdate) div 2;
  685. P3 := 0;
  686. P4 := LongInt(@aUpdate[0]);
  687. Hasp(MEMOHASP_WRITEBLOCK, 0, 0, GetHaspPass1, GetHaspPass2, P1, P2, P3, P4);
  688. if P3 <> 0 then
  689. Exit;
  690. end
  691. else
  692. Exit;
  693. Result := True;
  694. end;
  695. end.