5

I need to generate publickey from a private key without temporary location locally like we do in sshgen.So i use this.Here iam passing my private key as input like this(while executing):

python codekey.py "-----BEGIN RSA PRIVATE KEY-----\nMIhhhhhhhhhhhhhhhh......Bidqt/YS3/0giWrtv+rMkJtv8n\nmirJ+16SZodI5gMuknvZG....................n-----END RSA PRIVATE KEY-----"

My code (codekey.py):

import sys
import io
from twisted.conch.ssh import keys
k = sys.argv[1]
rsa = keys.RSA.importKey(k)
key = keys.Key(rsa)
ssh_public = key.public().toString("openssh")
print ssh_public

error:

      Traceback (most recent call last):
    File "codekey.py", line 7, in <module>
     rsa = keys.RSA.importKey(k)
    File "/usr/lib/python2.7/dist-packages/Crypto/PublicKey/RSA.py", line                638, in importKey
     if lines[1].startswith(b('Proc-Type:4,ENCRYPTED')):
       IndexError: list index out of range

Dyanamically i need to pass key value as shown above while executing my python script and from that it will generate public key .Whether it is possible ??,i dont need to store locally,since for priveleges and key securities,dont want to hack.

Doefi
  • 79
  • 1
  • 4
  • 10
  • Looking at the source code, it doesn't look like that format of private key is supported. Also, you should be using conch.keys.Key.fromString() to import the key once you get it in an acceptable format. – President James K. Polk Aug 22 '16 at 16:00
  • Thanks James,but where am i missing,i know i have done some mistake in this line rsa = keys.RSA.importKey(k).Since i need to get my private key as input instead of getting in pem file and extract a public key from it.Is it possible????.If we use sshkeygen it storing keys locally .I dont want that for security reasons. – Doefi Aug 23 '16 at 05:44

1 Answers1

8

Here's how you can do it :

If you already have the private key you can basically make a private key object with it and then simply extract the public key from it using as :

public_key = private_key.publickey().exportKey('PEM')

assuming that private_key is your private key object.

In case you do not have this object, one way of obtaining it from the PEM encoded (PKCS#1) private key file (as you have given in your question above) would be like this :

from Crypto.PublicKey import RSA
from base64 import b64decode
pem_key = b'your private key in PEM'
key = b64decode(pem_key)
keyPriv = RSA.importKey(key)
# key now has all the components of the private 
print keyPriv.keydata
modulusN = keyPriv.n
pubExpE = keyPriv.e
priExpD = keyPriv.d
primeP = keyPriv.p
primeQ = keyPriv.q
private_key = RSA.construct((modulusN, pubExpE, priExpD, primeP, primeQ))

and then once you have the private key in the private_key objectdo the :

public_key = private_key.publickey().exportKey('PEM')
qre0ct
  • 5,680
  • 10
  • 50
  • 86
  • If you are having problems to import `from Crypto.PublicKey import RSA` try to install pycrypto with easy_install: `pip uninstall pycrypto easy_install pycrypto` – E235 Aug 24 '17 at 14:57
  • 1
    `keyPriv = RSA.importKey(key)` then `public_key = keyPriv.publickey().exportKey('PEM')` also works.. – pravin Sep 21 '18 at 09:06