HAVE TO USE PYTHON TO GET IT DONE Asymmetric encryption has been broadly used fo
ID: 3788650 • Letter: H
Question
HAVE TO USE PYTHON TO GET IT DONE
Asymmetric encryption has been broadly used for information security in modern society. Implement and test a prototype for RSA, an asymmetric encryption system. The program should: 1. Generate two large pseudo-prime numbers;
2. Use the two numbers to form a public encryption key and the private decryption key;
3. Create a main program that allows a user to key in a message in plaintext and encrypt it with public key; and decrypt a ciphered-text to plaintext with private key.
Explanation / Answer
import random
'''
Euclid's algorithm for determining the greatest common divisor
Use iteration to make it faster for larger integers
'''
def rsa_gcd(a, b):
while b != 0:
a, b = b, a % b
return a
'''
Euclid's extended algorithm for finding the multiplicative inverse of two numbers
'''
def mul_inverse(e, phi):
d = 0
x1 = 0
x2 = 1
y1 = 1
temps_phi = phi
while e > 0:
temps_var1 = temps_phi/e
temps_var2 = temps_phi - temps_var1 * e
temps_phi = e
e = temps_var2
x = x2- temp1* x1
y = d - temps_var1 * y1
x2 = x1
x1 = x
d = y1
y1 = y
if temps_phi == 1:
return d + phi
'''
Tests to see if a number is prime.
'''
def isit_primenum(num):
if num == 2:
return True
if num < 2 or num % 2 == 0:
return False
for n in xrange(3, int(num**0.5)+2, 2):
if num % n == 0:
return False
return True
def generate_keypair(p, q):
if not (isit_primenum(p) and isit_primenum(q)):
raise ValueError('Both numbers must be prime.')
elif p == q:
raise ValueError('p and q cannot be equal')
#n = pq
n = p * q
#Phi is the totient of n
phi = (p-1) * (q-1)
#Choose an integer e such that e and phi(n) are coprime
e = random.randrange(1, phi)
#Use Euclid's Algorithm to verify that e and phi(n) are comprime
g = rsa_gcd(e, phi)
while g != 1:
e = random.randrange(1, phi)
g = rsa_gcd(e, phi)
#Use Extended Euclid's Algorithm to generate the private key
d = mul_inverse(e, phi)
#Return public and private keypair
#Public key is (e, n) and private key is (d, n)
return ((e, n), (d, n))
def rsa_encrypt(pk, plaintext):
#Unpack the key into it's components
key, n = pk
#Convert each letter in the plaintext to numbers based on the character using a^b mod m
cipher = [(ord(char) ** key) % n for char in plaintext]
#Return the array of bytes
return cipher
def rsa_decrypt(pk, ciphertext):
#Unpack the key into its components
key, n = pk
#Generate the plaintext based on the ciphertext and key using a^b mod m
plain = [chr((char ** key) % n) for char in ciphertext]
#Return the array of bytes as a string
return ''.join(plain)
if __name__ == '__main__':
'''
Detect if the script is being run directly by the user
'''
print ("RSA Encrypter/ Decrypter")
p = int(input("Enter a prime number (17, 19, 23, etc): "))
q = int(input("Enter another prime number (Not one you entered above): "))
print ("Generating your public/private keypairs now . . .")
public, private = generate_keypair(p, q)
print ("Your public key is ", public ," and your private key is ", private)
message = input("Enter a message to rsa_encrypt with your private key: ")
encrypted_msg = rsa_encrypt(private, message)
print ("Your encrypted message is: ")
print (''.join(map(lambda x: str(x), encrypted_msg)))
print ("Decrypting message with public key ", public ," . . .")
print ("Your message is:")
print (rsa_decrypt(public, encrypted_msg))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.