-
-
Save nifgraup/4108309 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.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