Last active
August 29, 2015 14:19
-
-
Save theomessin/67f0465125c4f84904d2 to your computer and use it in GitHub Desktop.
This python program will allow you to see how RSA works!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import math | |
import Queue | |
def isPrime(n): | |
if n == 2 or n == 3: return True | |
if n < 2 or n%2 == 0: return False | |
if n < 9: return True | |
if n%3 == 0: return False | |
r = int(n**0.5) | |
f = 5 | |
while f <= r: | |
if n%f == 0: return False | |
if n%(f+2) == 0: return False | |
f +=6 | |
return True | |
def gcd (m, n): | |
while (m != n): | |
if (m > n): | |
m = m - n | |
else: | |
n = n - m | |
return m | |
def isCoPrime (a, b): | |
if (gcd(a, b) != 1): | |
return True | |
else: | |
return False | |
def extended_gcd(aa, bb): | |
lastremainder, remainder = abs(aa), abs(bb) | |
x, lastx, y, lasty = 0, 1, 1, 0 | |
while remainder: | |
lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder) | |
x, lastx = lastx - quotient*x, x | |
y, lasty = lasty - quotient*y, y | |
return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1) | |
def modInverse(m, a): | |
g, x, y = extended_gcd(a, m) | |
if g != 1: | |
raise ValueError | |
return x % m | |
def encrypt(m): | |
c=(m**myE) % myN | |
return c | |
def decrypt(c): | |
m=(c**myD) % myN | |
return m | |
print ("RSA Tester by Theodore Messinezis") | |
print ("Written in Python. This code is open-source!") | |
print ("This program will allow you to test the crypto algorithm created by Rivest, Shamir, and Adleman (RSA).") | |
print ("This version of RSA uses 1 byte / 8 bit prime random numbers.") | |
raw_input('Press <Enter> to run the RSA crypto algorithm\n-----------------------------------------------------------------------------------------------------\n') | |
while True: | |
primeA = random.randint(128,255) | |
if isPrime(primeA ): | |
break | |
print ("First 8 bit prime found:\n " + str(primeA) + " | " + str("{0:b}".format(primeA))) | |
while True: | |
primeB = random.randint(128,255) | |
if primeA!=primeB and isPrime(primeB): | |
break | |
print ("Second 8 bit prime found:\n " + str(primeB) + " | " + str("{0:b}".format(primeB))) | |
myN = primeA * primeB | |
print ("N = " + str(primeA) + " * " + str(primeB) +" =\n " + str(myN) + " | " + str("{0:b}".format(myN))) | |
myF = (primeA-1) * (primeB-1) | |
print ("f(" + str(myN) + ") = "+ str(primeA-1) + " * " + str(primeB-1) + " :\n " + str(myF) + " | " + str("{0:b}".format(myF))) | |
while True: | |
myE = random.randint(2,myF-1) | |
if isPrime(myE) and (not isCoPrime(myE, myF)): | |
break | |
print ("Non co-prime to f(N) and prime found E:\n " + str(myE) + " | " + str("{0:b}".format(myE))) | |
myD = modInverse(myF,myE) | |
print ("Modular multiplicative inverse found to be D:\n " + str(myD) + " | " + str("{0:b}".format(myD))) | |
messageNew = "Test Message" | |
ASCIIString = "" | |
for myChar in messageNew: | |
ASCIIString += str(ord(myChar)) + ", " | |
ASCIIString = ASCIIString[:-2] | |
print ("To encrypt message '" + messageNew + "' with public key = (N,E):\n Message '" + messageNew + "' to numbers = " + ASCIIString) | |
CipherString = "" | |
q = Queue.Queue() | |
for myChar in messageNew: | |
cipher=encrypt(ord(myChar)) | |
CipherString += str(cipher) + ", " | |
q.put(cipher) | |
CipherString = CipherString[:-2] | |
print (" Cipher is now: " + CipherString) | |
print ("\nTo decrypt cipher private key = (D)") | |
newMessageString = "" | |
myMessageNew = "" | |
for myChar in messageNew: | |
myCipher = q.get() | |
message=decrypt(myCipher) | |
newMessageString += str(message) + ", " | |
myMessageNew+=chr(message) | |
newMessageString = newMessageString[:-2] | |
print (" Message is now: " + newMessageString) | |
print (" Translated with ASCII: " + myMessageNew) | |
print ("\n-----------------------------------------------------------------------------------------------------\n") | |
messageNew = raw_input("Enter your message to encrypt: ") | |
ASCIIString = "" | |
for myChar in messageNew: | |
ASCIIString += str(ord(myChar)) + ", " | |
ASCIIString = ASCIIString[:-2] | |
print ("To encrypt message '" + messageNew + "' with public key = (N,E):\n Message '" + messageNew + "' to numbers = " + ASCIIString) | |
CipherString = "" | |
q = Queue.Queue() | |
for myChar in messageNew: | |
cipher=encrypt(ord(myChar)) | |
CipherString += str(cipher) + ", " | |
q.put(cipher) | |
CipherString = CipherString[:-2] | |
print (" Cipher is now: " + CipherString) | |
print ("\nTo decrypt cipher private key = (D)") | |
newMessageString = "" | |
myMessageNew = "" | |
for myChar in messageNew: | |
myCipher = q.get() | |
message=decrypt(myCipher) | |
newMessageString += str(message) + ", " | |
myMessageNew+=chr(message) | |
newMessageString = newMessageString[:-2] | |
print (" Message is now: " + newMessageString) | |
print (" Translated with ASCII: " + myMessageNew) | |
print ("\n-----------------------------------------------------------------------------------------------------") | |
print("Press <Enter> to quit") | |
raw_input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment