ScSNSEncrypt.pas 24 KB

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