利用Python實(shí)現(xiàn)RSA加密解密方法實(shí)例
前言
- 加密技術(shù)在數(shù)據(jù)安全存儲(chǔ),數(shù)據(jù)傳輸中發(fā)揮著重要作用,能夠保護(hù)用戶隱私數(shù)據(jù)安全,防止信息竊取。RSA是一種非對(duì)稱加密技術(shù),在軟件、網(wǎng)頁(yè)中已得到廣泛應(yīng)用。本文將介紹RSA加密解密在python中的實(shí)現(xiàn)。
- 原則:公鑰加密,私鑰解密
- 解釋:具體過(guò)程的解釋請(qǐng)見(jiàn)代碼前的注釋
RSA加密實(shí)驗(yàn)基本流程:
一、選取兩個(gè)大素?cái)?shù)p、q,并計(jì)算得到n、phi_n
二、選取常用的e = 0x10001,方便將冪運(yùn)算優(yōu)化為左移,加快運(yùn)算速度
三、計(jì)算d,使用了擴(kuò)展歐幾里得算法
四、輸入明文a,將明文轉(zhuǎn)化為可以用于計(jì)算的數(shù)字形式
五、對(duì)a使用快速冪取模,得到密文b,以16進(jìn)制顯示
RSA解密流程:
六、對(duì)b使用快速冪取模,得到明文a,以字符形式顯示
一、安裝模塊
pip install pycryptodome
二、生成密鑰對(duì)
- 密鑰對(duì)文件生成和讀取
- 代碼:
from Crypto.PublicKey import RSA
def create_rsa_pair(is_save=False):
'''
創(chuàng)建rsa公鑰私鑰對(duì)
:param is_save: default:False
:return: public_key, private_key
'''
f = RSA.generate(2048)
private_key = f.exportKey("PEM") # 生成私鑰
public_key = f.publickey().exportKey() # 生成公鑰
if is_save:
with open("crypto_private_key.pem", "wb") as f:
f.write(private_key)
with open("crypto_public_key.pem", "wb") as f:
f.write(public_key)
return public_key, private_key
def read_public_key(file_path="crypto_public_key.pem") -> bytes:
with open(file_path, "rb") as x:
b = x.read()
return b
def read_private_key(file_path="crypto_private_key.pem") -> bytes:
with open(file_path, "rb") as x:
b = x.read()
return b三、加密
- 流程:輸入文本(str)→字符串編碼(默認(rèn)utf-8)(bytes)→rsa加密(bytes)→base64編碼(bytes)→解碼為字符串(str)
- 代碼:
import base64
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
def encryption(text: str, public_key: bytes):
# 字符串指定編碼(轉(zhuǎn)為bytes)
text = text.encode('utf-8')
# 構(gòu)建公鑰對(duì)象
cipher_public = PKCS1_v1_5.new(RSA.importKey(public_key))
# 加密(bytes)
text_encrypted = cipher_public.encrypt(text)
# base64編碼,并轉(zhuǎn)為字符串
text_encrypted_base64 = base64.b64encode(text_encrypted ).decode()
return text_encrypted_base64
if __name__ == '__main__':
public_key = read_public_key()
text = '123456'
text_encrypted_base64 = encryption(text, public_key)
print('密文:',text_encrypted_base64)四、解密
- 說(shuō)明:解密流程與加密流程相反(按照加密流程逆序解密)
- 流程:輸入文本(str)→字符串編碼(默認(rèn)utf-8)(bytes)→base64解碼(bytes)→rsa解密(bytes)→解碼為字符串(str)
- 代碼:
import base64
from Crypto.Cipher import PKCS1_v1_5
from Crypto import Random
from Crypto.PublicKey import RSA
def decryption(text_encrypted_base64: str, private_key: bytes):
# 字符串指定編碼(轉(zhuǎn)為bytes)
text_encrypted_base64 = text_encrypted_base64.encode('utf-8')
# base64解碼
text_encrypted = base64.b64decode(text_encrypted_base64 )
# 構(gòu)建私鑰對(duì)象
cipher_private = PKCS1_v1_5.new(RSA.importKey(private_key))
# 解密(bytes)
text_decrypted = cipher_private.decrypt(text_encrypted , Random.new().read)
# 解碼為字符串
text_decrypted = text_decrypted.decode()
return text_decrypted
if __name__ == '__main__':
# 生成密文
public_key = read_public_key()
text = '123456'
text_encrypted_base64 = encryption(text, public_key)
print('密文:',text_encrypted_base64)
# 解密
private_key = read_private_key()
text_decrypted = decryption(text_encrypted_base64, private_key)
print('明文:',text_decrypted)五、完整代碼
import base64
from Crypto.Cipher import PKCS1_v1_5
from Crypto import Random
from Crypto.PublicKey import RSA
# ------------------------生成密鑰對(duì)------------------------
def create_rsa_pair(is_save=False):
'''
創(chuàng)建rsa公鑰私鑰對(duì)
:param is_save: default:False
:return: public_key, private_key
'''
f = RSA.generate(2048)
private_key = f.exportKey("PEM") # 生成私鑰
public_key = f.publickey().exportKey() # 生成公鑰
if is_save:
with open("crypto_private_key.pem", "wb") as f:
f.write(private_key)
with open("crypto_public_key.pem", "wb") as f:
f.write(public_key)
return public_key, private_key
def read_public_key(file_path="crypto_public_key.pem") -> bytes:
with open(file_path, "rb") as x:
b = x.read()
return b
def read_private_key(file_path="crypto_private_key.pem") -> bytes:
with open(file_path, "rb") as x:
b = x.read()
return b
# ------------------------加密------------------------
def encryption(text: str, public_key: bytes):
# 字符串指定編碼(轉(zhuǎn)為bytes)
text = text.encode('utf-8')
# 構(gòu)建公鑰對(duì)象
cipher_public = PKCS1_v1_5.new(RSA.importKey(public_key))
# 加密(bytes)
text_encrypted = cipher_public.encrypt(text)
# base64編碼,并轉(zhuǎn)為字符串
text_encrypted_base64 = base64.b64encode(text_encrypted).decode()
return text_encrypted_base64
# ------------------------解密------------------------
def decryption(text_encrypted_base64: str, private_key: bytes):
# 字符串指定編碼(轉(zhuǎn)為bytes)
text_encrypted_base64 = text_encrypted_base64.encode('utf-8')
# base64解碼
text_encrypted = base64.b64decode(text_encrypted_base64)
# 構(gòu)建私鑰對(duì)象
cipher_private = PKCS1_v1_5.new(RSA.importKey(private_key))
# 解密(bytes)
text_decrypted = cipher_private.decrypt(text_encrypted, Random.new().read)
# 解碼為字符串
text_decrypted = text_decrypted.decode()
return text_decrypted
if __name__ == '__main__':
# 生成密鑰對(duì)
# create_rsa_pair(is_save=True)
# public_key = read_public_key()
# private_key = read_private_key()
public_key, private_key = create_rsa_pair(is_save=False)
# 加密
text = '123456'
text_encrypted_base64 = encryption(text, public_key)
print('密文:', text_encrypted_base64)
# 解密
text_decrypted = decryption(text_encrypted_base64, private_key)
print('明文:', text_decrypted)
總結(jié)
到此這篇關(guān)于利用Python實(shí)現(xiàn)RSA加密解密的文章就介紹到這了,更多相關(guān)Python RSA加密解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬蟲抓取論壇關(guān)鍵字過(guò)程解析
這篇文章主要介紹了Python爬蟲抓取論壇關(guān)鍵字過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Pytorch實(shí)現(xiàn)Fashion-mnist分類任務(wù)全過(guò)程
這篇文章主要介紹了Pytorch實(shí)現(xiàn)Fashion-mnist分類任務(wù)全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
基于Python實(shí)現(xiàn)格斗小游戲的示例代碼
格斗游戲,曾經(jīng)是街機(jī)廳里最火爆的游戲之一,甚至可以把“之一”去掉,那個(gè)年代的格斗游戲就是街機(jī)游戲的王。本文就來(lái)用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的格斗游戲,感興趣的可以了解一下2023-03-03
python中的信號(hào)通信 blinker的使用小結(jié)
信號(hào)是一種通知或者說(shuō)通信的方式,信號(hào)分為發(fā)送方和接收方,信號(hào)的特點(diǎn)就是發(fā)送端通知訂閱者發(fā)生了什么,今天通過(guò)本文給大家介紹python中的信號(hào)通信 blinker的相關(guān)知識(shí),感興趣的朋友一起看看吧2021-10-10
python3使用mutagen進(jìn)行音頻元數(shù)據(jù)處理的方法
mutagen是一個(gè)處理音頻元數(shù)據(jù)的python模塊,支持多種音頻格式,是一個(gè)純粹的python庫(kù),僅依賴python標(biāo)準(zhǔn)庫(kù),可在Python?3.7及以上版本運(yùn)行,支持Linux、Windows?和?macOS系統(tǒng),這篇文章主要介紹了python3使用mutagen進(jìn)行音頻元數(shù)據(jù)處理,需要的朋友可以參考下2022-10-10
Pycharm編輯器技巧之自動(dòng)導(dǎo)入模塊詳解
我們?cè)诰幊踢^(guò)程中經(jīng)常會(huì)不經(jīng)意的使用到一些尚未導(dǎo)入的類和模塊,在這種情況下Pycharm會(huì)幫助我們定位模塊文件位置并將其添加到導(dǎo)入列表中,這也就是所謂的自動(dòng)導(dǎo)入模塊功能。本文給大家介紹了關(guān)于Pycharm編輯器技巧之自動(dòng)導(dǎo)入模塊的相關(guān)資料,需要的朋友可以參考下。2017-07-07

