您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么利用Python實現(xiàn)RSA加密解密”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“怎么利用Python實現(xiàn)RSA加密解密”文章能幫助大家解決問題。
RSA加密實驗基本流程:
一、選取兩個大素數(shù)p、q,并計算得到n、phi_n
二、選取常用的e = 0x10001,方便將冪運算優(yōu)化為左移,加快運算速度
三、計算d,使用了擴展歐幾里得算法
四、輸入明文a,將明文轉(zhuǎn)化為可以用于計算的數(shù)字形式
五、對a使用快速冪取模,得到密文b,以16進制顯示
RSA解密流程:
六、對b使用快速冪取模,得到明文a,以字符形式顯示
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)→字符串編碼(默認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)→字符串編碼(默認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)
關(guān)于“怎么利用Python實現(xiàn)RSA加密解密”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。