library RSA; { Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. } uses SysUtils, Classes, FGInt in 'FGInt.pas', FGIntPrimeGeneration in 'FGIntPrimeGeneration.PAS', FGIntRSA in 'FGIntRSA.PAS', ranlib in 'ranlib.pas'; {$R *.res} //------------------------------------------------------------------------------ procedure GetDNE(var KeyD: PChar; KeyDMaxCount: Integer; var KeyN: PChar; KeyNMaxCount: Integer; var KeyE: PChar; KeyEMaxCount: Integer); stdcall; var Seed, iLoop, S1, S2: integer; n, e, d, dp, dq, p, q, phi, one, two, gcd, temp, nilgint,te1,te2,te3 : TFGInt; e1, d1, n1: string; begin //生成随机数 Seed := Round(Time()*3600000.0); WRandomInit(seed); for iLoop := 1 to 20 do begin WRandom(); WIRandom(0,1000); end; S1 := WIRandom(0,1000000000); S2 := WIRandom(0,1000000000); //生成密钥 Base10StringToFGInt(IntToStr(S1), p); PrimeSearch(p);//产生质数 Base256StringToFGInt(IntToStr(S2), q);//对可视字符解码,使之成为数字 PrimeSearch(q);//产生质数 FGIntMul(p, q, n);//作p*q=n p.Number[1] := p.Number[1] - 1; //把p的最后一位减1 q.Number[1] := q.Number[1] - 1; FGIntMul(p, q, phi); //作(p-1)*(q-1)=phi //找出与phi(就是(p-1)*(q-1))互质的数(r) Base10StringToFGInt('65537', e); 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; FGIntDestroy(two); FGIntDestroy(one); FGIntDestroy(gcd); 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(e, e1); StrCopy(KeyE, PChar(e1)); FGIntToBase10String(d, d1); StrCopy(KeyD, PChar(d1)); FGIntToBase10String(n, n1); StrCopy(KeyN, PChar(n1)); FGIntDestroy(phi); FGIntDestroy(nilgint); FGIntDestroy(p); FGIntDestroy(q); FGIntDestroy(dp); FGIntDestroy(dq); FGIntDestroy(e); FGIntDestroy(d); FGIntDestroy(n); end; //------------------------------------------------------------------------------ //E,N为公钥 function RealICQRSAEncrypt(P: PChar; KeyE, KeyN: PChar): PChar; stdcall; var strResult, strSource: String; e, n: TFGInt; begin strResult := ''; try strSource := P; Base10StringToFGInt(KeyE, e); Base10StringToFGInt(KeyN, n); RSAEncrypt(strSource, e, n, strSource); ConvertBase256to64(strSource, strResult); FGIntDestroy(e); FGIntDestroy(n); finally Result := PChar(strResult); end; end; //------------------------------------------------------------------------------ //D,N为私钥 function RealICQRSADecrypt(E: PChar; KeyD, KeyN: PChar): PChar; stdcall; var strResult, strSource: String; d,n,nilgint:TFGInt; begin strResult := E; try ConvertBase64to256(strResult, strSource); strResult := ''; Base10StringToFGInt(KeyD, d); Base10StringToFGInt(KeyN, n); RSADecrypt(strSource, d, n, Nilgint, Nilgint, Nilgint, Nilgint, strResult); FGIntDestroy(d); FGIntDestroy(n); FGIntDestroy(nilgint); finally Result := PChar(strResult); end; end; exports GetDNE, RealICQRSAEncrypt, RealICQRSADecrypt; begin IsMultiThread := TRUE; end.