Skip to content

Instantly share code, notes, and snippets.

@cosu
Created November 12, 2012 10:57
Show Gist options
  • Save cosu/4058678 to your computer and use it in GitHub Desktop.
Save cosu/4058678 to your computer and use it in GitHub Desktop.
Toy script to create a RSA key from scratch
__author__ = 'cdumitru'
from Crypto.PublicKey import RSA
from Crypto import Random
import gmpy
import base64
#tup (tuple) - A tuple of long integers, with at least 2 and no more than 6 items. The items come in the following order:
#RSA modulus (n).
#Public exponent (e).
#Private exponent (d)
#First factor of n (p).
#Second factor of n (q)
n = 0L
p = 0L
q = 0L
e = 65537L
string_to_be_encrypted="Hello"
phi = (p - 1) * (q - 1)
d = long(gmpy.invert(e, phi))
tup = (n ,e, d, p, q )
key = RSA.construct(tup)
random_generator = Random.new().read
data = key.encrypt(string_to_be_encrypted,random_generator)
print key.exportKey('PEM')
print key.publickey().exportKey()
print base64.b64encode(data[0])
@hasan2001jk
Copy link

How does it work
????

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