Detailed explanation of reading public and private keys for encryption and decryption under python

Y2J
Release: 2017-04-20 09:58:25
Original
2685 people have browsed it

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);

  ...
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template