Advertisement
MarkUa

Untitled

Nov 4th, 2019
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. import random
  2. import math
  3. import time
  4.  
  5. def gcd(a, b):
  6.     while b != 0:
  7.         a, b = b, a % b
  8.     return a
  9.  
  10.  
  11. def is_prime(num):
  12.     if num == 2:
  13.         return True
  14.     if num < 2 or num % 2 == 0:
  15.         return False
  16.     for n in range(3, int(num ** 0.5) + 2, 2):
  17.         if num % n == 0:
  18.             return False
  19.     return True
  20.  
  21. def get_d(e,phi):
  22.     d = 1.2
  23.     k = 1
  24.     while math.floor(d) != math.ceil(d):
  25.         d = (k * phi + 1) / e
  26.         k = k + 1
  27.     return int(d)
  28.  
  29.  
  30. def generate_keypair(p, q):
  31.     if not (is_prime(p) and is_prime(q)):
  32.         raise ValueError('Both numbers must be prime.')
  33.     elif p == q:
  34.         raise ValueError('p and q cannot be equal')
  35.     n = p * q
  36.     phi = (p - 1) * (q - 1)
  37.  
  38.     e = random.randrange(1, phi)
  39.  
  40.     g = gcd(e, phi)
  41.     while g != 1:
  42.         e = random.randrange(1, phi)
  43.         g = gcd(e, phi)
  44.     d = get_d(e,phi)
  45.     print("d = {} e = {} n = {}".format(d,e,n))
  46.     return ((e, n), (d, n))
  47.  
  48.  
  49. def encrypt(pk, plaintext):
  50.     # Unpack the key into it's components
  51.     key, n = pk
  52.  
  53.     b = [(char.encode("cp1251")) for char in plaintext]
  54.  
  55.     с = [ord(i) for i in b]
  56.  
  57.     return [(char ** key) % n for char in с]
  58.  
  59.  
  60. def decrypt(pk, ciphertext):
  61.     # Unpack the key into its components
  62.     key, n = pk
  63.     z = [ (i ** key) % n for i in ciphertext]
  64.     return bytes(z).decode('cp1251')
  65.  
  66. if __name__ == '__main__':
  67.     '''
  68.    Detect if the script is being run directly by the user
  69.    '''
  70.     print("RSA Encrypter/ Decrypter")
  71.     start_time = time.time()
  72.     p = 17 # int(input("Enter a prime number (17, 19, 23, etc): "))
  73.     q = 37 #int(input("Enter another prime number (Not one you entered above): "))
  74.     print("p = {} q = {}".format(p,q))
  75.     print("Generating your public/private keypairs now . . .")
  76.     public, private = generate_keypair(p, q)
  77.     print("Your public key is ", public, " and your private key is ", private)
  78.     message =  "390 1037 583 1354 987" #input("Enter a message to encrypt with your private key: ")
  79.     print("text to encript '{}'".format(message))
  80.     encrypted_msg = encrypt(public, message)
  81.  
  82.     print("Your encrypted message is: ")
  83.     print(''.join(map(lambda x: str(x), encrypted_msg)))
  84.     print("Decrypting message with public key ", public, " . . .")
  85.     print("Your message is:")
  86.     s = decrypt(private, encrypted_msg)
  87.     print( s)
  88.     time_elapsed = time.time() - start_time
  89.  
  90.     print('Time of RSA5 %.9f' % (time_elapsed))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement