2016-07-06 6 views
1

어떻게 문자열에서 load_pub_key 수 있습니다.문자열에서 파이썬 M2Crypto load_pub_key

pub_key1 = M2Crypto.RSA.load_pub_key('public.pem') 
f = open('public.pem', 'rb') 
bio = M2Crypto.BIO.MemoryBuffer(f.read()) 
pub_key2 = M2Crypto.RSA.load_pub_key_bio(bio) 
print pub_key1 == pub_key2 

false 

답변

1

인쇄 pub_key1 == pub_key2 -> 거짓 있지만 :

PUBKEY_TEMPLATE = "-----BEGIN PUBLIC KEY-----\n{}\n-----END PUBLIC KEY-----" 
pub_key1 = M2Crypto.RSA.load_pub_key('public.pem') 
bio = M2Crypto.BIO.MemoryBuffer(PUBKEY_TEMPLATE.format('<string with pub key>'.strip())) 
pub_key2 = M2Crypto.RSA.load_pub_key_bio(bio) 
CipherText1 = pub_key1.public_encrypt("This is a secret", M2Crypto.RSA.pkcs1_oaep_padding) 
CipherText2 = pub_key2.public_encrypt("This is a secret", M2Crypto.RSA.pkcs1_oaep_padding) 
RSA = M2Crypto.RSA.load_key('private.pem') 
Text1 = RSA.private_decrypt(CipherText1, M2Crypto.RSA.pkcs1_oaep_padding) 
Text2 = RSA.private_decrypt(CipherText2, M2Crypto.RSA.pkcs1_oaep_padding) 
print Text1 
print Text2 


This is a secret 
This is a secret