python_rsa encryption and decryption

Python for using encryption and encryption rsa, including public key encryption private key to decrypt the private key to decrypt the encrypted public key. (Need to install M2Crypto library).

#!/usr/bin/env python
#encoding=utf-8 
'''
Test rsa encryption and decryption
'''
from M2Crypto import RSA 
msg = 'aaaa-aaaa'
rsa_pub = RSA.load_pub_key('rsa_pub.pem')
rsa_pri = RSA.load_key ( ' rsa_pri.pem ' )
 Print  ' ************************************ ************************************************************ ' 
Print  ' public key cryptography, private key to decrypt ' 
ctxt = rsa_pub.public_encrypt (MSG, RSA.pkcs1_padding)
ctxt64 = ctxt.encode('base64')
print ('密文:%s'% ctxt64)
rsa_pri = RSA.load_key('rsa_pri.pem')
TXT = rsa_pri.private_decrypt (ctxt, RSA.pkcs1_padding)
 Print ( ' plaintext: S% ' % TXT)
 Print  ' ************************************************************ ************************************ ' 
Print  ' private key encryption, public key to decrypt ' 
ctxt_pri = rsa_pri .private_encrypt (msg, RSA.pkcs1_padding)
ctxt64_pri = ctxt.encode('base64')
print ('密文:%s'% ctxt64_pri)
txt_pri = rsa_pub.public_decrypt(ctxt_pri, RSA.pkcs1_padding)
print('明文:%s'% txt_pri)

Installation instructions library

Download M2Crypto library:

https://github.com/martinpaljak/M2Crypto

Or: https://pypi.python.org/pypi/M2Crypto

Dependent libraries: openssh-devel gcc swig (three libraries can be used directly mounted on CentOS yum)

Original link:

https://www.jb51.net/article/79596.htm

Guess you like

Origin www.cnblogs.com/sunxiuwen/p/11904885.html