2017-01-15 3 views
0

RSA https://www.dlitz.net/software/pycrypto/api/current/Crypto.PublicKey.RSA-module.html에 대한 PyCrypto이 문서를 사용 .PEM 파일에서 RSA 키를 읽는 것은 우리가 쓰고 .PEM과에서 키를 읽을 생각하는 방법이 언급 :ValueError를 ImportKey

from Crypto.PublicKey import RSA 

key = RSA.generate(2048) 
f = open('mykey.pem','w') 
f.write(RSA.exportKey('PEM')) 
f.close() 
... 
f = open('mykey.pem','r') 
key = RSA.importKey(f.read()) 

을 그리고 이것은 어떻게 나는 그것을했다.

random_generator = Random.new().read 
rsakey = RSA.generate(1024, random_generator) 
f = open(email + '.pem', 'w') 
cipher = PKCS1_OAEP.new(rsakey.publickey()) 
f.write(str(rsakey.exportKey("PEM"))) 
f.write(str(rsakey.publickey().exportKey("PEM"))) 
... 
f = open(receiver + '.pem', 'r') 
key = RSA.importKey(f.read()) 
pubkey = key.publickey() 
f.close() 

하지만이 오류가 반환

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Crypto/PublicKey/RSA.py", line 682, in importKey 
    raise ValueError("RSA key format is not supported") 
ValueError: RSA key format is not supported 

답변

1

"WB"모드에 문제

random_generator = Random.new().read 
rsakey = RSA.generate(1024, random_generator) 
f = open(email + '.pem', 'wb') 
cipher = PKCS1_OAEP.new(rsakey.publickey()) 
f.write(str(rsakey.exportKey("PEM"))) 
f.write(str(rsakey.publickey().exportKey("PEM"))) 
... 
f = open(receiver + '.pem', 'r') 
key = RSA.importKey(f.read()) 
pubkey = key.publickey() 
f.close() 
를 열쇠 해결 작성을