⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 网上很少有供asp使用的rsa算法组件
💻 PAS
字号:
unit Unit1;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  ComObj, ActiveX, SysUtils, AspTlb, rsasvr_TLB, StdVcl, ranlib, FGInt, FGIntPrimeGeneration, FGIntRSA;

type
  Tmake = class(TASPObject, Imake)
  protected
    procedure OnEndPage; safecall;
    procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
    function RSAEnc(const RsaE, RsaN, RSAEncStr: WideString): WideString;
      safecall;
    function RSADec(const RsaD, RsaN, RSADecStr: WideString): WideString;
      safecall;
    function GenKey: WideString; safecall;
  end;

implementation

uses ComServ;

procedure Tmake.OnEndPage;
begin
  inherited OnEndPage;
end;

procedure Tmake.OnStartPage(const AScriptingContext: IUnknown);
begin
  inherited OnStartPage(AScriptingContext);
end;

function Tmake.RSAEnc(const RsaE, RsaN, RSAEncStr: WideString): WideString;
var
   test,b64:string;
   e,n:tfgint;
begin
   test:=RSAEncStr;
   Base10StringToFGInt(RsaE, e);
   Base10StringToFGInt(RsaN, n);
   RSAEncrypt(test, e, n, test);
   ConvertBase256to64(test,b64);
   Result:= b64;
   FGIntDestroy(e);
   FGIntDestroy(n);
end;

function Tmake.RSADec(const RsaD, RsaN, RSADecStr: WideString): WideString;
var
   test,b64:string;
   d,n,nilgint:tfgint;
begin
   test:=RSADecStr;
   ConvertBase64to256(test,b64);
   test:='';
   Base10StringToFGInt(RsaD, d);
   Base10StringToFGInt(RsaN, n);
   RSADecrypt(b64, d, n, Nilgint, Nilgint, Nilgint, Nilgint, test);
   Result:=test;
   FGIntDestroy(d);
   FGIntDestroy(n);
   FGIntDestroy(nilgint);
end;

function Tmake.GenKey: WideString;
var
   n, e, d, dp, dq, p, q, phi, one, two, gcd, temp, nilgint,te1,te2,te3 : TFGInt;
   p1,q1,dp1,dq1,e1,d1,n1,nil1,gcdStr:string;
   seed, i: integer;
   s,s2: string;
Begin
   seed := Round(Time()*3600000.0);
   WRandomInit(seed);
   for i := 1 to 20 do begin
     WRandom();
     WIRandom(0,1000);
   end;
   s:=floattostr(WIRandom(0,1000000000));
   s2:=floattostr(WIRandom(0,1000000000));
     //edit1.text:=s;
     //edit2.text:=s2;
     // Enter a random number to generate a prime, i.e.
     // incremental search starting from that number
   Base10StringToFGInt(s, p);
   PrimeSearch(p);//产生质数
   Base256StringToFGInt(s2, q);//对可视字符解码,使之成为数字
   PrimeSearch(q);//产生质数
     // Compute the modulus
   FGIntMul(p, q, n);//作p*q=n
     // Compute p-1, q-1 by adjusting the last digit of the GInt
   p.Number[1] := p.Number[1] - 1;//把p的最后一位减1
   q.Number[1] := q.Number[1] - 1;
     // Compute phi(n)
   FGIntMul(p, q, phi);//作(p-1)*(q-1)=phi
     // Choose a public exponent e such that GCD(e,phi)=1 //找出与phi(就是(p-1)*(q-1))互质的数(r)
     // common values are 3, 65537 but if these aren 't coprime
     // to phi, use the following code

   Base10StringToFGInt('2399993', e); // just an odd starting point
   Base10StringToFGInt('1', one);
   Base10StringToFGInt('2', two);
   FGIntGCD(phi, e, gcd);       //检验是否互质的函数,如果PHI和E互质,GCD等于1
   While FGIntCompareAbs(gcd, one) <> Eq Do     //寻找互质数
   Begin
      FGIntadd(e, two, temp);  //每次循环加2
      FGIntCopy(temp, e);
      FGIntGCD(phi, e, gcd);
   End;
   FGIntToBase10String(e,gcdstr);
   //edit12.text:=gcdstr;
   FGIntDestroy(two);
   FGIntDestroy(one);
   FGIntDestroy(gcd);
    // Compute the modular (multiplicative) inverse of e, i.e. the secret exponent (key)
   FGIntModInv(e, phi, d);
   FGIntModInv(e, p, dp);
   FGIntModInv(e, q, dq);
   p.Number[1] := p.Number[1] + 1;
   q.Number[1] := q.Number[1] + 1;
   Base10StringToFGInt('100', te1);
   Base10StringToFGInt('25', te2);
   FGIntMul(te1,te2,te3);
   FGIntToBase10String(p,p1);
   //edit3.text:=p1;
   FGIntToBase10String(q,q1);
   //edit4.text:=q1;
   FGIntToBase10String(dp,dp1);
   //edit5.text:=dp1;
   FGIntToBase10String(dq,dq1);
   //edit6.text:=dq1;
   FGIntToBase10String(e,e1);
   //edit7.text:=e1;
   FGIntToBase10String(d,d1);
   //edit8.text:=d1;
   FGIntToBase10String(n,n1);
   //edit9.text:=n1;
   FGIntToBase10String(phi,nil1);
   //edit10.text:=nil1;

   result:=e1+'|'+d1+'|'+n1;
   
   FGIntDestroy(phi);
   FGIntDestroy(nilgint);
   FGIntDestroy(p);
   FGIntDestroy(q);
   FGIntDestroy(dp);
   FGIntDestroy(dq);
   FGIntDestroy(e);
   FGIntDestroy(d);
   FGIntDestroy(n);
end;

initialization
  TAutoObjectFactory.Create(ComServer, Tmake, Class_make,
    ciMultiInstance, tmApartment);
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -