This article mainly introduces relevant information on reading public and private keys for encryption and decryption under python. Friends who need it can refer to
Reading public and private keys for encryption and decryption under python.
In RSA, one application mode is public key encryption and private key decryption (the other is private key signature and public key signature verification). The following are application examples under Python.
Suppose I have a public key file, rsa_pub.pem, and I want to read this public key and use it to encrypt.
from M2Crypto import RSA,BIO fp = file('rsa_pub.pem','rb'); pub_key_str = fp.read(); fp.close(); mb = BIO.MemoryBuffer(pub_key_str); pub_key = RSA.load_pub_key_bio(mb); data = '12345678'; en_data = pub_key.public_encrypt(data,RSA.pkcs1_padding); ...
Private key file rsa_private.pem, read the private key and use it to decrypt
from M2Crypto import RSA,BIO private_key_str = file('rsa_private.pem','rb').read(); private_key = RSA.load_key_string(private_key_str); data = 'sdfdjslfjaskldfjdsklfjsd'; de_data = private_key.private_decrypt(data,RSA.pkcs1_padding);
The above is the detailed content of Detailed explanation of reading public and private keys for encryption and decryption under python. For more information, please follow other related articles on the PHP Chinese website!