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)文章
Python中的os.path路徑模塊中的操作方法總結(jié)
os.path模塊主要集成了針對路徑文件夾的操作功能,這里我們就來看一下Python中的os.path路徑模塊中的操作方法總結(jié),需要的朋友可以參考下2016-07-07
使用Keras訓(xùn)練好的.h5模型來測試一個(gè)實(shí)例
這篇文章主要介紹了使用Keras訓(xùn)練好的.h5模型來測試一個(gè)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Pandas使用分隔符或正則表達(dá)式將字符串拆分為多列
本文主要介紹了Pandas使用分隔符或正則表達(dá)式將字符串拆分為多列,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python編碼規(guī)范擺脫P(yáng)ython編碼噩夢
Python 中編碼問題,一直是很多 Python 開發(fā)者的噩夢,盡管你是工作多年的 Python 開發(fā)者,也肯定會(huì)經(jīng)常遇到令人神煩的編碼問題,收藏這篇文章以后你可以不用再Google2021-10-10
Nginx+Uwsgi+Django 項(xiàng)目部署到服務(wù)器的思路詳解
這篇文章主要介紹了Nginx+Uwsgi+Django 項(xiàng)目部署到服務(wù)器的思路,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
關(guān)于Python中的海象運(yùn)算符使用方法詳解
這篇文章主要介紹了關(guān)于Python中的海象運(yùn)算符“:=”使用方法詳解,海象運(yùn)算符(walrus?operator)是?Python?3.8?中引入的一種新的語法,需要的朋友可以參考下2023-04-04

