利用Python實(shí)現(xiàn)RSA加密解密方法實(shí)例_第1頁(yè)
利用Python實(shí)現(xiàn)RSA加密解密方法實(shí)例_第2頁(yè)
利用Python實(shí)現(xiàn)RSA加密解密方法實(shí)例_第3頁(yè)
利用Python實(shí)現(xiàn)RSA加密解密方法實(shí)例_第4頁(yè)
利用Python實(shí)現(xiàn)RSA加密解密方法實(shí)例_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第利用Python實(shí)現(xiàn)RSA加密解密方法實(shí)例目錄前言一、安裝模塊二、生成密鑰對(duì)三、加密四、解密五、完整代碼總結(jié)

前言

加密技術(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,以字符形式顯示

一、安裝模塊

pipinstallpycryptodome

二、生成密鑰對(duì)

密鑰對(duì)文件生成和讀取代碼:

fromCrypto.PublicKeyimportRSA

defcreate_rsa_pair(is_save=False):

創(chuàng)建rsa公鑰私鑰對(duì)

:paramis_save:default:False

:return:public_key,private_key

f=RSA.generate(2048)

private_key=f.exportKey("PEM")#生成私鑰

public_key=f.publickey().exportKey()#生成公鑰

ifis_save:

withopen("crypto_private_key.pem","wb")asf:

f.write(private_key)

withopen("crypto_public_key.pem","wb")asf:

f.write(public_key)

returnpublic_key,private_key

defread_public_key(file_path="crypto_public_key.pem")-bytes:

withopen(file_path,"rb")asx:

b=x.read()

returnb

defread_private_key(file_path="crypto_private_key.pem")-bytes:

withopen(file_path,"rb")asx:

b=x.read()

returnb

三、加密

流程:輸入文本(str)字符串編碼(默認(rèn)utf-8)(bytes)rsa加密(bytes)base64編碼(bytes)解碼為字符串(str)代碼:

importbase64

fromCrypto.CipherimportPKCS1_v1_5

fromCrypto.PublicKeyimportRSA

defencryption(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()

returntext_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)代碼:

importbase64

fromCrypto.CipherimportPKCS1_v1_5

fromCryptoimportRandom

fromCrypto.PublicKeyimportRSA

defdecryption(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()

returntext_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)

五、完整代碼

importbase64

fromCrypto.CipherimportPKCS1_v1_5

fromCryptoimportRandom

fromCrypto.PublicKeyimportRSA

#------------------------生成密鑰對(duì)------------------------

defcreate_rsa_pair(is_save=False):

創(chuàng)建rsa公鑰私鑰對(duì)

:paramis_save:default:False

:return:public_key,private_key

f=RSA.generate(2048)

private_key=f.exportKey("PEM")#生成私鑰

public_key=f.publickey().exportKey()#生成公鑰

ifis_save:

withopen("crypto_private_key.pem","wb")asf:

f.write(private_key)

withopen("crypto_public_key.pem","wb")asf:

f.write(public_key)

returnpublic_key,private_key

defread_public_key(file_path="crypto_public_key.pem")-bytes:

withopen(file_path,"rb")asx:

b=x.read()

returnb

defread_private_key(file_path="crypto_private_key.pem")-bytes:

withopen(file_path,"rb")asx:

b=x.read()

returnb

#------------------------加密------------------------

defencryption(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()

returntext_encrypted_base64

#------------------------解密------------------------

defdecryption(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()

returntext_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

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論