mSNSEncrypt.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. unit mSNSEncrypt;
  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, mEncryptPWD, CryptUtils, ScConfig,
  21. mEncryptUnit, mEncryptTypes, mEncryptEditions, mConnectEncrypt,
  22. PHPWebDm;
  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. if aData[1] = $16 then
  230. Result := CS_Success
  231. else
  232. Result := CS_NeedUpdate;
  233. end;
  234. end;
  235. function CheckSNSEdition: Integer;
  236. var
  237. aData: array [0..11] of Byte;
  238. iEdition: Byte;
  239. dwRetCode: Integer;
  240. begin
  241. Result := CS_NoDog;
  242. if SimpleSNSCheck < 0 then
  243. Exit;
  244. iEdition := 0;
  245. // 实际是$07,为迷惑,取$02-$0D
  246. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  247. DogAddr := $02;
  248. DogData := @aData[0];
  249. if not IsNet then
  250. dwRetCode := ReadDog
  251. else
  252. begin
  253. if ReadDogData(DogAddr, DogBytes, DogData) then
  254. dwRetCode := 0
  255. else dwRetCode := 1;
  256. end;
  257. if dwRetCode = 0 then
  258. iEdition := aData[5];
  259. if iEdition = 0 then
  260. Exit;
  261. DogEdition := iEdition;
  262. if CheckEncryptEdition(iEdition) then
  263. Result := CS_Success
  264. else
  265. Result := CS_DogTypeError;
  266. end;
  267. function CheckSNSData: Integer;
  268. var
  269. iCheckData, iOutputLength: Integer;
  270. pKey: Pointer;
  271. arrKey: array [0..11] of Byte;
  272. arrKey2: array [0..11] of Byte;
  273. arrSN: array [0..11] of Byte;
  274. I, dwRetCode: Integer;
  275. bIsZero: Boolean;
  276. begin
  277. Result := CS_NoDog;
  278. if SimpleSNSCheck < 0 then
  279. Exit;
  280. Randomize;
  281. iCheckData := RandomRange(11000, 11050);
  282. case iCheckData of
  283. 11011..11020:
  284. begin
  285. Result := CS_Ignore;
  286. Exit;
  287. end;
  288. end;
  289. //strS4SN := '';
  290. ZeroMemory(@arrKey[0], Length(arrKey));
  291. // 实际是$00-$05,为迷惑,取$00-$0B
  292. DogBytes := Length(arrKey); {if you want to Read SerialNo DogBytes must be 0}
  293. DogAddr := $00;
  294. DogData := @arrKey[0];
  295. if not IsNet then
  296. dwRetCode := ReadDog
  297. else
  298. begin
  299. if ReadDogData(DogAddr, DogBytes, DogData) then
  300. dwRetCode := 0
  301. else dwRetCode := 1;
  302. end;
  303. if dwRetCode = 0 then
  304. begin
  305. bIsZero := True;
  306. for I := 0 to 5 do
  307. begin
  308. if arrKey[I] <> 0 then
  309. begin
  310. bIsZero := False;
  311. Break;
  312. end;
  313. end;
  314. if bIsZero then
  315. Exit;
  316. end
  317. else
  318. Exit;
  319. ZeroMemory(@arrKey2[0], Length(arrKey2));
  320. ZeroMemory(@arrSN[0], Length(arrSN));
  321. // 实际是$14-$1F
  322. DogBytes := Length(arrSN); {if you want to Read SerialNo DogBytes must be 0}
  323. DogAddr := $14;
  324. DogData := @arrSN[0];
  325. if not IsNet then
  326. dwRetCode := ReadDog
  327. else
  328. begin
  329. if ReadDogData(DogAddr, DogBytes, DogData) then
  330. dwRetCode := 0
  331. else dwRetCode := 1;
  332. end;
  333. if dwRetCode = 0 then
  334. begin
  335. pKey := nil;
  336. try
  337. Decrypt_BlowFish(EncryptKey, @arrSN[0], 12, pKey, iOutputLength);
  338. CopyMemory(@arrKey2[0], pKey, iOutputLength);
  339. finally
  340. if Assigned(pKey) then
  341. FreeMem(pKey);
  342. end;
  343. end;
  344. for I := 0 to 5 do
  345. begin
  346. if arrKey[I] <> arrKey2[I] then
  347. begin
  348. Result := CS_VerifyError;
  349. Exit;
  350. Break;
  351. end;
  352. end;
  353. strHaspID := '';
  354. for I := 0 to 5 do
  355. strHaspID := strHaspID + IntToHex(arrKey[I], 1);
  356. Result := CS_Success;
  357. end;
  358. function GetSNSReportOprNum(AOpr: string): Integer;
  359. const
  360. OprStr = '[,-,*,^,:=,/,p,<,<=,>=,>,+,<>,=,or,!=,and,f';
  361. var
  362. OprStrs: TStringList;
  363. StrIndex, dwRetCode: Integer;
  364. OprNum: Byte;
  365. aData: array [0..19] of Byte;
  366. begin
  367. Result := -1;
  368. StrIndex := GetReportOprList.IndexOf(AOpr);
  369. if StrIndex > -1 then
  370. begin
  371. Result := Integer(GetReportOprList.Objects[StrIndex]);
  372. end
  373. else
  374. begin
  375. OprStrs := TStringList.Create;
  376. try
  377. OprStrs.CommaText := OprStr;
  378. DogBytes := Length(aData);
  379. DogAddr := $30;
  380. DogData := @aData[0];
  381. if not IsNet then
  382. dwRetCode := ReadDog
  383. else
  384. begin
  385. if ReadDogData(DogAddr, DogBytes, DogData) then
  386. dwRetCode := 0
  387. else dwRetCode := 1;
  388. end;
  389. if dwRetCode = 0 then
  390. begin
  391. StrIndex := OprStrs.IndexOf(AOpr);
  392. if StrIndex >= 0 then
  393. begin
  394. OprNum := aData[StrIndex];
  395. GetReportOprList.AddObject(AOpr, Pointer(OprNum));
  396. Result := OprNum;
  397. end;
  398. end;
  399. finally
  400. OprStrs.Free;
  401. end;
  402. end;
  403. end;
  404. function GetSNSReportFuncNum(AOpr: string): Integer;
  405. const
  406. FuncStr = 'sql,treechapterinsert,getindex,addnull,shrink,copy,'+
  407. 'sorttreechapter,joindata,chapterinsert,'+
  408. 'refcopy,count,refcopyex1';
  409. var
  410. FuncStrs: TStringList;
  411. StrIndex, dwRetCode: Integer;
  412. OprNum: Byte;
  413. aData: array [0..19] of Byte;
  414. begin
  415. Result := -1;
  416. StrIndex := GetReportFuncList.IndexOf(AOpr);
  417. if StrIndex > -1 then
  418. begin
  419. Result := Integer(GetReportFuncList.Objects[StrIndex]);
  420. end
  421. else
  422. begin
  423. FuncStrs := TStringList.Create;
  424. try
  425. FuncStrs.CommaText := FuncStr;
  426. DogBytes := Length(aData);
  427. DogAddr := $42;
  428. DogData := @aData[0];
  429. if not IsNet then
  430. dwRetCode := ReadDog
  431. else
  432. begin
  433. if ReadDogData(DogAddr, DogBytes, DogData) then
  434. dwRetCode := 0
  435. else dwRetCode := 1;
  436. end;
  437. if dwRetCode = 0 then
  438. begin
  439. StrIndex := FuncStrs.IndexOf(AOpr);
  440. if StrIndex >= 0 then
  441. begin
  442. OprNum := aData[StrIndex];
  443. GetReportFuncList.AddObject(AOpr, Pointer(900 + OprNum));
  444. Result := 900 + OprNum;
  445. end;
  446. end;
  447. finally
  448. FuncStrs.Free;
  449. end;
  450. end;
  451. end;
  452. function OpenSNSDog: Integer;
  453. var
  454. aData: array [0..11] of Byte;
  455. iTimes: Word;
  456. iTimes1, iTimes2: Byte;
  457. begin
  458. if IsNet then
  459. begin
  460. Result := CS_Success;
  461. Exit;
  462. end;
  463. Result := CS_Error;
  464. // 实际是$10-$11,$12-$13,为迷惑,取$0C-$17
  465. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  466. DogAddr := $0C;
  467. DogData := @aData[0];
  468. if ReadDog = 0 then
  469. begin
  470. iTimes := aData[6] shl 8 + aData[7];
  471. end
  472. else
  473. Exit;
  474. iTimes := iTimes + 1;
  475. iTimes1 := iTimes shr 8;
  476. iTimes2 := iTimes and $00FF;
  477. DogBytes := 1; {if you want to Read SerialNo DogBytes must be 0}
  478. DogAddr := $12;
  479. DogData := @iTimes1;
  480. if WriteDog <> 0 then
  481. Exit;
  482. DogBytes := 1; {if you want to Read SerialNo DogBytes must be 0}
  483. DogAddr := $13;
  484. DogData := @iTimes2;
  485. if WriteDog <> 0 then
  486. Exit;
  487. Result := CS_Success;
  488. end;
  489. procedure SaveSNSDog;
  490. var
  491. aData: array [0..11] of Byte;
  492. aSecData: array [0..3] of Byte;
  493. iLastTime, iRunSeconds: LongWord;
  494. iBytesReturned: Cardinal;
  495. begin
  496. if IsNet then Exit;
  497. RunTime := GetTickCount div 1000;
  498. if RunTime < StartTime then
  499. begin
  500. iLastTime := High(LongWord) div 1000 + RunTime - StartTime;
  501. end
  502. else
  503. iLastTime := RunTime - StartTime;
  504. // 实际是$08-$0B,$0C-$0F,为迷惑,取$04-$0F
  505. DogBytes := Length(aData); {if you want to Read SerialNo DogBytes must be 0}
  506. DogAddr := $04;
  507. DogData := @aData[0];
  508. if ReadDog = 0 then
  509. begin
  510. iRunSeconds := ((aData[$08] shl 8 + aData[$09]) shl 8 + aData[$0A]) shl 8 + aData[$0B];
  511. iLastTime := iLastTime + iRunSeconds;
  512. aSecData[$00] := iLastTime shr 24;
  513. aSecData[$01] := (iLastTime shr 16) and $00FF;
  514. aSecData[$02] := (iLastTime and $0000FFFF) shr 8;
  515. aSecData[$03] := iLastTime and $000000FF;
  516. DogBytes := Length(aSecData); {if you want to Read SerialNo DogBytes must be 0}
  517. DogAddr := $0C;
  518. DogData := @aSecData[0];
  519. WriteDog;
  520. end;
  521. end;
  522. function _SNSUserAuthorize10(AAuthorize: array of Byte): Boolean;
  523. var
  524. iOutputLength: Integer;
  525. arrSN: array [0..5] of Byte;
  526. arrSN2: array [0..5] of Byte;
  527. arrKey: array [0..11] of Byte;
  528. pEncryptKey: Pointer;
  529. pKey: Pointer;
  530. pSN: Pointer;
  531. bRightKey: Boolean;
  532. strS4SNDC, strS4SN: string[6];
  533. pData: _PEncryptData10;
  534. iFileSize, iDataSize: Integer;
  535. iBytesReturned: Cardinal;
  536. aData2002: array [0..1] of Byte;
  537. aData2003: array [0..11] of Byte;
  538. I: Integer;
  539. begin
  540. Result := False;
  541. iDataSize := SizeOf(_TEncryptData10);
  542. iFileSize := iDataSize;
  543. if iFileSize mod 8 <> 0 then
  544. iFileSize := 8 * (iFileSize div 8 + 1);
  545. pEncryptKey := AllocMem(iFileSize);
  546. pKey := nil;
  547. New(pData);
  548. try
  549. CopyMemory(pEncryptKey, @AAuthorize[0], iFileSize);
  550. Decrypt_BlowFish(AuthorizeKey, pEncryptKey, iFileSize, pKey, iDataSize);
  551. CopyMemory(pData, pKey, iDataSize);
  552. CopyMemory(@arrKey[0], @(pData^.Key[0]), Length(arrKey));
  553. pSN := nil;
  554. try
  555. Decrypt_BlowFish(AuthorizeKey, @arrKey[0], Length(arrKey), pSN, iOutputLength);
  556. ZeroMemory(@arrSN2[0], Length(arrSN2));
  557. CopyMemory(@arrSN2[0], pSN, Length(arrSN2));
  558. finally
  559. if Assigned(pSN) then
  560. FreeMem(pSN);
  561. end;
  562. strS4SNDC := '';
  563. for I := Low(arrSN2) to High(arrSN2) do
  564. strS4SNDC := strS4SNDC + IntToHex(arrSN2[I], 1);
  565. // 读出序列号
  566. DogBytes := Length(arrSN);
  567. DogAddr := $00;
  568. DogData := @arrSN[0];
  569. ZeroMemory(@arrSN[0], Length(arrSN));
  570. if ReadDog = 0 then
  571. begin
  572. strS4SN := '';
  573. for I := Low(arrSN) to High(arrSN) do
  574. strS4SN := strS4SN + IntToHex(arrSN[I], 1);
  575. end
  576. else
  577. Exit;
  578. // 如果验证序列号相同,设置允许时间和允许次数
  579. if SameText(strS4SN, strS4SNDC) then
  580. begin
  581. DogBytes := Length(aData2003);
  582. DogAddr := $08;
  583. DogData := @aData2003[0];
  584. ZeroMemory(@aData2003[0], Length(aData2003));
  585. if ReadDog = 0 then
  586. begin
  587. {do nothing}
  588. end
  589. else
  590. Exit;
  591. // 允许时间
  592. aData2003[0] := pData^.LimitSeconds shr 24;
  593. aData2003[1] := (pData^.LimitSeconds shr 16) and $00FF;
  594. aData2003[2] := (pData^.LimitSeconds and $0000FFFF) shr 8;
  595. aData2003[3] := pData^.LimitSeconds and $000000FF;
  596. // 允许次数
  597. aData2003[8] := pData^.LimitTimes shr 8;
  598. aData2003[9] := pData^.LimitTimes and $00FF;
  599. DogBytes := Length(aData2003);
  600. DogAddr := $08;
  601. DogData := @aData2003[0];
  602. if WriteDog = 0 then
  603. begin
  604. end
  605. else
  606. Exit;
  607. // 版本
  608. DogBytes := Length(aData2002);
  609. DogAddr := $06;
  610. DogData := @aData2002[0];
  611. aData2002[0] := pData^.ProductID;
  612. aData2002[1] := pData^.Editon;
  613. if WriteDog = 0 then
  614. begin
  615. end
  616. else
  617. Exit;
  618. end
  619. else
  620. Exit;
  621. bAuthorized := True;
  622. Result := True;
  623. finally
  624. Dispose(pData);
  625. if Assigned(pKey) then
  626. FreeMem(pKey);
  627. if Assigned(pEncryptKey) then
  628. FreeMem(pEncryptKey);
  629. end;
  630. end;
  631. function _SNSUserAuthorize20(AAuthorize: array of Byte): Boolean;
  632. var
  633. iOutputLength: Integer;
  634. arrSN: array [0..5] of Byte;
  635. arrSN2: array [0..5] of Byte;
  636. arrKey: array [0..11] of Byte;
  637. pEncryptKey: Pointer;
  638. pKey: Pointer;
  639. pSN: Pointer;
  640. bRightKey: Boolean;
  641. strS4SNDC, strS4SN: string[6];
  642. pData: PEncryptData;
  643. iFileSize, iDataSize: Integer;
  644. iBytesReturned: Cardinal;
  645. aData2002: array [0..1] of Byte;
  646. aData2003: array [0..11] of Byte;
  647. aDataLimitDate: array [0..3] of Byte;
  648. I: Integer;
  649. begin
  650. Result := False;
  651. iDataSize := SizeOf(TEncryptData);
  652. iFileSize := iDataSize;
  653. if iFileSize mod 8 <> 0 then
  654. iFileSize := 8 * (iFileSize div 8 + 1) + SizeOf(Integer);
  655. pEncryptKey := AllocMem(iFileSize);
  656. pKey := nil;
  657. New(pData);
  658. try
  659. CopyMemory(pEncryptKey, @AAuthorize[0], iFileSize);
  660. Decrypt_BlowFish(AuthorizeKey, pEncryptKey, iFileSize, pKey, iDataSize);
  661. CopyMemory(pData, pKey, iDataSize);
  662. CopyMemory(@arrKey[0], @(pData^.Key[0]), Length(arrKey));
  663. pSN := nil;
  664. try
  665. Decrypt_BlowFish(AuthorizeKey, @arrKey[0], Length(arrKey), pSN, iOutputLength);
  666. ZeroMemory(@arrSN2[0], Length(arrSN2));
  667. CopyMemory(@arrSN2[0], pSN, Length(arrSN2));
  668. finally
  669. if Assigned(pSN) then
  670. FreeMem(pSN);
  671. end;
  672. strS4SNDC := '';
  673. for I := Low(arrSN2) to High(arrSN2) do
  674. strS4SNDC := strS4SNDC + IntToHex(arrSN2[I], 1);
  675. // 读出序列号
  676. DogBytes := Length(arrSN);
  677. DogAddr := $00;
  678. DogData := @arrSN[0];
  679. ZeroMemory(@arrSN[0], Length(arrSN));
  680. if ReadDog = 0 then
  681. begin
  682. strS4SN := '';
  683. for I := Low(arrSN) to High(arrSN) do
  684. strS4SN := strS4SN + IntToHex(arrSN[I], 1);
  685. end
  686. else
  687. Exit;
  688. // 如果验证序列号相同,设置允许时间和允许次数
  689. if SameText(strS4SN, strS4SNDC) then
  690. begin
  691. DogBytes := Length(aData2003);
  692. DogAddr := $08;
  693. DogData := @aData2003[0];
  694. ZeroMemory(@aData2003[0], Length(aData2003));
  695. if ReadDog = 0 then
  696. begin
  697. {do nothing}
  698. end
  699. else
  700. Exit;
  701. // 允许时间
  702. aData2003[0] := pData^.LimitSeconds shr 24;
  703. aData2003[1] := (pData^.LimitSeconds shr 16) and $00FF;
  704. aData2003[2] := (pData^.LimitSeconds and $0000FFFF) shr 8;
  705. aData2003[3] := pData^.LimitSeconds and $000000FF;
  706. // 允许次数
  707. aData2003[8] := pData^.LimitTimes shr 8;
  708. aData2003[9] := pData^.LimitTimes and $00FF;
  709. DogBytes := Length(aData2003);
  710. DogAddr := $08;
  711. DogData := @aData2003[0];
  712. if WriteDog = 0 then
  713. begin
  714. end
  715. else
  716. Exit;
  717. // 版本
  718. DogBytes := Length(aData2002);
  719. DogAddr := $06;
  720. DogData := @aData2002[0];
  721. aData2002[0] := pData^.ProductID;
  722. aData2002[1] := pData^.Editon;
  723. if WriteDog = 0 then
  724. begin
  725. end
  726. else
  727. Exit;
  728. // 限制时间
  729. aDataLimitDate[$0] := pData^.LimitDate shr 24;
  730. aDataLimitDate[$1] := (pData^.LimitDate shr 16) and $00FF;
  731. aDataLimitDate[$2] := (pData^.LimitDate and $0000FFFF) shr 8;
  732. aDataLimitDate[$3] := pData^.LimitDate and $000000FF;
  733. DogBytes := Length(aDataLimitDate);
  734. DogAddr := $24;
  735. DogData := @aDataLimitDate[0];
  736. if WriteDog = 0 then
  737. begin
  738. end
  739. else
  740. Exit;
  741. end
  742. else
  743. Exit;
  744. bAuthorized := True;
  745. Result := True;
  746. finally
  747. Dispose(pData);
  748. if Assigned(pKey) then
  749. FreeMem(pKey);
  750. if Assigned(pEncryptKey) then
  751. FreeMem(pEncryptKey);
  752. end;
  753. end;
  754. function SNSUserAuthorize(AAuthorize: array of Byte): Boolean;
  755. var
  756. pHead: PAuthFileHead20;
  757. Key: array [0..255] of Byte;
  758. iPos: Integer;
  759. begin
  760. iPos := SizeOf(TAuthFileHead20);
  761. New(pHead);
  762. try
  763. CopyMemory(pHead, @AAuthorize[0], iPos);
  764. if SameText(pHead^.FileInfo, SAuthFileInfo) and
  765. (pHead^.Version = SAuthFileVer20) then
  766. begin
  767. CopyMemory(@Key[0], @AAuthorize[iPos], 255 - iPos);
  768. _SNSUserAuthorize20(Key);
  769. end
  770. else
  771. _SNSUserAuthorize10(AAuthorize);
  772. finally
  773. Dispose(pHead);
  774. end;
  775. end;
  776. const
  777. iDataSize: Integer = 16;
  778. iSNSize: Integer = 6;
  779. function SNSUserUpdateLock(AUpdateData: array of Byte): Boolean;
  780. var
  781. iOutputLength: Integer;
  782. arrSN: array [0..5] of Byte;
  783. arrSN2: array [0..5] of Byte;
  784. pEncryptKey: Pointer;
  785. pKey: Pointer;
  786. strS4SNDC, strS4SN: string[6];
  787. iFileSize: Integer;
  788. aData: array [0..21] of Byte;
  789. aUpdate: array [0..15] of Byte;
  790. I: Integer;
  791. begin
  792. if IsNet then
  793. begin
  794. Result := True;
  795. Exit;
  796. end;
  797. Result := False;
  798. iFileSize := iDataSize + 6;
  799. if iFileSize mod 8 <> 0 then
  800. iFileSize := 8 * (iFileSize div 8 + 1);
  801. iFileSize := iFileSize + 4;
  802. pEncryptKey := AllocMem(iFileSize);
  803. pKey := nil;
  804. try
  805. ZeroMemory(@aData[0], Length(aData));
  806. ZeroMemory(@aUpdate[0], Length(aUpdate));
  807. CopyMemory(pEncryptKey, @AUpdateData[0], iFileSize);
  808. Decrypt_BlowFish(AuthorizeKey, pEncryptKey, iFileSize, pKey, iFileSize);
  809. CopyMemory(@aData[0], pKey, Length(aData));
  810. CopyMemory(@arrSN2[0], @aData[0], iSNSize);
  811. CopyMemory(@aUpdate[0], @aData[iSNSize], Length(aUpdate));
  812. strS4SNDC := '';
  813. for I := Low(arrSN2) to High(arrSN2) do
  814. strS4SNDC := strS4SNDC + IntToHex(arrSN2[I], 1);
  815. // 读出序列号
  816. DogBytes := Length(arrSN);
  817. DogAddr := $00;
  818. DogData := @arrSN[0];
  819. ZeroMemory(@arrSN[0], Length(arrSN));
  820. if ReadDog = 0 then
  821. begin
  822. strS4SN := '';
  823. for I := Low(arrSN) to High(arrSN) do
  824. strS4SN := strS4SN + IntToHex(arrSN[I], 1);
  825. end
  826. else
  827. Exit;
  828. // 如果验证序列号相同,设置允许升级
  829. if SameText(strS4SN, strS4SNDC) then
  830. begin
  831. // 版本
  832. DogBytes := Length(aUpdate);
  833. DogAddr := $4E;
  834. DogData := @aUpdate[0];
  835. if WriteDog = 0 then
  836. begin
  837. end
  838. else
  839. Exit;
  840. end
  841. else
  842. Exit;
  843. Result := True;
  844. finally
  845. if Assigned(pKey) then
  846. FreeMem(pKey);
  847. if Assigned(pEncryptKey) then
  848. FreeMem(pEncryptKey);
  849. end;
  850. end;
  851. initialization
  852. {$IFDEF _ScNet}
  853. IsNet := True;
  854. Open(0);
  855. {$ENDIF}
  856. finalization
  857. {$IFDEF _ScNet}
  858. if IsNet then
  859. CloseConnection;
  860. {$ENDIF}
  861. end.