error when import private key in python

Ho Yeung Lee jobmattcon at gmail.com
Sun Jun 4 07:22:23 EDT 2017


#step 1
from Crypto.PublicKey import RSA
keypair = RSA.generate(2048)
alice_privkey = keypair.exportKey('PEM', 'mysecret', pkcs=1)
#alice_privkey = keypair.exportKey()
alice_pubkey = keypair.publickey().exportKey()

text_file = open("alice_pubkey.txt", "w")
text_file.write(alice_pubkey)
text_file.close()

keypair = RSA.generate(2048)
bob_privkey = keypair.exportKey('PEM', 'mysecret2', pkcs=1)
#bob_privkey = keypair.exportKey()
bob_pubkey = keypair.publickey().exportKey()

text_file = open("bob_pubkey.txt", "w")
text_file.write(bob_pubkey)
text_file.close()

text_file = open("alice_privkey.pem", "w")
text_file.write(alice_privkey)
text_file.close()
text_file = open("bob_privkey.pem", "w")
text_file.write(bob_privkey)
text_file.close()

#step 2
#use alice public key to encrypt
pubkey = RSA.importKey(alice_pubkey)
alice_masterkey = pubkey.encrypt("i am Martin", None)

text_file = open("alice_masterkey.txt", "w")
text_file.write(bob_pubkey)
text_file.close()

#step 3
quit()
text_file = open("alice_masterkey.txt", "r")
alice_masterkey=text_file.read().replace('\n', '')
text_file.close()
text_file = open("alice_privkey.pem", "r")
alice_privkey=text_file.read().replace('\n', '')
text_file.close()

privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
encryption_key = privkey.decrypt(alice_masterkey)

>>> privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\Crypto\PublicKey\RSA.py", line 638, in importKey
    if lines[1].startswith(b('Proc-Type:4,ENCRYPTED')):
IndexError: list index out of range



More information about the Python-list mailing list