IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python rsa和Crypto.PublicKey.RSA 模块 -> 正文阅读

[Python知识库]python rsa和Crypto.PublicKey.RSA 模块

rsa基础 https://blog.csdn.net/qq_52549196/article/details/123491446

Crypto.PublicKey.RSA

生成公私钥

import Crypto.PublicKey.RSA

x = Crypto.PublicKey.RSA.generate(1024)

private = x.exportKey("PEM")  # 生成私钥
public = x.publickey().exportKey()   # 生成公钥

with open("private.pem", "wb") as x:
    x.write(private)
with open("public.pem", "wb") as x:
    x.write(public)

生成的密钥结果

private.pem

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCcIy+CGb9lIkimJEwKXtRqAjCIAngiXZchKlnIAQBnAbI6VKQz
Jtkrak2MQXVy9eN7tj97fTHCjRCxdRNP1VaROGjnVPkOEbGHhvNc/BjW0x017B4p
93ncw8mbSM3f/+lzIVkdr9EA6iEOajh+hjehc6NM5Al6HESniQ0gnhroSQIDAQAB
AoGASBRMNczKGReTHzPJcM4nVErveZJeQEshXhutd9HSjDrxutDhPoX0IoFADHwZ
irkty6ouM0Vv58b0il+oSyYLAGUS1Qrdr+kmOh8TjSeBt2xX+RKXwrzVY14MrsqJ
3zOhlPcsyVDtGztX7+4Eel+mLElf2D41oB05pNiSTwBkE5sCQQC3H5c4GO/UReLZ
4n/Fz0BMahAzgkXz+bLYZcJU+2chbuoStkFVEdtU3RvxbgaO34CJtXX2NaCfCr5r
E0yGqPkbAkEA2kZLoGII0RCevNl0tzRatOwBZSdxuhh6emx1XMG3W4rOXifcB/ji
A4bGME5ImOvhZ7v6mA/3VZX9FaPUMhb+awJAfJLiBUh5hldQflamA0P9Ar64bMH3
ooRAFRQMglFQFB9m3vnbMerm4TFVfAUUMwd6YrPuHmLW9LqTVTYYvhae9wJBAIt1
2IIbwQha9yEwS0t2zvHDzMxlnFtLoo2v2HiwaMe7c0ljbElHspiQNvbMPcQ/CeCu
kVkw8lWjpHV8HGr4mAsCQC2ye3y2eaVqe7+tv8+DL1/idiKioY44uYtYIxNg1WxZ
Xzig7Ri7DKOFRLw9wyS2OdBUpcZJ01xsIqtjWuHFMDM=
-----END RSA PRIVATE KEY-----

public.pem

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcIy+CGb9lIkimJEwKXtRqAjCI
AngiXZchKlnIAQBnAbI6VKQzJtkrak2MQXVy9eN7tj97fTHCjRCxdRNP1VaROGjn
VPkOEbGHhvNc/BjW0x017B4p93ncw8mbSM3f/+lzIVkdr9EA6iEOajh+hjehc6NM
5Al6HESniQ0gnhroSQIDAQAB
-----END PUBLIC KEY-----

读取公私钥

公钥(n,e) 私钥(n,d)

import Crypto.PublicKey.RSA

with open("public.pem", 'rb') as x:
    public_key = Crypto.PublicKey.RSA.importKey(x.read())
    print(public_key.n)
    print(public_key.e)

with open("private.pem", 'rb') as x:
    private_key = Crypto.PublicKey.RSA.importKey(x.read())
    print(private_key.n)
    print(private_key.d)

验证

from Crypto.Util.number import bytes_to_long, long_to_bytes

c = bytes_to_long(b'xiaoxiaoran')  # 145568744200917766156476782

e = 65537
n = 109643441631386007615207297424331621014870648392454190718008050275074314522481791638636510984908444997607601077178481245842175926260005040415391800640503546886684365699706581166859202247619266556089792006189825410067873631530196098659194702332240479537964725096088707413180064673445928734211470673853181257801
d = 50615797257072614971608239314043075331406747818629252377709912828588977962378890781255295811418169113327115473541607959661444856509045615381349027762609120393751592942681652318623050975912426954942311074907668935122731656262294557574124857501289932634444295689852337987042610580415469287093874225498684658587
m = 4760371267684579630308571621455961776995122884329127227626895682331686504102393602160541535737343533151346614474308194637699485212151592440962407908438970696018720824027009690388552784020837086483711342994294618339728483898311036392512162634521480659231411043537783938261406684265246639310985133668266244632

m = pow(c, d, n)  # m = c^d mod n
c = pow(m, e, n)  # c = m^e mod n

# print(m)  # 密文

print(long_to_bytes(c))

使用公私钥加解密

import Crypto.PublicKey.RSA
import Crypto.Cipher.PKCS1_v1_5
import Crypto.Random


str = b"xiaoxiaoran"

with open("./public.pem", "rb") as x:
    cipher_public = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(x.read()))
    cipher_text = cipher_public.encrypt(str)  # 使用公钥进行加密
with open("./private.pem", "rb") as x:
    cipher_private = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(x.read()))
    text = cipher_private.decrypt(cipher_text, Crypto.Random.new().read)    # 使用私钥进行解密

print(text)

SHA256签名

import Crypto.Hash
import Crypto.Signature.PKCS1_v1_5
import Crypto.PublicKey.RSA


str = b"xiaoxiaoran"

with open("./private.pem", "rb") as x:
    c_rsa = Crypto.PublicKey.RSA.importKey(x.read())
    signer = Crypto.Signature.PKCS1_v1_5.new(c_rsa)
    msg_hash = Crypto.Hash.SHA256.new()
    msg_hash.update(str)
    sign = signer.sign(msg_hash)    # 使用私钥进行'sha256'签名
    # print(sign)

with open("./public.pem", "rb") as x:
    d_rsa = Crypto.PublicKey.RSA.importKey(x.read())
    verifer = Crypto.Signature.PKCS1_v1_5.new(d_rsa)
    msg_hash = Crypto.Hash.SHA256.new()
    msg_hash.update(str)
    verify = verifer.verify(msg_hash, sign)  # 使用公钥验证签名
    print(verify)

rsa

生成公私钥

import rsa

public, private = rsa.newkeys(1024)    # 生成公钥、私钥

with open("./private.pem", "wb") as x:  # 保存私钥
    x.write(private.save_pkcs1())
with open("./public.pem", "wb") as x:  # 保存公钥
    x.write(public.save_pkcs1())

private.pem

-----BEGIN RSA PRIVATE KEY-----
MIICYQIBAAKBgQCLolURP6bv3C/tP6VjhDQZW8hS4kA7RvSBUXpcJphXeOrU6M3D
pyoEeIT17ZKWcI69jqJoH7J+35v8jb3y6kACVGKihQuYKhDU4T+QL6566Jc7g9Cx
b4M+P9BLTVxY90X85cQ6mVQayZlUdyZP6bktoo/EclXc/d40Nt/+CApbFwIDAQAB
AoGBAIspymZabeH4LLvjBs5b4CC13LzeSO3LFbwcnDpjLMlg9YokS46Hd9Tk2H7R
u5q7q0c4dJwZmyffa/MUwC8TwPN5OHrdHm8JeHvZ7uOnHDW5KDcBCoiKIbCC7ViI
zXxD5NkyKZ/Sof8IuWrQ1sgKXdg58gkE3DGpv2zZgFAt8+vhAkUA4JqTgtyrzU+Z
v9IQGc4IdVNLA+WDMZkZy294zJYIc8bRB+5gm2cx6eZlqu2chTv0O16l4qa5jVkD
qX7uHIBK6t0J2sMCPQCfJx/M6Th0XewFCQcxozEt6rNqXXPLl0kwkftUm/3U2+ZM
w0DvG+rXbcKZH2aXsmhk8RPrCMTQJD9p8R0CRQCXcCHdriQ4fl9QMragb4bMdKiz
q+BVZME7paFxcAeq6HawPk3s+htxdeM+6I8MVmxKS3rV40xgvY/2jJrHpMjeqsKm
OQI8XQk6/sdrOCNbgIoJiYvTiPsFjO06bs5ue5Uq7ggOd2thEoiloeGZA/xm4KpJ
h+5ENpXzRdcnbmFyLJVFAkRtWtGGcWZHUqf1zjXE8nJeSQyVNbmA+3bI8tKFsApA
itohoV+pkRPH9sxjRQrBsCxXYIFb87IAEP4kiGPvMj7ZLHjlQw==
-----END RSA PRIVATE KEY-----

public.pem

-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAIuiVRE/pu/cL+0/pWOENBlbyFLiQDtG9IFRelwmmFd46tTozcOnKgR4
hPXtkpZwjr2Oomgfsn7fm/yNvfLqQAJUYqKFC5gqENThP5AvrnrolzuD0LFvgz4/
0EtNXFj3RfzlxDqZVBrJmVR3Jk/puS2ij8RyVdz93jQ23/4IClsXAgMBAAE=
-----END RSA PUBLIC KEY-----

读取公私钥

import rsa

with open("public.pem", 'rb') as x:
    public_key = rsa.PublicKey.load_pkcs1(x.read())
    print(public_key.n)
    print(public_key.e)

with open("private.pem", 'rb') as x:
    private_key = rsa.PrivateKey.load_pkcs1(x.read())
    print(private_key.n)
    print(private_key.d)

验证

公钥(n,e) 私钥(n,d)

from Crypto.Util.number import bytes_to_long, long_to_bytes

c = bytes_to_long(b'xiaoxiaoran')  # 145568744200917766156476782

e = 65537
n = 98054406985821460066911603492015436508222328814374109039765557792856783931929168543322947453890981964715321111180038798035875220954127258650951988858642038592569897228708251931577213665199206272026081143068775371001292827622859223963765601160574439687305128509021681199582745974505369805031375581229838129943
d = 97723753706851312781030536852228210796512651309026340937811116969013438199763272236685866547116031829460395100444564354982852830185082869616332404661301340214981730355677936849258187423827926814158044656867718148132598580321985363744279619391251894170459217295515011494314707777862815537663422883333275904993
m = 13886493648838710972140441482025058224090313231890577846399034902329916886695637737270404761882925259348133525185810448167361990827202097240482590034903652157080355065360915691138809161852288803959776946985306508273560998949496106417372434897047232906410711241839859082994232410306590851173666920567520980560

m = pow(c, d, n)  # m = c^d mod n
c = pow(m, e, n)  # c = m^e mod n

# print(m)  # 密文
print(long_to_bytes(c))

使用公私钥加解密

import rsa

str = b"xiaoxiaoran"
with open("public.pem", 'rb') as x:
    public_key = rsa.PublicKey.load_pkcs1(x.read())
    cipher_text = rsa.encrypt(str, public_key)  # 使用公钥加密

with open("private.pem", 'rb') as x:
    private_key = rsa.PrivateKey.load_pkcs1(x.read())
    text = rsa.decrypt(cipher_text, private_key)   # 使用私钥解密

print(text)

SHA256签名

import rsa

str = b"xiaoxiaoran"

with open("public.pem", 'rb') as x:
    public_key = rsa.PublicKey.load_pkcs1(x.read())
with open("private.pem", 'rb') as x:
    private_key = rsa.PrivateKey.load_pkcs1(x.read())
  
sign = rsa.sign(str, private_key, "SHA-256")  # 使用私钥进行'sha256'签名
verify = rsa.verify(str, sign, public_key)  # 使用公钥验证签名
print(verify)  # 签名正确则返回SHA-256

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-04-22 18:32:27  更:2022-04-22 18:34:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 17:22:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码