Created
February 6, 2014 18:27
-
-
Save shantanoo/8849792 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import argparse | |
from Crypto.PublicKey import RSA | |
from Crypto import Random | |
import sys | |
def createKeys(bits=2048): | |
rnd = Random.new() | |
RSAkey = RSA.generate(bits, rnd.read) | |
passphrase = rnd.read(128) | |
privateKey = RSAkey.exportKey('PEM', passphrase=passphrase) | |
publicKey = RSAkey.publickey().exportKey('PEM', passphrase=passphrase) | |
return privateKey.decode('utf-8'), publicKey.decode('utf-8') | |
def parseargs(a): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--bits', type=int, default=2048) | |
parser.add_argument('--file') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
params = parseargs(sys.argv) | |
pri, pub = createKeys(params.bits) | |
try: | |
open(params.file, 'w', encoding='utf-8').write(pri + '\n') | |
open(params.file + '.pub', 'w', encoding='utf-8').write(pub + '\n') | |
except Exception as e: | |
print(e) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment