# determine if a positive integer is prime # list of known primes, will be extended whenever new primes are found. KP = [2,3,5,7,11,13,17,19] def bin(x,A): # binary search for x in sorted array A ax = False min,max = 0,len(A)-1 while (min<=max and ax==False): i = (min+max)/2 if A[i]==x: ax=True else: if xA[i]: i+=1 # end while loop A.insert(i,x) #sinsert def isprime(n): # determines if n is a prime number if bin(n,KP): return True # known prime if nKP[len(KP)-1]): KP.append(n) # bigger than all known primes else: sinsert(n,KP) return ax # # find all prime factors of a number n (assume n>=0) def primefactors(n): if isprime(n): return [n] F = [] # array to be returned if (n%2==0): F.append(2) i = 3 while i <= n/2: if isprime(i) and n%i==0: F.append(i) i += 2 # return F # ############ RSA encrpytion: pick two primes p and q, let n=p*q. let # e and d be such that (e*d) % ((p-1)*(q-1)) == 1. Then e is the # public encryption key while d is the private decryption key. # example: p,q = 73,67 e,d = 53,269 n = p*q # convert string to array of ascii vals def ords(s): A = [0]*len(s) i = 0 while i