Skip to content

Instantly share code, notes, and snippets.

@ItsASine
Created July 4, 2018 18:49
Show Gist options
  • Save ItsASine/aeadc396626e7e707a664e26e7f63216 to your computer and use it in GitHub Desktop.
Save ItsASine/aeadc396626e7e707a664e26e7f63216 to your computer and use it in GitHub Desktop.
Testing out an RSA logic script with super low primes
import sys
sys.setrecursionlimit(100)
def eea(stuff, thing):
if stuff != 0:
(gcd, s, t) = eea(thing % stuff, stuff)
return gcd, t - (thing // stuff) * s, s # // is quotient only division
else:
return thing, 0, 1
def inversemod(number, mod):
(gcd, s, t) = eea(number, mod)
if gcd == 1:
return s % mod
else:
return None # modular inverse can only be found if numbers are coprime
e = 7
p = 11
q = 13
n = p * q
phi = (p - 1) * (q - 1)
def encrypt(message):
return (message ** e) % n
d = inversemod(e, phi)
def decrypt(cipher):
return (cipher ** d) % n
print('n: ' + str(n))
print('phi: ' + str(phi))
print('d: ' + str(d))
print
if e == 7 and p == 11 and q == 13:
print(d == 103)
print
print('public: (' + str(e) + ',' + str(n) + ')')
print('private: (' + str(d) + ',' + str(n) + ')')
@ItsASine
Copy link
Author

ItsASine commented Jul 4, 2018

n: 143
phi: 120
d: 103

True

public: (7,143)
private: (103,143)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment