#C2.txt, Jan. 22, 2024 Help:=proc(): print(` NextPrime1(n),MakeRSAkey(D1), ME1s(a,e,n), ME1(a,e,n), RSAe(M,n,e), CR2(p,q), EA1(n,m), EA(n,m) `): end: #NextPrime1(n): inputs a pos. integer n #and outputs the first prime >=n NextPrime1:=proc(n) local i: for i from n while not isprime(i) do od: i: end: #ME1s(a,e,n): a^e mod n,the stupid way ME1s:=proc(a,e,n) local i,s: s:=1: for i from 1 to e do s:=s*a mod n: od: s: end: #ME1(a,e,n): a^e mod n,the smart way ME1:=proc(a,e,n): if e=1 then RETURN(a mod n): fi: if e mod 2=0 then RETURN(ME1(a,e/2,n)^2 mod n): else RETURN(ME1(a,e-1,n)*a mod n): fi: end: #MakeRSAkey: The key [n,e]: a->a^e mod n #n must be a product of two primes #inputs D1 and outputs an RSA key #[n,e], where n is a product of two primes #with D1 digits. Try: #MakeRSAkey(100); MakeRSAkey:=proc(D1) local n,m,S,e,p,q,d: p:=NextPrime1(rand(10^(D1-1)..10^D1-1)()): q:=NextPrime1(rand(10^(D1-1)..10^D1-1)()): n:=p*q: m:=(p-1)*(q-1): S:=rand(m/2..m-1)(): for e from S to m-1 while igcd(e,m)<>1 do od: d:=e&^(-1) mod m: [n,e,d]: end: RSAe:=proc(M,n,e) ME1(M,e,n):end: #CR2(p,q):Chinese remainder CR2:=proc(p,q) local i: {seq([i mod p, i mod q],i=1..p*q)}: end: #EA1(n,m): The quotient and remainder of n/m EA1:=proc(n,m) [trunc(n/m),n-m*trunc(n/m)]:end: #EA(n,m): The gcd of n and m where n>m EA:=proc(n,m) if m=0 then n: else EA(m,EA1(n,m)[2]): fi: end: #EEA(n,m,A,B): The gcd of n and m where n>m EEA:=proc(n,m,A,B) local q,r: end: