Created
February 14, 2012 15:56
-
-
Save crankycoder/1827731 to your computer and use it in GitHub Desktop.
Jython code to generation DSA/RSA keys
This file contains 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
""" | |
Simple stupid utility to create RSA or DSA key keys | |
Just add the JSCH library to your classpath and things ought to work. | |
(JY25)~/Downloads > export CLASSPATH=./jsch-0.1.42.jar | |
(JY25)~/Downloads > jython | |
*sys-package-mgr*: processing new jar, '/Users/victorng/Downloads/jsch-0.1.42.jar' | |
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06) | |
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_29 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> from com.jcraft.jsch import KeyPair, JSch | |
>>> JSch | |
<type 'com.jcraft.jsch.JSch'> | |
>>> | |
""" | |
import argparse | |
from com.jcraft.jsch import KeyPair, JSch | |
import sys | |
def parse_args(): | |
parser = argparse.ArgumentParser(description="Generate public/private keys") | |
parser.add_argument('-crypt', | |
choices=('rsa', 'dsa'), | |
required=True, | |
help="Crypto algorithm to use") | |
parser.add_argument('-o', | |
type=argparse.FileType('w'), | |
required=True, | |
help="Output file") | |
parser.add_argument('-m', | |
type=str, | |
required=True, | |
help="Key comment") | |
return parser.parse_args() | |
def error_out(): | |
print """ | |
usage: java KeyGen -crypt rsa -o output_keyfile -m comment | |
java KeyGen -crypt dsa -o output_keyfile -m comment | |
""" | |
sys.exit(-1) | |
if __name__ == '__main__': | |
args = parse_args() | |
typemap = {'rsa': KeyPair.RSA, | |
'dsa': KeyPair.DSA,} | |
keytype = typemap[args.crypt] | |
filename = args.o.name | |
args.o.close() | |
comment = args.m | |
jsch = JSch() | |
passphrase = raw_input("Enter passphrase: ").strip() | |
kpair = KeyPair.genKeyPair(jsch, keytype) | |
kpair.setPassphrase(passphrase) | |
kpair.writePrivateKey(filename) | |
kpair.writePublicKey(filename+".pub", comment) | |
print "Finger print: %s" % kpair.getFingerPrint() | |
kpair.dispose() | |
print "Private key is at: %s" % filename | |
print "Public key is at: %s.pub" % filename | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment