Advertisement
MGakowski

RSA lite.py

Dec 31st, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2. import math
  3. from random import randint
  4.  
  5. numbertotest = int()
  6. primefound = 0
  7. lamdaN = int()
  8. e = int()
  9.  
  10.  
  11. def test_prime(a):
  12.     b = round(math.sqrt(a))
  13.     while b != 1:
  14.         if a % b == 0:
  15.             print(str(a) + " is not prime. Divides by " + str(int(b)))
  16.             break
  17.         else:
  18.             b -= 1
  19.     else:
  20.         print(str(a) + " confirmed as prime.")
  21.  
  22.  
  23. def compute_lcm(x, y):
  24.     if x > y:
  25.         greater = x
  26.     else:
  27.         greater = y
  28.     while True:
  29.         if (greater % x == 0) and (greater % y == 0):
  30.             lcm = greater
  31.             break
  32.         greater += 1
  33.     return lcm
  34.  
  35.  
  36. def compute_e():
  37.     divisor = int(round(math.sqrt(numbertotest)))
  38.     while divisor != 1:
  39.         if numbertotest % divisor == 0:
  40.             break
  41.         else:
  42.             divisor -= 1
  43.     else:
  44.         print(str(numbertotest) + " is prime and =e")
  45.         global e
  46.         e = int(numbertotest)
  47.         global primefound
  48.         primefound = int(numbertotest)
  49.  
  50.  
  51. def modInverse(e, lamdaN):
  52.     e = e % lamdaN;
  53.     for q in range(1, lamdaN):
  54.         if ((e * q) % lamdaN == 1):
  55.             return q
  56.     return 1
  57.  
  58.  
  59. # Accept message and confirm primes
  60. m = int(input("Message to encrypt: (Digits only)"))
  61. p = int(input("First prime: "))
  62. test_prime(p)
  63. q = int(input("Second prime: "))
  64. test_prime(q)
  65.  
  66. # Generate public key
  67. n = p * q  # Second half of public key
  68. lamdaN = int(compute_lcm(int(p - 1), int(q - 1)))
  69. while primefound == 0:
  70.     generaterandomnumber = int(randint(1, int(lamdaN)))
  71.     numbertotest = generaterandomnumber
  72.     compute_e()
  73. print("Public key is e=" + str(e) + ", n=" + str(n))
  74.  
  75. # Generate private key (D, N)
  76. d = int(modInverse(e, lamdaN))
  77. print("Private key is d=" + str(d) + ", n=" + str(n))
  78.  
  79. # Encrypt message as CipherText
  80. mtoe = m ** e
  81. c = mtoe % n
  82. print("Ciphertext=" + str(c))
  83.  
  84. # Decrypt ciphertext
  85. ctod = c ** d
  86. dec = ctod % n
  87. print("Decrypted ciphertext " + str(dec))
  88.  
  89. # Print Steps:
  90. print("")
  91. print("Steps and Formulas involved:")
  92. print("Public Key Generation:")
  93. print("p = " + str(p))
  94. print("q = " + str(q))
  95. print("n = p*q")
  96. print("n = " + str(n))
  97. print("Lamda" + "(n) = lcm (p − 1, q − 1)")
  98. print("Lamda" + "(" + str(n) + ") = lowest common multiple (" + str(p) + " − 1, " + str(q) + " − 1)")
  99. print("Lamda" + "(" + str(n) + ") = " + str(lamdaN))
  100. print("e = prime(rand(1, " + "Lamda" + "(n)))")
  101. print("e = prime(rand(1, " + str(n) + "))")
  102. print("e = " + str(e))
  103. print("Public Key = e, n")
  104. print("Public Key = " + str(e) + ", " + str(n))
  105. print("")
  106. print("Message Encryption:")
  107. print("m = message/plaintext")
  108. print("m = " + str(m))
  109. print("c = m^e mod n")
  110. print("c = " + str(m) + "^" + str(e) + " mod " + str(n))
  111. print("c = " + str(mtoe) + " mod " + str(n))
  112. print("c = " + str(c))
  113. print("")
  114. print("Private Key Generation:")
  115. print("d = e modular inverse " + "Lamda" + "(n)")
  116. print("d = " + str(e) + " modular inverse " + "Lamda" + "(" + str(n) + ")")
  117. print("d = " + str(e) + " modular inverse " + str(lamdaN))
  118. print("d = " + str(d))
  119. print("Private Key = d, n")
  120. print("Private Key = " + str(d) + ", " + str(n))
  121. print("")
  122. print("Message Decryption:")
  123. print("m = c^d mod n")
  124. print("m = " + str(c) + "^" + str(d) + " mod " + str(n))
  125. print("m = " + str(dec))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement