python怎么给rsa加密(Python生成rsa密钥对操作示例)
类别:脚本大全 浏览量:910
时间:2021-10-15 00:48:31 python怎么给rsa加密
Python生成rsa密钥对操作示例本文实例讲述了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:关于加密解密感兴趣的朋友还可以参考本站在线工具:
md5在线加密工具:https://tool.zzvips.com/t/md5/
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/xuezhangjun0121/article/details/84388840
您可能感兴趣
- python里字典常用方法(Python使用字典的嵌套功能详解)
- python 爬虫招聘(Python3获取拉勾网招聘信息的方法实例)
- python中for语句的无限循环(python使用for循环计算0-100的整数的和方法)
- pythonpandas提取列索引(Python3.5 Pandas模块缺失值处理和层次索引实例详解)
- python如何使用yield(YII2.0框架行为Behavior深入详解)
- python云服务技术(Python脚本修改阿里云的访问控制列表的方法)
- python中字典的常用操作(11个Python3字典内置方法大全与示例汇总)
- python对象创建流程(Python3.5面向对象与继承图文实例详解)
- python符串操作教程(Python3.5运算符操作实例详解)
- python 循环遍历新方法(python for 循环获取index索引的方法)
- python实例教程(Python魔法方法详解)
- 如何使用github中的python库(使用GitHub和Python实现持续部署的方法)
- python中test函数用法(Python TestCase中的断言方法介绍)
- python中tryexcept的用法(对python中的try、except、finally 执行顺序详解)
- python读取和写入数据excel(Python向excel中写入数据的方法)
- python3安装编程环境(Python3 修改默认环境的方法)
- 以家人之名广受好评,剧情生动引起观众共鸣,演员张新成圈粉无数(以家人之名广受好评)
- 三兄妹感情再遇波折,人设接连崩塌 《以家人之名》剧情猜不透(三兄妹感情再遇波折)
- 《小敏家》金波想要复婚 这只是他圈套的第1步,更可恶的在后面(小敏家金波想要复婚)
- 小敏家 剧情离谱一锅乱炖,但他们俩绝对是这部剧的一大 亮点(剧情离谱一锅乱炖)
- 《倚天屠龙记》再遭翻拍,关晓彤主演赵敏,蒋劲夫演张无忌,你怎么看(倚天屠龙记再遭翻拍)
- 吴启华与曾舜晞两代张无忌同框,戏里经典的他却没活出原著的潇洒(吴启华与曾舜晞两代张无忌同框)
热门推荐
- python程序怎么打包(Python 实现文件打包、上传与校验的方法)
- tomcat运行警告(tomcat异常解决Invalid character found in the request target. The valid characters are defined in)
- python表白代码演示(python3实现表白神器)
- linux模糊删除多个文件(Linux删除文件不同方法效率对比)
- flask快速开发框架(手把手教你使用Flask搭建ES搜索引擎预备篇)
- 已授权和未授权(提示您未被授权查看该页怎么解决?)
- vue-cli4开发多页面应用(深入理解Vue-cli4路由配置)
- drools机制(Swoole实现异步投递task任务案例详解)
- 如何编写错误日志才更容易排查问题
- mysql8.0.25安装教程(Mysql8.0.17安装教程推荐)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9