RSA - 7


function BN_PrimeTest(var a: TBigNum): Integer;

implementation

uses SysUtils;

var primes: array[0..53] of Integer =

( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,

41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,

97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,

157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,

227, 229, 233, 239, 241, 251);

procedure BN_bignum_to_hex(var a: TBigNum; var s: string);

var i: Integer;

begin

i := BIGNUM_DWORD;

while (i>=0) and (a[i]=0) do Dec(i);

18.2

s := '0';

if (i<0) then Exit;

s := '';

while (i>=0) do

begin

s := s + IntToHex(a[i],8);

Dec(i);

end;

while (Length(s)>1) and (s[1]='0') do Delete(s,1,1);

end;

procedure BN_hex_to_bignum(var s: string; var a: TBigNum);

var i,j,l: Integer;

var n1,n2,n3,n4: TBigNum;

var n16: TBigNum;

begin

for i := 0 to BIGNUM_DWORD do a[i] := 0;

for i := 0 to BIGNUM_DWORD do n16[i] := 0; n16[0] := 16;

for i := 0 to BIGNUM_DWORD do n1[i] := 0; n1[0] := 1;

for i := 0 to BIGNUM_DWORD do n2[i] := 0;

l := Length(s);

for i := l downto 1 do

begin

j := Ord(UpCase(s[i]));

case j of

Ord('0')..Ord('9'): j := j - Ord('0');

Ord('A')..Ord('F'): j := j - Ord('A') + 10;

else Exit;

end;

n2[0] := Cardinal(j);

BN_a_mul_b(n1,n2,n3);

BN_a_add_b(a,n3,n4);

Move(n4,a,sizeof(n4));

BN_a_mul_b(n1,n16,n3);

Move(n3,n1,sizeof(n3));

end;

end;

procedure BN_bignum_to_dec(var a: TBigNum; var s: string);

var i: Integer;

var n1,n2: TBigNum;

18.2

var nzero,nten: TBigNum;

begin

for i := 0 to BIGNUM_DWORD do nzero[i] := 0;

for i := 0 to BIGNUM_DWORD do nten[i] := 0; nten[0] := 10;

s := '0';

if (BN_a_cmp_b(a,nzero)=0) then Exit;

Move(a,n1,sizeof(a));

s := '';

repeat

BN_a_mod_b(n1,nten,n2);

s := Chr(n2[0]+48)+s;

BN_a_div_b(n1,nten,n2);

Move(n2,n1,sizeof(n2));

until (BN_a_cmp_b(n1,nzero)=0);

while (Length(s)>1) and (s[1]='0') do Delete(s,1,1);