i use wb to write pubic and private key and succeed to import private, but after decrypt, it return hex number not a clear text
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() 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 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 >>> encryption_key 'o\x94\xaeC\xe0S\x81\x05t\xd8\\A\x10?\xd2\xe5\x8c5\xc9\x1d\x14\xc7\xfd)Cs\x8b"cg\x16y\xe2\xf2L\xf1-\x08qHt\x99\xbc\xb5\xf6_\x17c\xd2&Z\x0b\xc5t\t\xe0\x8b\x03G\x10\xce\xd6\xcd\x86\xfc!\xc9i\xa2\xab\x9d\x8a\x92\xfc7 g\xa5$\x91\x85\xa2L]I\xd6\xc6\xaez\xed\x01\x95\xee)8z\x18\xc9aag\x97\x97\xb0\\)\xec"\xe4\xbez\xd3\xa8\'k%\x12\x1d\xf9\xf0\x0e\x0c\xcb\xa8\xb1\xe7}\x90\xd3\xcfs@\xc2m\x1a^\x1b0\xa7\xdd\xcd\xea\x1f\xd5\x08\x13+y"]vu\xe3\x9e\xba\x97\x10\x90S\xea\xae1=r4Yp,\xe3\xa9\xc66H\xa7\x95[M|n\x91\x98\x9c,\xc4\xf5\x7f\x8cJ\x03\xba\x04Z0lV\xe1\xd6d\xeec@\xe1\xa0\xec\x81]\xef5\r\x12\x88\xbe/\xfc\xe01\xaacn,\x8a\xe1\x14\x8a\xf4\xd85\xd8\xabD\x137\xe7T\xc4\xc1\x84b.\xd9RZ\x0e\x03#\x1e\x8dl\xe8\xe4N^\r\xf0\x1d\x8c' 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