DbSNSEncrypt.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. unit DbSNSEncrypt;
  2. interface
  3. uses
  4. Windows, SysUtils, Forms, Classes;
  5. function SimpleSNSCheck: Integer;
  6. function CheckSNSTimes(AIsOnOpen: Boolean = False): Integer;
  7. function CheckSNSHours: Integer;
  8. function CheckSNSAdditionEdition: Integer;
  9. function CheckSNSEdition: Integer;
  10. function CheckSNSData: Integer;
  11. function CheckSNSLimitDate: Integer;
  12. function GetSNSReportOprNum(AOpr: string): Integer;
  13. function GetSNSReportFuncNum(AOpr: string): Integer;
  14. function OpenSNSDog: Integer;
  15. procedure SaveSNSDog;
  16. function SNSUserAuthorize(AAuthorize: array of Byte): Boolean;
  17. function SNSUserUpdateLock(AUpdateData: array of Byte): Boolean;
  18. implementation
  19. uses
  20. Math, CommonMessages, ScUtils, DbEncryptPWD, S4CryptUtils, ScConfig,
  21. DbEncryptUnit, DbS4Utils, DbEncryptTypes, DbEncryptEditions, DbConnectEncrypt,
  22. ScPHPWeb;
  23. var
  24. DogAddr: integer;
  25. DogBytes: integer;
  26. DogData: ^byte;
  27. IsNet: Boolean;
  28. RefCount: Integer;
  29. const
  30. SDSerialNo: array [0..9] of Byte = ($00, $00, $00, $06, $FB, $FB, $F7, $FB, $FD, $FF);
  31. function WriteDog: LongInt; external;
  32. function ReadDog: LongInt; external;
  33. {$L rgdlw32d.obj}
  34. // 检查软件狗厂家序列号
  35. function SimpleSNSCheck: Integer;
  36. var
  37. dwRetCode: longint;
  38. dwSerialNumber: Longint;
  39. aSDSN: array [0..9] of Byte;
  40. strSN: string[6];
  41. aStrSN: array [0..5] of Byte;
  42. I: Integer;
  43. begin
  44. Result := -1;
  45. DogBytes := 0; {if you want to Read SerialNo DogBytes must be 0}
  46. DogAddr := 0;
  47. DogData := @dwSerialNumber;
  48. if not IsNet then
  49. dwRetCode := ReadDog
  50. else
  51. begin
  52. if ReadDogData(DogAddr, DogBytes, DogData) then
  53. dwRetCode := 0
  54. else dwRetCode := 1;
  55. end;
  56. if dwRetCode = 0 then
  57. begin
  58. strSN := IntToStr(dwSerialNumber);//IntToHex(dwSerialNumber, 6);
  59. for I := Low(aStrSN) to High(aStrSN) do
  60. aStrSN[I] := StrToInt(strSN[I + 1]);
  61. Encrypt_Simple(aStrSN, Length(aStrSN), aSDSN{, Length(aSDSN)});
  62. Result := DT_SN_SD;
  63. for I := 0 to Length(aSDSN) - 1 do
  64. begin
  65. if aSDSN[I] <> SDSerialNo[I] then
  66. Result := -1;
  67. end;
  68. end;
  69. end;
  70. function CheckSNSTimes(AIsOnOpen: Boolean): Integer;
  71. var
  72. aData: array [0..11] of Byte;
  73. iTimes, iRunTimes: Word;
  74. begin
  75. if IsNet then
  76. begin
  77. Result := CS_Success;
  78. Exit;
  79. end;
  80. Result := CS_Error;
  81. if SimpleSNSCheck < 0 then
  82. Exit;
  83. iTimes := 0;
  84. ZeroMemory(@aData[0], Length(aData));
  85. // 实际是$10-$11,$12-$13,为迷惑,取$0E-$19
  86. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  87. DogAddr := $0E;
  88. DogData := @aData[0];
  89. if ReadDog = 0 then
  90. begin
  91. iTimes := aData[2] shl 8 + aData[3];
  92. iRunTimes := aData[4] shl 8 + aData[5];
  93. end;
  94. if iTimes = 0 then
  95. Exit;
  96. if iTimes = $FFFF then
  97. begin
  98. Result := CS_Success;
  99. Exit;
  100. end;
  101. bAuthorized := False;
  102. Activations := iRunTimes;
  103. if AIsOnOpen then
  104. begin
  105. if iRunTimes >= iTimes then
  106. Result := CS_NeedAuthorize
  107. else
  108. Result := CS_WantAuthorize;
  109. end
  110. else
  111. begin
  112. if iRunTimes > iTimes then
  113. Result := CS_NeedAuthorize
  114. else
  115. Result := CS_WantAuthorize;
  116. end;
  117. end;
  118. function CheckSNSHours: Integer;
  119. var
  120. aData: array [0..11] of Byte;
  121. iSeconds, iRunSeconds: Cardinal;
  122. begin
  123. if IsNet then
  124. begin
  125. Result := CS_Success;
  126. Exit;
  127. end;
  128. Result := CS_Error;
  129. if SimpleSNSCheck < 0 then
  130. Exit;
  131. iSeconds := 0;
  132. ZeroMemory(@aData[0], Length(aData));
  133. // 实际是$08-$0B,$0C-$0F,为迷惑,取$07-$12
  134. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  135. DogAddr := $07;
  136. DogData := @aData[0];
  137. if ReadDog = 0 then
  138. begin
  139. iSeconds := ((aData[1] shl 8 + aData[2]) shl 8 + aData[3]) shl 8 + aData[4];
  140. iRunSeconds := ((aData[5] shl 8 + aData[6]) shl 8 + aData[7]) shl 8 + aData[8];
  141. end;
  142. if iSeconds = 0 then
  143. Exit;
  144. if iSeconds = $FFFFFFFF then
  145. begin
  146. Result := CS_Success;
  147. Exit;
  148. end;
  149. bAuthorized := False;
  150. LastTime := iRunSeconds;
  151. if iRunSeconds > iSeconds then
  152. Result := CS_NeedAuthorize
  153. else
  154. Result := CS_WantAuthorize;
  155. end;
  156. function CheckSNSLimitDate: Integer;
  157. var
  158. aData: array [0..11] of Byte;
  159. iType: Byte;
  160. iUserDate, iLimitDate: Cardinal;
  161. fDate, fCompileDate: TDateTime;
  162. begin
  163. if IsNet then
  164. begin
  165. Result := CS_Success;
  166. Exit;
  167. end;
  168. Result := CS_Error;
  169. if SimpleSNSCheck < 0 then
  170. Exit;
  171. // 先判断是否时间限制狗
  172. ZeroMemory(@aData[0], Length(aData));
  173. // 实际是$51,为迷惑,取$48-$53
  174. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  175. DogAddr := $48;
  176. DogData := @aData[0];
  177. if (ReadDog = 0) and (aData[9] <> Ord(etDate)) then
  178. begin
  179. Result := CS_Ignore;
  180. Exit;
  181. end;
  182. bAuthorized := False;
  183. ZeroMemory(@aData[0], Length(aData));
  184. // 实际是$20-$23,$24-$27,为迷惑,取$1E-$29
  185. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  186. DogAddr := $1E;
  187. DogData := @aData[0];
  188. if ReadDog = 0 then
  189. begin
  190. iUserDate := ((aData[2] shl 8 + aData[3]) shl 8 + aData[4]) shl 8 + aData[5];
  191. iLimitDate := ((aData[6] shl 8 + aData[7]) shl 8 + aData[8]) shl 8 + aData[9];
  192. end;
  193. fDate := PHPWeb.SystemDateTime;
  194. fCompileDate := CompileDateTime;
  195. if fCompileDate > iLimitDate then
  196. begin
  197. Result := CS_EndDate;
  198. Exit;
  199. end;
  200. if fDate > iLimitDate then
  201. Result := CS_EndDate
  202. else if iLimitDate - fDate < 7 then
  203. Result := CS_CloseToLimitDate
  204. else
  205. Result := CS_Success;
  206. end;
  207. function CheckSNSAdditionEdition: Integer;
  208. var
  209. aData: array [0..15] of Byte;
  210. iEdition: Byte;
  211. dwRetCode: Integer;
  212. begin
  213. Result := CS_NoDog;
  214. if SimpleSNSCheck < 0 then
  215. Exit;
  216. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  217. DogAddr := $4E;
  218. DogData := @aData[0];
  219. if not IsNet then
  220. dwRetCode := ReadDog
  221. else
  222. begin
  223. if ReadDogData(DogAddr, DogBytes, DogData) then
  224. dwRetCode := 0
  225. else dwRetCode := 1;
  226. end;
  227. if dwRetCode = 0 then
  228. begin
  229. {$IFDEF _ScBills}
  230. if aData[0] = $08 then
  231. {$ENDIF}
  232. {$IFDEF _ScBudget}
  233. {$IFDEF _ScEstimate}
  234. if aData[2] = $24 then
  235. {$ELSE}
  236. if aData[1] = $16 then
  237. {$ENDIF}
  238. {$ENDIF}
  239. Result := CS_Success
  240. else
  241. Result := CS_NeedUpdate;
  242. end;
  243. end;
  244. function CheckSNSEdition: Integer;
  245. var
  246. aData: array [0..11] of Byte;
  247. iEdition: Byte;
  248. dwRetCode: Integer;
  249. begin
  250. Result := CS_NoDog;
  251. if SimpleSNSCheck < 0 then
  252. Exit;
  253. iEdition := 0;
  254. // 实际是$07,为迷惑,取$02-$0D
  255. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  256. DogAddr := $02;
  257. DogData := @aData[0];
  258. if not IsNet then
  259. dwRetCode := ReadDog
  260. else
  261. begin
  262. if ReadDogData(DogAddr, DogBytes, DogData) then
  263. dwRetCode := 0
  264. else dwRetCode := 1;
  265. end;
  266. if dwRetCode = 0 then
  267. iEdition := aData[5];
  268. if iEdition = 0 then
  269. Exit;
  270. DogEdition := iEdition;
  271. if CheckEncryptEdition(iEdition) then
  272. (* if iEdition in {$IFDEF _ScPersonal}
  273. [164] // 个人版
  274. {$ENDIF}
  275. {$IFDEF _ScBillsForArch}
  276. {$IFDEF _ScBills}
  277. [178, 162] // 建筑版 ,招投标版
  278. {$ELSE}
  279. [178] // 建筑版
  280. {$ENDIF}
  281. {$ELSE}
  282. {$IFDEF _ScBills}
  283. {$IFDEF _ScJiangXi}
  284. [34] // 江西专业版
  285. {$ELSE}
  286. {$IFDEF _ScGanSu}
  287. {$IFDEF _ScGanSu_XX}
  288. [88] // 甘肃农村版
  289. {$ELSE}
  290. [81] // 甘肃专业版
  291. {$ENDIF}
  292. {$ELSE}
  293. {$IFDEF _ScHeBei}
  294. [61] // 河北专业版
  295. {$ELSE}
  296. [161, 162] // 专业版/招投标版
  297. {$ENDIF}
  298. {$ENDIF}
  299. {$ENDIF}
  300. {$ENDIF}
  301. {$IFDEF _ScBudget}
  302. {$IFDEF _ScGanSu}
  303. {$IFDEF _ScGanSu_XX}
  304. [88] // 甘肃农村版
  305. {$ELSE}
  306. [81, 82] // 甘肃专业版/甘肃概预估算版
  307. {$ENDIF}
  308. {$ELSE}
  309. {$IFDEF _ScJiangXi}
  310. {$IFDEF _ScYangHu_JiangXi}
  311. [33] // 江西养护预算版
  312. {$ELSE}
  313. [33, 34] // 江西专业版
  314. {$ENDIF}
  315. {$ELSE}
  316. {$IFDEF _ScHeBei} // 河北专业版
  317. [61]
  318. {$ELSE}
  319. {$IFDEF _ScEstimate}
  320. [161, 163] // 专业版/概预估算版
  321. {$ELSE}
  322. [161, 163, 33] // 专业版/概预估算版/江西养护预算版
  323. {$ENDIF}
  324. {$ENDIF}
  325. {$ENDIF}
  326. {$ENDIF}
  327. {$ENDIF}
  328. {$ENDIF} then*)
  329. Result := CS_Success
  330. else
  331. Result := CS_DogTypeError;
  332. end;
  333. function CheckSNSData: Integer;
  334. var
  335. iCheckData, iOutputLength: Integer;
  336. pKey: Pointer;
  337. arrKey: array [0..11] of Byte;
  338. arrKey2: array [0..11] of Byte;
  339. arrSN: array [0..11] of Byte;
  340. I, dwRetCode: Integer;
  341. bIsZero: Boolean;
  342. begin
  343. Result := CS_NoDog;
  344. if SimpleSNSCheck < 0 then
  345. Exit;
  346. Randomize;
  347. iCheckData := RandomRange(11000, 11050);
  348. case iCheckData of
  349. 11011..11020:
  350. begin
  351. Result := CS_Ignore;
  352. Exit;
  353. end;
  354. end;
  355. //strS4SN := '';
  356. ZeroMemory(@arrKey[0], Length(arrKey));
  357. // 实际是$00-$05,为迷惑,取$00-$0B
  358. DogBytes := Length(arrKey); {if you want to Read SerialNo DogBytes must be 0}
  359. DogAddr := $00;
  360. DogData := @arrKey[0];
  361. if not IsNet then
  362. dwRetCode := ReadDog
  363. else
  364. begin
  365. if ReadDogData(DogAddr, DogBytes, DogData) then
  366. dwRetCode := 0
  367. else dwRetCode := 1;
  368. end;
  369. if dwRetCode = 0 then
  370. begin
  371. bIsZero := True;
  372. for I := 0 to 5 do
  373. begin
  374. if arrKey[I] <> 0 then
  375. begin
  376. bIsZero := False;
  377. Break;
  378. end;
  379. end;
  380. if bIsZero then
  381. Exit;
  382. end
  383. else
  384. Exit;
  385. ZeroMemory(@arrKey2[0], Length(arrKey2));
  386. ZeroMemory(@arrSN[0], Length(arrSN));
  387. // 实际是$14-$1F
  388. DogBytes := Length(arrSN); {if you want to Read SerialNo DogBytes must be 0}
  389. DogAddr := $14;
  390. DogData := @arrSN[0];
  391. if not IsNet then
  392. dwRetCode := ReadDog
  393. else
  394. begin
  395. if ReadDogData(DogAddr, DogBytes, DogData) then
  396. dwRetCode := 0
  397. else dwRetCode := 1;
  398. end;
  399. if dwRetCode = 0 then
  400. begin
  401. pKey := nil;
  402. try
  403. Decrypt_BlowFish(EncryptKey, @arrSN[0], 12, pKey, iOutputLength);
  404. CopyMemory(@arrKey2[0], pKey, iOutputLength);
  405. finally
  406. if Assigned(pKey) then
  407. FreeMem(pKey);
  408. end;
  409. end;
  410. for I := 0 to 5 do
  411. begin
  412. if arrKey[I] <> arrKey2[I] then
  413. begin
  414. Result := CS_VerifyError;
  415. Exit;
  416. Break;
  417. end;
  418. end;
  419. strHaspID := '';
  420. for I := 0 to 5 do
  421. strHaspID := strHaspID + IntToHex(arrKey[I], 1);
  422. Result := CS_Success;
  423. end;
  424. function GetSNSReportOprNum(AOpr: string): Integer;
  425. const
  426. OprStr = '[,-,*,^,:=,/,p,<,<=,>=,>,+,<>,=,or,!=,and,f';
  427. var
  428. OprStrs: TStringList;
  429. StrIndex, dwRetCode: Integer;
  430. OprNum: Byte;
  431. aData: array [0..19] of Byte;
  432. begin
  433. Result := -1;
  434. StrIndex := GetReportOprList.IndexOf(AOpr);
  435. if StrIndex > -1 then
  436. begin
  437. Result := Integer(GetReportOprList.Objects[StrIndex]);
  438. end
  439. else
  440. begin
  441. OprStrs := TStringList.Create;
  442. try
  443. OprStrs.CommaText := OprStr;
  444. DogBytes := Length(aData);
  445. DogAddr := $30;
  446. DogData := @aData[0];
  447. if not IsNet then
  448. dwRetCode := ReadDog
  449. else
  450. begin
  451. if ReadDogData(DogAddr, DogBytes, DogData) then
  452. dwRetCode := 0
  453. else dwRetCode := 1;
  454. end;
  455. if dwRetCode = 0 then
  456. begin
  457. StrIndex := OprStrs.IndexOf(AOpr);
  458. if StrIndex >= 0 then
  459. begin
  460. OprNum := aData[StrIndex];
  461. GetReportOprList.AddObject(AOpr, Pointer(OprNum));
  462. Result := OprNum;
  463. end;
  464. end;
  465. finally
  466. OprStrs.Free;
  467. end;
  468. end;
  469. end;
  470. function GetSNSReportFuncNum(AOpr: string): Integer;
  471. const
  472. FuncStr = 'sql,treechapterinsert,getindex,addnull,shrink,copy,'+
  473. 'sorttreechapter,joindata,chapterinsert,'+
  474. 'refcopy,count,refcopyex1';
  475. var
  476. FuncStrs: TStringList;
  477. StrIndex, dwRetCode: Integer;
  478. OprNum: Byte;
  479. aData: array [0..19] of Byte;
  480. begin
  481. Result := -1;
  482. StrIndex := GetReportFuncList.IndexOf(AOpr);
  483. if StrIndex > -1 then
  484. begin
  485. Result := Integer(GetReportFuncList.Objects[StrIndex]);
  486. end
  487. else
  488. begin
  489. FuncStrs := TStringList.Create;
  490. try
  491. FuncStrs.CommaText := FuncStr;
  492. DogBytes := Length(aData);
  493. DogAddr := $42;
  494. DogData := @aData[0];
  495. if not IsNet then
  496. dwRetCode := ReadDog
  497. else
  498. begin
  499. if ReadDogData(DogAddr, DogBytes, DogData) then
  500. dwRetCode := 0
  501. else dwRetCode := 1;
  502. end;
  503. if dwRetCode = 0 then
  504. begin
  505. StrIndex := FuncStrs.IndexOf(AOpr);
  506. if StrIndex >= 0 then
  507. begin
  508. OprNum := aData[StrIndex];
  509. GetReportFuncList.AddObject(AOpr, Pointer(900 + OprNum));
  510. Result := 900 + OprNum;
  511. end;
  512. end;
  513. finally
  514. FuncStrs.Free;
  515. end;
  516. end;
  517. end;
  518. function OpenSNSDog: Integer;
  519. var
  520. aData: array [0..11] of Byte;
  521. iTimes: Word;
  522. iTimes1, iTimes2: Byte;
  523. begin
  524. if IsNet then
  525. begin
  526. Result := CS_Success;
  527. Exit;
  528. end;
  529. Result := CS_Error;
  530. // 实际是$10-$11,$12-$13,为迷惑,取$0C-$17
  531. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  532. DogAddr := $0C;
  533. DogData := @aData[0];
  534. if ReadDog = 0 then
  535. begin
  536. iTimes := aData[6] shl 8 + aData[7];
  537. end
  538. else
  539. Exit;
  540. iTimes := iTimes + 1;
  541. iTimes1 := iTimes shr 8;
  542. iTimes2 := iTimes and $00FF;
  543. DogBytes := 1; {if you want to Read SerialNo DogBytes must be 0}
  544. DogAddr := $12;
  545. DogData := @iTimes1;
  546. if WriteDog <> 0 then
  547. Exit;
  548. DogBytes := 1; {if you want to Read SerialNo DogBytes must be 0}
  549. DogAddr := $13;
  550. DogData := @iTimes2;
  551. if WriteDog <> 0 then
  552. Exit;
  553. Result := CS_Success;
  554. end;
  555. procedure SaveSNSDog;
  556. var
  557. aData: array [0..11] of Byte;
  558. aSecData: array [0..3] of Byte;
  559. iLastTime, iRunSeconds: LongWord;
  560. iBytesReturned: Cardinal;
  561. begin
  562. if IsNet then Exit;
  563. RunTime := GetTickCount div 1000;
  564. if RunTime < StartTime then
  565. begin
  566. iLastTime := High(LongWord) div 1000 + RunTime - StartTime;
  567. end
  568. else
  569. iLastTime := RunTime - StartTime;
  570. // 实际是$08-$0B,$0C-$0F,为迷惑,取$04-$0F
  571. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  572. DogAddr := $04;
  573. DogData := @aData[0];
  574. if ReadDog = 0 then
  575. begin
  576. iRunSeconds := ((aData[$08] shl 8 + aData[$09]) shl 8 + aData[$0A]) shl 8 + aData[$0B];
  577. iLastTime := iLastTime + iRunSeconds;
  578. aSecData[$00] := iLastTime shr 24;
  579. aSecData[$01] := (iLastTime shr 16) and $00FF;
  580. aSecData[$02] := (iLastTime and $0000FFFF) shr 8;
  581. aSecData[$03] := iLastTime and $000000FF;
  582. DogBytes := Length(aSecData); {if you want to Read SerialNo DogBytes must be 0}
  583. DogAddr := $0C;
  584. DogData := @aSecData[0];
  585. WriteDog;
  586. end;
  587. end;
  588. function _SNSUserAuthorize10(AAuthorize: array of Byte): Boolean;
  589. var
  590. iOutputLength: Integer;
  591. arrSN: array [0..5] of Byte;
  592. arrSN2: array [0..5] of Byte;
  593. arrKey: array [0..11] of Byte;
  594. pEncryptKey: Pointer;
  595. pKey: Pointer;
  596. pSN: Pointer;
  597. bRightKey: Boolean;
  598. strS4SNDC, strS4SN: string[6];
  599. pData: _PEncryptData10;
  600. iFileSize, iDataSize: Integer;
  601. iBytesReturned: Cardinal;
  602. aData2002: array [0..1] of Byte;
  603. aData2003: array [0..11] of Byte;
  604. I: Integer;
  605. begin
  606. Result := False;
  607. iDataSize := SizeOf(_TEncryptData10);
  608. iFileSize := iDataSize;
  609. if iFileSize mod 8 <> 0 then
  610. iFileSize := 8 * (iFileSize div 8 + 1);
  611. pEncryptKey := AllocMem(iFileSize);
  612. pKey := nil;
  613. New(pData);
  614. try
  615. CopyMemory(pEncryptKey, @AAuthorize[0], iFileSize);
  616. Decrypt_BlowFish(AuthorizeKey, pEncryptKey, iFileSize, pKey, iDataSize);
  617. CopyMemory(pData, pKey, iDataSize);
  618. CopyMemory(@arrKey[0], @(pData^.Key[0]), Length(arrKey));
  619. pSN := nil;
  620. try
  621. Decrypt_BlowFish(AuthorizeKey, @arrKey[0], Length(arrKey), pSN, iOutputLength);
  622. ZeroMemory(@arrSN2[0], Length(arrSN2));
  623. CopyMemory(@arrSN2[0], pSN, Length(arrSN2));
  624. finally
  625. if Assigned(pSN) then
  626. FreeMem(pSN);
  627. end;
  628. strS4SNDC := '';
  629. for I := Low(arrSN2) to High(arrSN2) do
  630. strS4SNDC := strS4SNDC + IntToHex(arrSN2[I], 1);
  631. // 读出序列号
  632. DogBytes := Length(arrSN);
  633. DogAddr := $00;
  634. DogData := @arrSN[0];
  635. ZeroMemory(@arrSN[0], Length(arrSN));
  636. if ReadDog = 0 then
  637. begin
  638. strS4SN := '';
  639. for I := Low(arrSN) to High(arrSN) do
  640. strS4SN := strS4SN + IntToHex(arrSN[I], 1);
  641. end
  642. else
  643. Exit;
  644. // 如果验证序列号相同,设置允许时间和允许次数
  645. if SameText(strS4SN, strS4SNDC) then
  646. begin
  647. DogBytes := Length(aData2003);
  648. DogAddr := $08;
  649. DogData := @aData2003[0];
  650. ZeroMemory(@aData2003[0], Length(aData2003));
  651. if ReadDog = 0 then
  652. begin
  653. {do nothing}
  654. end
  655. else
  656. Exit;
  657. // 允许时间
  658. aData2003[0] := pData^.LimitSeconds shr 24;
  659. aData2003[1] := (pData^.LimitSeconds shr 16) and $00FF;
  660. aData2003[2] := (pData^.LimitSeconds and $0000FFFF) shr 8;
  661. aData2003[3] := pData^.LimitSeconds and $000000FF;
  662. // 允许次数
  663. aData2003[8] := pData^.LimitTimes shr 8;
  664. aData2003[9] := pData^.LimitTimes and $00FF;
  665. DogBytes := Length(aData2003);
  666. DogAddr := $08;
  667. DogData := @aData2003[0];
  668. if WriteDog = 0 then
  669. begin
  670. end
  671. else
  672. Exit;
  673. // 版本
  674. DogBytes := Length(aData2002);
  675. DogAddr := $06;
  676. DogData := @aData2002[0];
  677. aData2002[0] := pData^.ProductID;
  678. aData2002[1] := pData^.Editon;
  679. if WriteDog = 0 then
  680. begin
  681. end
  682. else
  683. Exit;
  684. end
  685. else
  686. Exit;
  687. bAuthorized := True;
  688. Result := True;
  689. finally
  690. Dispose(pData);
  691. if Assigned(pKey) then
  692. FreeMem(pKey);
  693. if Assigned(pEncryptKey) then
  694. FreeMem(pEncryptKey);
  695. end;
  696. end;
  697. function _SNSUserAuthorize20(AAuthorize: array of Byte): Boolean;
  698. var
  699. iOutputLength: Integer;
  700. arrSN: array [0..5] of Byte;
  701. arrSN2: array [0..5] of Byte;
  702. arrKey: array [0..11] of Byte;
  703. pEncryptKey: Pointer;
  704. pKey: Pointer;
  705. pSN: Pointer;
  706. bRightKey: Boolean;
  707. strS4SNDC, strS4SN: string[6];
  708. pData: PEncryptData;
  709. iFileSize, iDataSize: Integer;
  710. iBytesReturned: Cardinal;
  711. aData2002: array [0..1] of Byte;
  712. aData2003: array [0..11] of Byte;
  713. aDataLimitDate: array [0..3] of Byte;
  714. I: Integer;
  715. begin
  716. Result := False;
  717. iDataSize := SizeOf(TEncryptData);
  718. iFileSize := iDataSize;
  719. if iFileSize mod 8 <> 0 then
  720. iFileSize := 8 * (iFileSize div 8 + 1) + SizeOf(Integer);
  721. pEncryptKey := AllocMem(iFileSize);
  722. pKey := nil;
  723. New(pData);
  724. try
  725. CopyMemory(pEncryptKey, @AAuthorize[0], iFileSize);
  726. Decrypt_BlowFish(AuthorizeKey, pEncryptKey, iFileSize, pKey, iDataSize);
  727. CopyMemory(pData, pKey, iDataSize);
  728. CopyMemory(@arrKey[0], @(pData^.Key[0]), Length(arrKey));
  729. pSN := nil;
  730. try
  731. Decrypt_BlowFish(AuthorizeKey, @arrKey[0], Length(arrKey), pSN, iOutputLength);
  732. ZeroMemory(@arrSN2[0], Length(arrSN2));
  733. CopyMemory(@arrSN2[0], pSN, Length(arrSN2));
  734. finally
  735. if Assigned(pSN) then
  736. FreeMem(pSN);
  737. end;
  738. strS4SNDC := '';
  739. for I := Low(arrSN2) to High(arrSN2) do
  740. strS4SNDC := strS4SNDC + IntToHex(arrSN2[I], 1);
  741. // 读出序列号
  742. DogBytes := Length(arrSN);
  743. DogAddr := $00;
  744. DogData := @arrSN[0];
  745. ZeroMemory(@arrSN[0], Length(arrSN));
  746. if ReadDog = 0 then
  747. begin
  748. strS4SN := '';
  749. for I := Low(arrSN) to High(arrSN) do
  750. strS4SN := strS4SN + IntToHex(arrSN[I], 1);
  751. end
  752. else
  753. Exit;
  754. // 如果验证序列号相同,设置允许时间和允许次数
  755. if SameText(strS4SN, strS4SNDC) then
  756. begin
  757. DogBytes := Length(aData2003);
  758. DogAddr := $08;
  759. DogData := @aData2003[0];
  760. ZeroMemory(@aData2003[0], Length(aData2003));
  761. if ReadDog = 0 then
  762. begin
  763. {do nothing}
  764. end
  765. else
  766. Exit;
  767. // 允许时间
  768. aData2003[0] := pData^.LimitSeconds shr 24;
  769. aData2003[1] := (pData^.LimitSeconds shr 16) and $00FF;
  770. aData2003[2] := (pData^.LimitSeconds and $0000FFFF) shr 8;
  771. aData2003[3] := pData^.LimitSeconds and $000000FF;
  772. // 允许次数
  773. aData2003[8] := pData^.LimitTimes shr 8;
  774. aData2003[9] := pData^.LimitTimes and $00FF;
  775. DogBytes := Length(aData2003);
  776. DogAddr := $08;
  777. DogData := @aData2003[0];
  778. if WriteDog = 0 then
  779. begin
  780. end
  781. else
  782. Exit;
  783. // 版本
  784. DogBytes := Length(aData2002);
  785. DogAddr := $06;
  786. DogData := @aData2002[0];
  787. aData2002[0] := pData^.ProductID;
  788. aData2002[1] := pData^.Editon;
  789. if WriteDog = 0 then
  790. begin
  791. end
  792. else
  793. Exit;
  794. // 限制时间
  795. aDataLimitDate[$0] := pData^.LimitDate shr 24;
  796. aDataLimitDate[$1] := (pData^.LimitDate shr 16) and $00FF;
  797. aDataLimitDate[$2] := (pData^.LimitDate and $0000FFFF) shr 8;
  798. aDataLimitDate[$3] := pData^.LimitDate and $000000FF;
  799. DogBytes := Length(aDataLimitDate);
  800. DogAddr := $24;
  801. DogData := @aDataLimitDate[0];
  802. if WriteDog = 0 then
  803. begin
  804. end
  805. else
  806. Exit;
  807. end
  808. else
  809. Exit;
  810. bAuthorized := True;
  811. Result := True;
  812. finally
  813. Dispose(pData);
  814. if Assigned(pKey) then
  815. FreeMem(pKey);
  816. if Assigned(pEncryptKey) then
  817. FreeMem(pEncryptKey);
  818. end;
  819. end;
  820. function SNSUserAuthorize(AAuthorize: array of Byte): Boolean;
  821. var
  822. pHead: PAuthFileHead20;
  823. Key: array [0..255] of Byte;
  824. iPos: Integer;
  825. begin
  826. iPos := SizeOf(TAuthFileHead20);
  827. New(pHead);
  828. try
  829. CopyMemory(pHead, @AAuthorize[0], iPos);
  830. if SameText(pHead^.FileInfo, SAuthFileInfo) and
  831. (pHead^.Version = SAuthFileVer20) then
  832. begin
  833. CopyMemory(@Key[0], @AAuthorize[iPos], 255 - iPos);
  834. _SNSUserAuthorize20(Key);
  835. end
  836. else
  837. _SNSUserAuthorize10(AAuthorize);
  838. finally
  839. Dispose(pHead);
  840. end;
  841. end;
  842. const
  843. iDataSize: Integer = 16;
  844. iSNSize: Integer = 6;
  845. function SNSUserUpdateLock(AUpdateData: array of Byte): Boolean;
  846. var
  847. iOutputLength: Integer;
  848. arrSN: array [0..5] of Byte;
  849. arrSN2: array [0..5] of Byte;
  850. pEncryptKey: Pointer;
  851. pKey: Pointer;
  852. strS4SNDC, strS4SN: string[6];
  853. iFileSize: Integer;
  854. aData: array [0..21] of Byte;
  855. aUpdate: array [0..15] of Byte;
  856. I: Integer;
  857. begin
  858. if IsNet then
  859. begin
  860. Result := True;
  861. Exit;
  862. end;
  863. Result := False;
  864. iFileSize := iDataSize + 6;
  865. if iFileSize mod 8 <> 0 then
  866. iFileSize := 8 * (iFileSize div 8 + 1);
  867. iFileSize := iFileSize + 4;
  868. pEncryptKey := AllocMem(iFileSize);
  869. pKey := nil;
  870. try
  871. ZeroMemory(@aData[0], Length(aData));
  872. ZeroMemory(@aUpdate[0], Length(aUpdate));
  873. CopyMemory(pEncryptKey, @AUpdateData[0], iFileSize);
  874. Decrypt_BlowFish(AuthorizeKey, pEncryptKey, iFileSize, pKey, iFileSize);
  875. CopyMemory(@aData[0], pKey, Length(aData));
  876. CopyMemory(@arrSN2[0], @aData[0], iSNSize);
  877. CopyMemory(@aUpdate[0], @aData[iSNSize], Length(aUpdate));
  878. strS4SNDC := '';
  879. for I := Low(arrSN2) to High(arrSN2) do
  880. strS4SNDC := strS4SNDC + IntToHex(arrSN2[I], 1);
  881. // 读出序列号
  882. DogBytes := Length(arrSN);
  883. DogAddr := $00;
  884. DogData := @arrSN[0];
  885. ZeroMemory(@arrSN[0], Length(arrSN));
  886. if ReadDog = 0 then
  887. begin
  888. strS4SN := '';
  889. for I := Low(arrSN) to High(arrSN) do
  890. strS4SN := strS4SN + IntToHex(arrSN[I], 1);
  891. end
  892. else
  893. Exit;
  894. // 如果验证序列号相同,设置允许升级
  895. if SameText(strS4SN, strS4SNDC) then
  896. begin
  897. // 版本
  898. DogBytes := Length(aUpdate);
  899. DogAddr := $4E;
  900. DogData := @aUpdate[0];
  901. if WriteDog = 0 then
  902. begin
  903. end
  904. else
  905. Exit;
  906. end
  907. else
  908. Exit;
  909. Result := True;
  910. finally
  911. if Assigned(pKey) then
  912. FreeMem(pKey);
  913. if Assigned(pEncryptKey) then
  914. FreeMem(pEncryptKey);
  915. end;
  916. end;
  917. initialization
  918. {$IFDEF _ScNet}
  919. IsNet := True;
  920. {$IFDEF _ScBills}
  921. Open(0);
  922. {$ELSE}
  923. {$IFDEF _ScBudget}
  924. open(1);
  925. {$ELSE}
  926. {$IFDEF _ScEstimate}
  927. open(2);
  928. {$ENDIF}
  929. {$ENDIF}
  930. {$ENDIF}
  931. {$ENDIF}
  932. finalization
  933. {$IFDEF _ScNet}
  934. if IsNet then
  935. CloseConnection;
  936. {$ENDIF}
  937. end.