########################################################################## # # RSA Encryption Algorithm # # (Miguel A. Lerma, 8/15/95) # # This file contains a function to compute large random primes, and two # functions to convert a string of text into a number and vice versa. # ########################################################################## # Load Number Theory library. with(numtheory): # This function generates an n-digit random prime number by the # probabilistic Solovay and Strassen's method. large_random_prime := proc(n_digits) local m1,m2,a,b,n: m2 := (10^n_digits)/2: m1 := m2/10+1: is_prime := `false`: while(not is_prime) do b := 2*rand(m1..m2)()-1: is_prime := `true`: for n from 1 to 100 do a := rand(1..b-1)(): if gcd(a,b) <> 1 or (J(a,b) - a&^((b-1)/2)) mod b <> 0 then is_prime := `false`: break: fi: od: od: RETURN(b): end: # Next function transforms a string into a number. text_to_number := proc(T1) local k1,M1,l1: M1 := 0: for k1 to length(T1) do l1 := substring(T1,k1..k1): if l1 = '_' then d1:=10 elif l1 = 'a' then d1 := 11 elif l1 = 'b' then d1 := 12 elif l1 = 'c' then d1 := 13 elif l1 = 'd' then d1 := 14 elif l1 = 'e' then d1 := 15 elif l1 = 'f' then d1 := 16 elif l1 = 'g' then d1 := 17 elif l1 = 'h' then d1 := 18 elif l1 = 'i' then d1 := 19 elif l1 = 'j' then d1 := 20 elif l1 = 'k' then d1 := 21 elif l1 = 'l' then d1 := 22 elif l1 = 'm' then d1 := 23 elif l1 = 'n' then d1 := 24 elif l1 = 'o' then d1 := 25 elif l1 = 'p' then d1 := 26 elif l1 = 'q' then d1 := 27 elif l1 = 'r' then d1 := 28 elif l1 = 's' then d1 := 29 elif l1 = 't' then d1 := 30 elif l1 = 'u' then d1 := 31 elif l1 = 'v' then d1 := 32 elif l1 = 'w' then d1 := 33 elif l1 = 'x' then d1 := 34 elif l1 = 'y' then d1 := 35 elif l1 = 'z' then d1 := 36 else ERROR(`Use only lower case letters or underscore.`) fi: M1 := 100*M1+d1: od: M1: end: # List of letters used to transform a number into text. L1 := ['_',''a'',''b'',''c'',''d'',''e'',''f'',''g'',''h'',''i'',''j'', ''k'',''l'',''m'',''n'',''o'',''p'',''q'',''r'',''s'',''t'',''u'',''v'', ''w'',''x'',''y'',''z'']: # Next function trasforms a number into a string of text. number_to_text := proc(M1) local T1, r1, M2: M2 := M1: T1 := ``: while(M2 > 0) do M2 := iquo(M2,100,'r1'): T1 := cat(L1[r1-9],T1): od: T1: end: