Read the public/private key in python for encryption and decryption.

Source: Internet
Author: User
This article describes how to read the public/private key for encryption/decryption in python, for more information about how to read the public and private keys for encryption and decryption instances in python, see the following article.

Read the public/private key in python for encryption and decryption.

In RSA, one application mode is public key encryption and private key decryption (the other is private key signature and public key verification ). The following is an example of an application in Python.

Suppose I have a public key file, rsa_pub.pem. I want to read this public key and use it to encrypt it.


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 = '000000'; en_data = pub_key.public_encrypt (data, RSA. pkcs1_padding );...

Private key file rsa_private.pem, which reads the private key and is used for decryption


From M2Crypto import RSA, BIO private_key_str = file ('RSA _ private. pem', 'RB '). read (); private_key = RSA. load_key_string (private_key_str); data = 'sdfdjslfjaskldfjdskljsd'; de_data = private_key.private_decrypt (data, RSA. pkcs1_padding );

The above is a detailed description of the example of reading the public/private key for encryption and decryption in python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.