每日一模块_M2Crypto模块rsa加密解密
使用python进行rsa加密与加密,包括公钥加密私钥解密,私钥加密公钥解密。(需要安装M2Crypto库)。
#!/usr/bin/env python #encoding=utf-8 ''' 测试rsa加密解密 ''' 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 '公钥加密,私钥解密' 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('明文:%s'% txt) print '*************************************************************' print '私钥加密,公钥解密' 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)
库的安装说明
M2Crypto库的下载地址:
https://github.com/martinpaljak/M2Crypto
或者:https://pypi.python.org/pypi/M2Crypto
依赖的库:openssh-devel gcc swig (这3个库在centos上可以直接使用yum安装)
原文链接:
https://www.jb51.net/article/79596.htm
人生苦短,我用python!