#sage users do not need this line import sage.all as sg #sage users do not need the sg prefix p = sg.random_prime(100,lbound=15) q = sg.random_prime(100,lbound=15) [p,q] #secret...sh! n = p*q #making the modulus n n #public -- anyone can know this! phi_n = sg.euler_phi(n) phi_n #secret! my_public_exponent = sg.randint(10,phi_n) while sg.gcd(my_public_exponent,phi_n) != 1: my_public_exponent = sg.randint(10,phi_n-1) my_public_exponent #public (duh) my_private_exponent = sg.inverse_mod(my_public_exponent,phi_n) my_private_exponent #secret! message = sg.randint(0,n-1) #the message can be anything message #secret ciphertext = sg.power_mod(message,my_public_exponent,n) ciphertext #public decoded_ciphertext = sg.power_mod(ciphertext,my_private_exponent,n) decoded_ciphertext