Python生成rsa密鑰對操作示例
本文實(shí)例講述了Python生成rsa密鑰對操作。分享給大家供大家參考,具體如下:
# -*- coding: utf-8 -*- import rsa # 先生成一對密鑰,然后保存.pem格式文件,當(dāng)然也可以直接使用 (pubkey, privkey) = rsa.newkeys(1024) pub = pubkey.save_pkcs1() pubfile = open('public.pem','w+') pubfile.write(pub) pubfile.close() pri = privkey.save_pkcs1() prifile = open('private.pem','w+') prifile.write(pri) prifile.close() # load公鑰和密鑰 message = 'lovesoo.org' with open('public.pem') as publickfile: p = publickfile.read() pubkey = rsa.PublicKey.load_pkcs1(p) with open('private.pem') as privatefile: p = privatefile.read() privkey = rsa.PrivateKey.load_pkcs1(p) # 用公鑰加密、再用私鑰解密 crypto = rsa.encrypt(message, pubkey) message = rsa.decrypt(crypto, privkey) print message # sign 用私鑰簽名認(rèn)證、再用公鑰驗(yàn)證簽名 signature = rsa.sign(message, privkey, 'SHA-1') rsa.verify('lovesoo.org', signature, pubkey)
對文件進(jìn)行RSA加密解密
from rsa.bigfile import * import rsa with open('public.pem') as publickfile: p = publickfile.read() pubkey = rsa.PublicKey.load_pkcs1(p) with open('private.pem') as privatefile: p = privatefile.read() privkey = rsa.PrivateKey.load_pkcs1(p) with open('mysec.txt', 'rb') as infile, open('outputfile', 'wb') as outfile: #加密輸出 encrypt_bigfile(infile, outfile, pubkey) with open('outputfile', 'rb') as infile2, open('result', 'wb') as outfile2: #解密輸出 decrypt_bigfile(infile2, outfile2, privkey)
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
在線RSA加密/解密工具:
http://tools.jb51.net/password/rsa_encode
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
conda配置python虛擬環(huán)境的實(shí)現(xiàn)步驟
本文主要介紹了conda配置python虛擬環(huán)境的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03pytorch學(xué)習(xí)教程之自定義數(shù)據(jù)集
這篇文章主要給大家介紹了關(guān)于pytorch學(xué)習(xí)教程之自定義數(shù)據(jù)集的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11python包pdfkit(wkhtmltopdf)?將HTML轉(zhuǎn)換為PDF的操作方法
pdfkit,把HTML+CSS格式的文件轉(zhuǎn)換成PDF格式文檔的一種工具。它就是html轉(zhuǎn)成pdf工具包wkhtmltopdf的Python封裝。所以,必須手動安裝wkhtmltopdf,這篇文章主要介紹了python包pdfkit(wkhtmltopdf)將HTML轉(zhuǎn)換為PDF,需要的朋友可以參考下2022-04-04PyCharm搭建Spark開發(fā)環(huán)境實(shí)現(xiàn)第一個pyspark程序
這篇文章主要介紹了PyCharm搭建Spark開發(fā)環(huán)境實(shí)現(xiàn)第一個pyspark程序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06四行Python3代碼實(shí)現(xiàn)圖片添加美顏效果
這篇文章主要為大家介紹了如何利用Python語言實(shí)現(xiàn)給圖片添加美顏效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-04-04