Created
November 12, 2012 10:57
-
-
Save cosu/4058678 to your computer and use it in GitHub Desktop.
Toy script to create a RSA key from scratch
This file contains hidden or 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
__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]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How does it work
????