Python生成rsa密钥对操作示例
作者:xuezhangjun 时间:2021-08-25 03:43:31
本文实例讲述了Python生成rsa密钥对操作。分享给大家供大家参考,具体如下:
# -*- coding: utf-8 -*-
import rsa
# 先生成一对密钥,然后保存.pem格式文件,当然也可以直接使用
(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 用私钥签名认证、再用公钥验证签名
signature = rsa.sign(message, privkey, 'SHA-1')
rsa.verify('lovesoo.org', signature, pubkey)
对文件进行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:关于加密解密感兴趣的朋友还可以参考本站在线工具:
在线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
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/xuezhangjun0121/article/details/84388840
标签:Python,rsa,密钥对
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Django模板层过滤器和继承的问题
2023-02-08 06:28:04
![](https://img.aspxhome.com/file/2023/0/79400_0s.png)
Python实现新年愿望代码雨效果
2022-08-02 00:52:35
![](https://img.aspxhome.com/file/2023/6/72186_0s.jpg)
Go语言流程控制详情
2023-10-16 13:16:24
图文详解如何在WordPress中嵌入iFrame
2023-06-12 23:58:02
![](https://img.aspxhome.com/file/2023/6/55526_0s.png)
SQL查询入门(中篇)
2011-09-30 11:15:09
学习ASP的理由 分析小结
2011-02-26 10:54:00
python实现事件驱动
2022-01-07 10:20:48
MySQL分页优化解析
2008-12-22 14:56:00
[译]艺术和设计的差异 (1)
2009-09-25 12:38:00
深入解析pandas数据聚合和重组
2023-11-17 17:45:51
![](https://img.aspxhome.com/file/2023/4/75154_0s.png)
PHP基于phpqrcode生成带LOGO图像的二维码实例
2023-11-23 23:47:18
scrapy与selenium结合爬取数据(爬取动态网站)的示例代码
2023-07-14 00:17:08
![](https://img.aspxhome.com/file/2023/7/79697_0s.png)
如何实现在下拉菜单里输入文字?
2010-06-03 10:31:00
IE window对象介绍
2008-05-21 18:47:00
利用Python写一个爬妹子的爬虫
2021-07-22 12:44:51
![](https://img.aspxhome.com/file/2023/1/72931_0s.png)
如何利用Python模拟GitHub登录详解
2023-11-18 11:08:05
![](https://img.aspxhome.com/file/2023/4/64354_0s.png)
Linux中大内存页Oracle数据库优化的方法
2023-07-19 11:24:57
![](https://img.aspxhome.com/file/2023/6/64436_0s.jpg)
数据库备份过程中经常遇到的九种情况
2008-12-26 16:38:00
Python Django框架中表单的用法详解
2021-11-06 04:24:41
![](https://img.aspxhome.com/file/2023/4/80534_0s.png)
Javascript函数类型判断解决方案
2009-08-27 15:32:00