最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)RSA加密解密

 更新時間:2022年04月14日 18:29:17   作者:淺若清風(fēng)cyf?  
這篇文章主要介紹了Python實(shí)現(xiàn)RSA加密解密,加密技術(shù)在數(shù)據(jù)安全存儲,數(shù)據(jù)傳輸中發(fā)揮著重要作用,能夠保護(hù)用戶隱私數(shù)據(jù)安全,防止信息竊取。RSA是一種非對稱加密技術(shù),在軟件、網(wǎng)頁中已得到廣泛應(yīng)用,下面文章更多相關(guān)內(nèi)容需要的小伙伴可以參考一下

前言

  • 加密技術(shù)在數(shù)據(jù)安全存儲,數(shù)據(jù)傳輸中發(fā)揮著重要作用,能夠保護(hù)用戶隱私數(shù)據(jù)安全,防止信息竊取。RSA是一種非對稱加密技術(shù),在軟件、網(wǎng)頁中已得到廣泛應(yīng)用。本文將介紹RSA加密解密在python中的實(shí)現(xiàn)。
  • 原則:公鑰加密,私鑰解密
  • 解釋:具體過程的解釋請見代碼前的注釋

如果本文對您有幫助,不妨點(diǎn)贊、收藏、關(guān)注喲!您的支持和關(guān)注是博主創(chuàng)作的動力!

一、安裝模塊

pip install pycryptodome 

二、生成密鑰對

  • 密鑰對文件生成和讀取
  • 代碼:
from Crypto.PublicKey import RSA

def create_rsa_pair(is_save=False):
    '''
    創(chuàng)建rsa公鑰私鑰對
    :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)建公鑰對象
	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)

四、解密

  • 說明:解密流程與加密流程相反(按照加密流程逆序解密)
  • 流程:輸入文本(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)建私鑰對象
	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


# ------------------------生成密鑰對------------------------
def create_rsa_pair(is_save=False):
    '''
    創(chuàng)建rsa公鑰私鑰對
    :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)建公鑰對象
    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)建私鑰對象
    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__':
    # 生成密鑰對
    # 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)

運(yùn)行:

在這里插入圖片描述

到此這篇關(guān)于Python實(shí)現(xiàn)RSA加密解密的文章就介紹到這了,更多相關(guān)Python RSA加解密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • selenium切換標(biāo)簽頁解決get超時問題的完整代碼

    selenium切換標(biāo)簽頁解決get超時問題的完整代碼

    這篇文章主要給大家介紹了關(guān)于selenium切換標(biāo)簽頁解決get超時問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Python的時間模塊datetime詳解

    Python的時間模塊datetime詳解

    本文給大家分享的是在python中的時間模塊datetime的使用方法,注意事項(xiàng)以及簡單示例,有需要的小伙伴可以參考下
    2017-04-04
  • Python經(jīng)典案例之圖像漫水填充分割詳解

    Python經(jīng)典案例之圖像漫水填充分割詳解

    圖像分割是將圖像分成若干具有獨(dú)特性質(zhì)的區(qū)域并提取感興趣目標(biāo)的技術(shù)和過程,這篇文章將詳細(xì)講解漫水填充分割應(yīng)用,感興趣的小伙伴可以了解一下
    2023-01-01
  • Python中自然語言處理和文本挖掘的常規(guī)操作詳解

    Python中自然語言處理和文本挖掘的常規(guī)操作詳解

    自然語言處理和文本挖掘是數(shù)據(jù)科學(xué)中的重要領(lǐng)域,涉及對文本數(shù)據(jù)的分析和處理,這篇文章為大家介紹了一些常見的任務(wù)和實(shí)現(xiàn)方法,需要的可以了解下
    2025-02-02
  • Python Pandas中創(chuàng)建Series的三種方法總結(jié)

    Python Pandas中創(chuàng)建Series的三種方法總結(jié)

    這篇文章主要介紹了Python Pandas中創(chuàng)建Series的三種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python簡單獲取網(wǎng)卡名稱及其IP地址的方法【基于psutil模塊】

    Python簡單獲取網(wǎng)卡名稱及其IP地址的方法【基于psutil模塊】

    這篇文章主要介紹了Python簡單獲取網(wǎng)卡名稱及其IP地址的方法,結(jié)合實(shí)例形式分析了Python基于psutil模塊針對本機(jī)網(wǎng)卡硬件信息的讀取操作簡單使用技巧,需要的朋友可以參考下
    2018-05-05
  • python使用urllib2提交http post請求的方法

    python使用urllib2提交http post請求的方法

    這篇文章主要介紹了python使用urllib2提交http post請求的方法,涉及Python使用urllib2模塊的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • 使用Python實(shí)現(xiàn)兩組數(shù)據(jù)縱向排序

    使用Python實(shí)現(xiàn)兩組數(shù)據(jù)縱向排序

    在數(shù)據(jù)處理和分析中,排序是一項(xiàng)非?;A(chǔ)且重要的操作,本文將詳細(xì)介紹如何使用Python對兩組數(shù)據(jù)進(jìn)行縱向排序,即每一列分別進(jìn)行排序,同時保持?jǐn)?shù)據(jù)的對應(yīng)關(guān)系,需要的可以參考下
    2024-12-12
  • Python requests模塊實(shí)例用法

    Python requests模塊實(shí)例用法

    在本篇文章中小編給大家分享了關(guān)于Python requests模塊實(shí)例用法,有需要的朋友們學(xué)習(xí)參考下。
    2019-02-02
  • python題解LeetCode303區(qū)域和檢索示例詳解

    python題解LeetCode303區(qū)域和檢索示例詳解

    這篇文章主要為大家介紹了python題解LeetCode303區(qū)域和檢索示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評論

延长县| 军事| 锡林浩特市| 湛江市| 项城市| 赤峰市| 修文县| 甘谷县| 陕西省| 江北区| 光泽县| 西乌珠穆沁旗| 台州市| 盱眙县| 赞皇县| 夏津县| 商南县| 额尔古纳市| 泉州市| 康马县| 甘孜县| 察雅县| 临清市| 汽车| 达拉特旗| 石景山区| 平湖市| 平原县| 景洪市| 临武县| 阳新县| 襄垣县| 长葛市| 正安县| 黑水县| 长汀县| 鹿泉市| 嘉禾县| 灌南县| 锡林郭勒盟| 崇义县|