Skip to content

Instantly share code, notes, and snippets.

@nifgraup
Forked from cosu/rsakey.py
Created November 19, 2012 00:17
Show Gist options
  • Save nifgraup/4108309 to your computer and use it in GitHub Desktop.
Save nifgraup/4108309 to your computer and use it in GitHub Desktop.
Toy script to create a RSA key from scratch
__author__ = 'cdumitru'
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
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)
p = 0L
q = 0L
n = p * q
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)
cipher = PKCS1_OAEP.new(key)
data = cipher.encrypt(string_to_be_encrypted)
print key.exportKey('PEM')
print key.publickey().exportKey()
print base64.b64encode(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment