Skip to content

Instantly share code, notes, and snippets.

@Goblin80
Created April 11, 2019 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goblin80/9af5e8caf3e5f2f664c17730aacd32b7 to your computer and use it in GitHub Desktop.
Save Goblin80/9af5e8caf3e5f2f664c17730aacd32b7 to your computer and use it in GitHub Desktop.
implementation of RSA public-key cryptosystem in python
import argparse
class RSA:
def __init__(self, p, q, e):
self.p, self.q, self.e = p, q, e
self.n = p * q
self.phi = (p - 1) * (q - 1)
self.d = e ** (self.phi - 1) % self.phi
def encrypt(self, m):
return m ** self.e % self.n
def decrypt(self, c):
return c ** self.d % self.n
parser = argparse.ArgumentParser(prog='RSA', description='RSA public-key cryptosystem implementation')
parser.add_argument('-p', help='Large Prime p', type=int, required=True)
parser.add_argument('-q', help='Large Prime q', type=int, required=True)
parser.add_argument('-e', help='Exponent where 1 < E < P * Q', type=int, required=True)
parser.add_argument('-k', help='Encrypt or Decrypt?', choices=['encrypt', 'decrypt'], required=True)
parser.add_argument('-x', help='Payload (to encrypt or decrypt)', type=int, required=True)
args = parser.parse_args()
rsa = RSA(args.p, args.q, args.e)
r = {'enc': rsa.encrypt(args.x),
'dec': rsa.decrypt(args.x)}
print(r[args.k])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment