欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用RSA庫(kù)加密和解密

 更新時(shí)間:2022年06月06日 10:26:06   作者:springsnow  
這篇文章介紹了Python使用RSA庫(kù)加密和解密的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、rsa庫(kù)(推薦)

1、公鑰加密、私鑰解密

# -*- coding: utf-8 -*-
import rsa

# rsa加密
def rsaEncrypt(str):
    # 生成公鑰、私鑰
    (pubkey, privkey) = rsa.newkeys(512)
    print("pub: ", pubkey)
    print("priv: ", privkey)
    # 明文編碼格式
    content = str.encode('utf-8')
    # 公鑰加密
    crypto = rsa.encrypt(content, pubkey)
    return (crypto, privkey)


# rsa解密
def rsaDecrypt(str, pk):
    # 私鑰解密
    content = rsa.decrypt(str, pk)
    con = content.decode('utf-8')
    return con


(a, b) = rsaEncrypt("hello")
print('加密后密文:'+ a)
content = rsaDecrypt(a, b)
print('解密后明文:'+ content)

2、密鑰導(dǎo)出、簽名驗(yàn)證

import rsa

# 先生成一對(duì)密鑰,然后保存.pem格式文件,當(dāng)然也可以直接使用
(pubkey, privkey) = rsa.newkeys(1024)

pub = pubkey.save_pkcs1()
pubfile = open('public.pem', 'wb')
pubfile.write(pub)
pubfile.close()

pri = privkey.save_pkcs1()
prifile = open('private.pem', 'wb')
prifile.write(pri)
prifile.close()

# load公鑰和密鑰
message = 'lovesoo.org'
with open('public.pem', "rb") as publickfile:
    p = publickfile.read()
    pubkey = rsa.PublicKey.load_pkcs1(p)
    print(pubkey)

with open('private.pem', "rb") as privatefile:
    p = privatefile.read()
    privkey = rsa.PrivateKey.load_pkcs1(p)
    print(privkey)

# 用公鑰加密、再用私鑰解密
crypto = rsa.encrypt(message.encode('utf-8'), pubkey)
message = rsa.decrypt(crypto, privkey)
message = message.decode('utf-8')
print (message)

# sign 用私鑰簽名認(rèn)證、再用公鑰驗(yàn)證簽名
signature = rsa.sign(message.encode('utf-8'), privkey, 'SHA-1')
verify = rsa.verify('lovesoo.org'.encode('utf-8'), signature, pubkey)
print(verify)

二、使用 Crypto.PublicKey.RSA庫(kù)

1、使用 Crypto.PublicKey.RSA 生成公鑰、私鑰:

import Crypto.PublicKey.RSA
import Crypto.Random
 
x = Crypto.PublicKey.RSA.generate(2048)
#  Crypto.PublicKey.RSA.generate(2048, Crypto.Random.new().read)   使用 Crypto.Random.new().read 偽隨機(jī)數(shù)生成器
a = x.exportKey("PEM")  # 生成私鑰
b = x.publickey().exportKey()   # 生成公鑰

with open("a.pem", "wb") as x:
    x.write(a)
with open("b.pem", "wb") as x:
    x.write(b)

2、使用 Crypto.PublicKey.RSA.importKey(private_key) 生成公鑰和證書(shū):

import Crypto.PublicKey.RSA
 
with open("a.pem", "rb") as x:
    xx = Crypto.PublicKey.RSA.importKey(x.read())
 
b = xx.publickey().exportKey()   # 生成公鑰
with open("b.pem", "wb") as x:
    x.write(b)
    
a = xx.exportKey("DER")   # 生成 DER 格式的證書(shū)
with open("a.der", "wb") as x:
    x.write(a)

3、使用 Crypto進(jìn)行RSA加解密

import Crypto.PublicKey.RSA
import Crypto.Cipher.PKCS1_v1_5
import Crypto.Random
import Crypto.Signature.PKCS1_v1_5
import Crypto.Hash
 
y = b"abcdefg1234567"
 
with open("b.pem", "rb") as x:
    b = x.read()
    cipher_public = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(b))
    cipher_text = cipher_public.encrypt(y) # 使用公鑰進(jìn)行加密

with open("a.pem", "rb") as x:
    a = x.read()
    # 如果私鑰有密碼 則使用相應(yīng)密碼 Crypto.PublicKey.RSA.importKey(a, password)
    cipher_private = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(a))
    text = cipher_private.decrypt(cipher_text, Crypto.Random.new().read)    # 使用私鑰進(jìn)行解密
assert text == y    # 斷言驗(yàn)證
 
with open("c.pem", "rb") as x:
    c = x.read()
    c_rsa = Crypto.PublicKey.RSA.importKey(c)
    signer = Crypto.Signature.PKCS1_v1_5.new(c_rsa)
    msg_hash = Crypto.Hash.SHA256.new()
    msg_hash.update(y)
    sign = signer.sign(msg_hash)    # 使用私鑰進(jìn)行'sha256'簽名

with open("d.pem", "rb") as x:
    d = x.read()
    d_rsa = Crypto.PublicKey.RSA.importKey(d)
    verifer = Crypto.Signature.PKCS1_v1_5.new(d_rsa)
    msg_hash = Crypto.Hash.SHA256.new()
    msg_hash.update(y)
    verify = verifer.verify(msg_hash, sign) # 使用公鑰驗(yàn)證簽名
    print(verify)

到此這篇關(guān)于Python使用RSA庫(kù)加密和解密的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • PyInstaller如何打包依賴文件至目標(biāo)程序目錄

    PyInstaller如何打包依賴文件至目標(biāo)程序目錄

    這篇文章主要介紹了PyInstaller如何打包依賴文件至目標(biāo)程序目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 一文教你將Visual Studio Code變成Python開(kāi)發(fā)神器

    一文教你將Visual Studio Code變成Python開(kāi)發(fā)神器

    Visual Studio Code 是一款功能強(qiáng)大、可擴(kuò)展且輕量級(jí)的代碼編輯器,經(jīng)過(guò)多年的發(fā)展,已經(jīng)成為 Python 社區(qū)的首選代碼編輯器之一。本文將為大家介紹一下如何將Visual Studio Code變成Python開(kāi)發(fā)神器,需要的可以參考一下
    2022-07-07
  • scrapy爬蟲(chóng):scrapy.FormRequest中formdata參數(shù)詳解

    scrapy爬蟲(chóng):scrapy.FormRequest中formdata參數(shù)詳解

    這篇文章主要介紹了scrapy爬蟲(chóng):scrapy.FormRequest中formdata參數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • tensorflow使用tf.data.Dataset 處理大型數(shù)據(jù)集問(wèn)題

    tensorflow使用tf.data.Dataset 處理大型數(shù)據(jù)集問(wèn)題

    這篇文章主要介紹了tensorflow使用tf.data.Dataset 處理大型數(shù)據(jù)集問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Django錯(cuò)誤:TypeError at / ''bool'' object is not callable解決

    Django錯(cuò)誤:TypeError at / ''bool'' object is not callable解決

    這篇文章主要介紹了Django 錯(cuò)誤:TypeError at / 'bool' object is not callable解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python使用googletrans報(bào)錯(cuò)的解決方法

    Python使用googletrans報(bào)錯(cuò)的解決方法

    這篇文章主要給大家介紹了關(guān)于Python使用googletrans報(bào)錯(cuò)的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Python實(shí)現(xiàn)文件按照日期命名的方法

    Python實(shí)現(xiàn)文件按照日期命名的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)文件按照日期命名的方法,涉及Python針對(duì)文件的遍歷、讀寫(xiě)及時(shí)間操作相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • 利用python如何處理百萬(wàn)條數(shù)據(jù)(適用java新手)

    利用python如何處理百萬(wàn)條數(shù)據(jù)(適用java新手)

    這篇文章主要給大家介紹了關(guān)于利用python如何處理百萬(wàn)條數(shù)據(jù)的相關(guān)資料,本文的教程非常適用于java新手,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • python簡(jiǎn)單批量梯度下降代碼

    python簡(jiǎn)單批量梯度下降代碼

    大家好,本篇文章主要講的是python簡(jiǎn)單批量梯度下降代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Python+OpenCV實(shí)戰(zhàn)之拖拽虛擬方塊的實(shí)現(xiàn)

    Python+OpenCV實(shí)戰(zhàn)之拖拽虛擬方塊的實(shí)現(xiàn)

    這篇文章主要介紹了如何利用Python+OpenCV實(shí)現(xiàn)拖拽虛擬方塊的效果,即根據(jù)手指坐標(biāo)位置和矩形的坐標(biāo)位置,判斷手指點(diǎn)是否在矩形上,如果在則矩形跟隨手指移動(dòng),感興趣的可以了解一下
    2022-08-08

最新評(píng)論