0

I'm fairly new to python but have basic understanding of crypto modules. However, I'm trying to import an RSA key pair from a remote machine(i.e. Safenet) to run some performance tests on signing data. I'm building the framework in Python and I haven't found many clear examples. For instance:

from Crypto.PublicKey import RSA
f = open('path/to/pair/00000103000003A2','r')
r = RSA.importKey(f.read(),  passphrase='123456') # Index out of range error
print(r)

This is about as far as I'm getting with opening the key pair. I can import the key pair to my personal computer so it's in a directory I have access to.

DJ2
  • 1,721
  • 3
  • 34
  • 74
  • Can you show an example key? – Artjom B. Jun 09 '16 at 13:31
  • This would be the path to my keystore. `C:\Users\Name\Company\00000103000003A2.p12` @Artjom B – DJ2 Jun 09 '16 at 13:39
  • PKCS#12 is not supported by pycrypto. You have to convert it first – Artjom B. Jun 09 '16 at 13:42
  • Does this conversion need to be made using openssh from the command line or within the python code? And what version of pkcs does python support? – DJ2 Jun 09 '16 at 14:18
  • Since pyCrypto doesn't support PKCS#12, you can't use that. So you have to find other means or use a different library. Though, pyCrypto supports OpenSSH file format, but I'm not sure if it's only for public keys or private key too. – Artjom B. Jun 09 '16 at 14:29

1 Answers1

0

I have found the answer elsewhere.

from OpenSSL import crypto

passwd = 'The password of your created .p12 file'
p12 = crypto.load_pkcs12(open("Path to p12 file.p12 ",'rb').read(),passwd)
print(p12.get_certificate())    #Prints object location
print(p12.get_privatekey())     #Prints object location
p12.get_ca_certificates()

Here's a link to convert object location to PEM formatted strings. PyOpenSSL convert certificate object to .pem file

Community
  • 1
  • 1
DJ2
  • 1,721
  • 3
  • 34
  • 74