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

📄 rsa sample.txt

📁 一个RSA的实现原代码
💻 TXT
字号:
uses GInt, PrimeGeneration, RSA;

Procedure RSAEncryptAndDecrypt;
var	
	n,e,d,p,q,phi,one,two,gcd,temp:TGInt;
	test:string;
begin
	// Enter a random number to generate a prime, i.e. 
	// incremental search starting from that number
DecStrtoGInt('102336547456161301',p);
PrimeSearch(p);
DecStrtoGInt('32454604541979840060313',q);
PrimeSearch(q);
	// Compute the modulus
GIntMul(p,q,n);
	// Compute p-1, q-1 by adjusting the last digit of the GInt
p^.value:=p^.value-1;
q^.value:=q^.value-1;
	// Compute phi(n)
GIntMul(p,q,phi);
GIntDestroy(p);
GIntDestroy(q);
	// Choose a public exponent e such that GCD(e,phi)=1
	// common values are 3, 65537 but if these aren 't coprime
	// to phi, use the following code
DecStrToGInt('65537',e);   // just an odd staring point
DecStrToGInt('1',one);
DecStrToGInt('2',two);
GIntGCD(phi,e,gcd);
while GIntCompareAbs(gcd,one)<>Eq do
begin
GIntDestroy(gcd);
GIntadd(e,two,temp);
GIntDestroy(e);
e:=temp;
GIntGCD(phi,e,gcd);
end;
GIntDestroy(two);
GIntDestroy(one);
GIntDestroy(gcd);
	// Compute the modular (multiplicative) inverse of e, i.e. the secret exponent (key)
GIntModInv(e,phi,d);
GIntDestroy(phi);
	// Now everything is set up to start Encrypting/Decrypting
test:='eagles may soar high, but weasles do not get sucked into jet engines';

RSAEncrypt(test,e,n,test);
RSADecrypt(test,d,n,test);

GIntDestroy(e);
GIntDestroy(d);
GIntDestroy(n);
end;

⌨️ 快捷键说明

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