Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import random
  5.  
  6.  
  7. '''
  8. Euclid's algorithm for determining the greatest common divisor
  9. Use iteration to make it faster for larger integers
  10. '''
  11. def gcd(a, b):
  12.     while b != 0:
  13.         a, b = b, a % b
  14.     return a
  15.  
  16. '''
  17. Euclid's extended algorithm for finding the multiplicative inverse of two numbers
  18. '''
  19. def multiplicative_inverse(e, phi):
  20.     d = 0
  21.     x1 = 0
  22.     x2 = 1
  23.     y1 = 1
  24.     temp_phi = phi
  25.  
  26.     while e > 0:
  27.         temp1 = temp_phi/e
  28.         temp2 = temp_phi - temp1 * e
  29.         temp_phi = e
  30.         e = temp2
  31.  
  32.         x = x2- temp1* x1
  33.         y = d - temp1 * y1
  34.  
  35.         x2 = x1
  36.         x1 = x
  37.         d = y1
  38.         y1 = y
  39.  
  40.     if temp_phi == 1:
  41.         return d + phi
  42.  
  43. '''
  44. Tests to see if a number is prime.
  45. '''
  46. def is_prime(num):
  47.     if num == 2:
  48.         return True
  49.     if num < 2 or num % 2 == 0:
  50.         return False
  51.     for n in xrange(3, int(num**0.5)+2, 2):
  52.         if num % n == 0:
  53.             return False
  54.     return True
  55.  
  56. def generate_keypair(p, q):
  57.     if not (is_prime(p) and is_prime(q)):
  58.         raise ValueError('Both numbers must be prime.')
  59.     elif p == q:
  60.         raise ValueError('p and q cannot be equal')
  61.     #n = pq
  62.     n = p * q
  63.  
  64.     #Phi is the totient of n
  65.     phi = (p-1) * (q-1)
  66.  
  67.     #Choose an integer e such that e and phi(n) are coprime
  68.     e = random.randrange(1, phi)
  69.  
  70.     #Use Euclid's Algorithm to verify that e and phi(n) are comprime
  71.     g = gcd(e, phi)
  72.     while g != 1:
  73.         e = random.randrange(1, phi)
  74.         g = gcd(e, phi)
  75.  
  76.     #Use Extended Euclid's Algorithm to generate the private key
  77.     d = multiplicative_inverse(e, phi)
  78.  
  79.     #Return public and private keypair
  80.     #Public key is (e, n) and private key is (d, n)
  81.     return ((e, n), (d, n))
  82.  
  83. def encrypt(pk, plaintext):
  84.     #Unpack the key into it's components
  85.     key, n = pk
  86.     #Convert each letter in the plaintext to numbers based on the character using a^b mod m
  87.     cipher = [(ord(char) ** key) % n for char in plaintext]
  88.     #Return the array of bytes
  89.     return cipher
  90.  
  91. def decrypt(pk, ciphertext):
  92.     #Unpack the key into its components
  93.     key, n = pk
  94.     #Generate the plaintext based on the ciphertext and key using a^b mod m
  95.     plain = [chr((char ** key) % n) for char in ciphertext]
  96.     #Return the array of bytes as a string
  97.     return ''.join(plain)
  98.  
  99.  
  100. if __name__ == '__main__':
  101.     '''
  102.    Detect if the script is being run directly by the user
  103.    '''
  104.     print "RSA Encrypter/ Decrypter"
  105.     p = int(raw_input("Enter a prime number (17, 19, 23, etc): "))
  106.     q = int(raw_input("Enter another prime number (Not one you entered above): "))
  107.     print "Generating your public/private keypairs now . . ."
  108.     public, private = generate_keypair(p, q)
  109.     print "Your public key is ", public ," and your private key is ", private
  110.     message = raw_input("Enter a message to encrypt with your private key: ")
  111.     encrypted_msg = encrypt(private, message)
  112.     print "Your encrypted message is: "
  113.     print ''.join(map(lambda x: str(x), encrypted_msg))
  114.     print "Decrypting message with public key ", public ," . . ."
  115.     print "Your message is:"
  116.     print decrypt(public, encrypted_msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement