今天只有短的RSA鑰匙才可能被強力方式解破。到2008年為止,世界上還沒有任何可靠的攻擊RSA算法的方式。只要其密鑰的長度足夠長,用RSA加密的信息實際上是不能被解破的。但在分布式計算和量子計算機理論日趨成熟的今天,RSA加密安全性受到了挑戰(zhàn)。
RSA算法基于一個十分簡單的數(shù)論事實:將兩個大素數(shù)相乘十分容易,但是想要對其乘積進行因式分解卻極其困難,因此可以將乘積公開作為加密密鑰。
核心代碼:
下面一段代碼給大家介紹python_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安裝)
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com