RSA.dpr 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. library RSA;
  2. { Important note about DLL memory management: ShareMem must be the
  3. first unit in your library's USES clause AND your project's (select
  4. Project-View Source) USES clause if your DLL exports any procedures or
  5. functions that pass strings as parameters or function results. This
  6. applies to all strings passed to and from your DLL--even those that
  7. are nested in records and classes. ShareMem is the interface unit to
  8. the BORLNDMM.DLL shared memory manager, which must be deployed along
  9. with your DLL. To avoid using BORLNDMM.DLL, pass string information
  10. using PChar or ShortString parameters. }
  11. uses
  12. SysUtils,
  13. Classes,
  14. FGInt in 'FGInt.pas',
  15. FGIntPrimeGeneration in 'FGIntPrimeGeneration.PAS',
  16. FGIntRSA in 'FGIntRSA.PAS',
  17. ranlib in 'ranlib.pas';
  18. {$R *.res}
  19. //------------------------------------------------------------------------------
  20. procedure GetDNE(var KeyD: PChar; KeyDMaxCount: Integer;
  21. var KeyN: PChar; KeyNMaxCount: Integer;
  22. var KeyE: PChar; KeyEMaxCount: Integer); stdcall;
  23. var
  24. Seed,
  25. iLoop,
  26. S1, S2: integer;
  27. n, e, d, dp, dq, p, q, phi, one, two, gcd, temp, nilgint,te1,te2,te3 : TFGInt;
  28. e1, d1, n1: string;
  29. begin
  30. //生成随机数
  31. Seed := Round(Time()*3600000.0);
  32. WRandomInit(seed);
  33. for iLoop := 1 to 20 do
  34. begin
  35. WRandom();
  36. WIRandom(0,1000);
  37. end;
  38. S1 := WIRandom(0,1000000000);
  39. S2 := WIRandom(0,1000000000);
  40. //生成密钥
  41. Base10StringToFGInt(IntToStr(S1), p);
  42. PrimeSearch(p);//产生质数
  43. Base256StringToFGInt(IntToStr(S2), q);//对可视字符解码,使之成为数字
  44. PrimeSearch(q);//产生质数
  45. FGIntMul(p, q, n);//作p*q=n
  46. p.Number[1] := p.Number[1] - 1; //把p的最后一位减1
  47. q.Number[1] := q.Number[1] - 1;
  48. FGIntMul(p, q, phi); //作(p-1)*(q-1)=phi
  49. //找出与phi(就是(p-1)*(q-1))互质的数(r)
  50. Base10StringToFGInt('65537', e);
  51. Base10StringToFGInt('1', one);
  52. Base10StringToFGInt('2', two);
  53. FGIntGCD(phi, e, gcd); //检验是否互质的函数,如果PHI和E互质,GCD等于1
  54. While FGIntCompareAbs(gcd, one) <> Eq Do //寻找互质数
  55. Begin
  56. FGIntadd(e, two, temp); //每次循环加2
  57. FGIntCopy(temp, e);
  58. FGIntGCD(phi, e, gcd);
  59. End;
  60. FGIntDestroy(two);
  61. FGIntDestroy(one);
  62. FGIntDestroy(gcd);
  63. FGIntModInv(e, phi, d);
  64. FGIntModInv(e, p, dp);
  65. FGIntModInv(e, q, dq);
  66. p.Number[1] := p.Number[1] + 1;
  67. q.Number[1] := q.Number[1] + 1;
  68. Base10StringToFGInt('100', te1);
  69. Base10StringToFGInt('25', te2);
  70. FGIntMul(te1, te2, te3);
  71. FGIntToBase10String(e, e1);
  72. StrCopy(KeyE, PChar(e1));
  73. FGIntToBase10String(d, d1);
  74. StrCopy(KeyD, PChar(d1));
  75. FGIntToBase10String(n, n1);
  76. StrCopy(KeyN, PChar(n1));
  77. FGIntDestroy(phi);
  78. FGIntDestroy(nilgint);
  79. FGIntDestroy(p);
  80. FGIntDestroy(q);
  81. FGIntDestroy(dp);
  82. FGIntDestroy(dq);
  83. FGIntDestroy(e);
  84. FGIntDestroy(d);
  85. FGIntDestroy(n);
  86. end;
  87. //------------------------------------------------------------------------------
  88. //E,N为公钥
  89. function RealICQRSAEncrypt(P: PChar; KeyE, KeyN: PChar): PChar; stdcall;
  90. var
  91. strResult,
  92. strSource: String;
  93. e, n: TFGInt;
  94. begin
  95. strResult := '';
  96. try
  97. strSource := P;
  98. Base10StringToFGInt(KeyE, e);
  99. Base10StringToFGInt(KeyN, n);
  100. RSAEncrypt(strSource, e, n, strSource);
  101. ConvertBase256to64(strSource, strResult);
  102. FGIntDestroy(e);
  103. FGIntDestroy(n);
  104. finally
  105. Result := PChar(strResult);
  106. end;
  107. end;
  108. //------------------------------------------------------------------------------
  109. //D,N为私钥
  110. function RealICQRSADecrypt(E: PChar; KeyD, KeyN: PChar): PChar; stdcall;
  111. var
  112. strResult,
  113. strSource: String;
  114. d,n,nilgint:TFGInt;
  115. begin
  116. strResult := E;
  117. try
  118. ConvertBase64to256(strResult, strSource);
  119. strResult := '';
  120. Base10StringToFGInt(KeyD, d);
  121. Base10StringToFGInt(KeyN, n);
  122. RSADecrypt(strSource, d, n, Nilgint, Nilgint, Nilgint, Nilgint, strResult);
  123. FGIntDestroy(d);
  124. FGIntDestroy(n);
  125. FGIntDestroy(nilgint);
  126. finally
  127. Result := PChar(strResult);
  128. end;
  129. end;
  130. exports
  131. GetDNE, RealICQRSAEncrypt, RealICQRSADecrypt;
  132. begin
  133. IsMultiThread := TRUE;
  134. end.