📄 elgamal sample.txt
字号:
uses GInt, PrimeGeneration, ElGamal;
procedure ElGamalSignAndVerify;
var
p, phi, g, x, y, k, one, two, temp, gcd: TGInt;
test, a, b: string;
ok: boolean;
begin
// Enter a random number to generate a prime, i.e.
// incremental search starting from that number
DecStrtoGInt('102336547456161301', p);
PrimeSearch(p);
// Compute phi(p)
GIntCopy(p, phi);
phi^.value := phi^.value - 1;
// x is your secret key
DecStrToGInt('1203', x);
// g is any number
DecStrToGInt('21316465461203', g);
// k a random value, such that GCD(k,phi)=1, NEVER use the same k twice
DecStrToGInt('1131', k);
DecStrToGInt('1', one);
DecStrToGInt('2', two);
GIntGCD(phi, k, gcd);
while GIntCompareAbs(gcd, one) <> Eq do
begin
GIntDestroy(gcd);
GIntadd(k, two, temp);
GIntDestroy(k);
k := temp;
GIntGCD(phi, k, gcd);
end;
GIntDestroy(two);
GIntDestroy(one);
GIntDestroy(gcd);
// Now everything is set up to sign and verify
test := 'eagles may soar high, but weasles do not get sucked into jet engines';
ElGamalSign(test, p, g, x, k, a, b);
// a and b form the signature
// compute a public key from the secret key: g^x = mod p
GIntModExp(g, x, p, y);
ElGamalVerify(g, y, p, a, b, test, ok);
end;
procedure ElGamalEncryptAndDecrypt;
var
test: string;
p, g, x, y, k: TGInt;
begin
// Setting up parameters
// p a prime number
DecStrToGInt('56683406451', p);
PrimeSearch(p);
// g,x any numbers, x is your secret key
DecStrToGInt('7675', g);
DecStrToGInt('561', x);
// k a random number, never use the same k twice
DecStrToGInt('10651', k);
// y = g^x mod p
GIntModExp(g, x, p, y);
// Now everything is set up to start encrypting and decrypting
test := 'A conscience is what hurts when all your other parts feel so good.';
ElGamalEncrypt(test, g, y, k, p, test);
ElGamalDecrypt(test, x, p, test);
GIntDestroy(p);
GIntDestroy(g);
GIntDestroy(x);
GIntDestroy(y);
GIntDestroy(k);
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -