i use "wb" to write public and private key and succeed to import private key but after decrypt, it is not clear text, it is hex number
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", "wb") 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", "wb") text_file.write(bob_pubkey) text_file.close() text_file = open("alice_privkey.pem", "wb") text_file.write(alice_privkey) text_file.close() text_file = open("bob_privkey.pem", "wb") 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() python text_file = open("alice_masterkey.txt", "r") alice_masterkey=text_file.read() text_file.close() text_file = open("alice_privkey.pem", "r") alice_privkey=text_file.read() text_file.close() text_file = open("alice_pubkey.txt", "r") alice_pubkey=text_file.read() text_file.close() from Crypto.PublicKey import RSA pubkey = RSA.importKey(alice_pubkey) privkey = RSA.importKey(alice_privkey,passphrase="mysecret") encryption_key = privkey.decrypt(alice_masterkey) encryption_key ....hex number On Sunday, June 4, 2017 at 7:29:07 PM UTC+8, Thomas Jollans wrote: > On 04/06/17 13:22, Ho Yeung Lee wrote: > > # [snip] > > alice_privkey=text_file.read().replace('\n', '') > > Why are you removing newlines? Does the documentation tell you to do this? > > >>>> 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 > > > > This is just a wild guess, but it looks like the package expects there > to be multiple lines in the key. > > > -- Thomas -- https://mail.python.org/mailman/listinfo/python-list