Python RSA
2024-08-29 05:06:47
# -*- coding: utf-8 -*- from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
import base64 # 加密解密:公钥加密,私钥解密
#
# 签名验签:私钥签名,公钥验签
#
# 生成 private key and pulic key
print "1、生成 private key and pulic key" # 伪随机数生成器
random_generator = Random.new().read
# rsa算法生成实例
rsa = RSA.generate(1024, random_generator) # master的秘钥对的生成
private_pem = rsa.exportKey() with open('master-private.pem', 'w') as f:
f.write(private_pem) public_pem = rsa.publickey().exportKey()
with open('master-public.pem', 'w') as f:
f.write(public_pem) # ghost的秘钥对的生成
private_pem = rsa.exportKey()
with open('ghost-private.pem', 'w') as f:
f.write(private_pem) public_pem = rsa.publickey().exportKey()
with open('ghost-public.pem', 'w') as f:
f.write(public_pem) # 加密和解密
print "2、加密和解密"
# Master使用Ghost的公钥对内容进行rsa 加密 message = 'hello ghost, this is a plian text'
print "message: " + message
with open('ghost-public.pem') as f:
key = f.read()
rsakey = RSA.importKey(key)
cipher = Cipher_pkcs1_v1_5.new(rsakey)
cipher_text = base64.b64encode(cipher.encrypt(message))
print "加密(encrypt)"
print cipher_text # Ghost使用自己的私钥对内容进行rsa 解密 with open('ghost-private.pem') as f:
key = f.read()
rsakey = RSA.importKey(key)
cipher = Cipher_pkcs1_v1_5.new(rsakey)
text = cipher.decrypt(base64.b64decode(cipher_text), random_generator) print "解密(decrypt)"
print "message:" + text assert text == message, 'decrypt falied' # 签名与验签
print "3、 签名与验签" # Master 使用自己的私钥对内容进行签名
print "签名"
with open('master-private.pem') as f:
key = f.read()
rsakey = RSA.importKey(key)
signer = Signature_pkcs1_v1_5.new(rsakey)
digest = SHA.new()
digest.update(message)
sign = signer.sign(digest)
signature = base64.b64encode(sign) print signature print "验签"
with open('master-public.pem') as f:
key = f.read()
rsakey = RSA.importKey(key)
verifier = Signature_pkcs1_v1_5.new(rsakey)
digest = SHA.new()
# Assumes the data is base64 encoded to begin with
digest.update(message)
is_verify = verifier.verify(digest, base64.b64decode(signature)) print is_verify
【转】https://blog.csdn.net/ts__cf/article/details/47862911
最新文章
- c# 游戏策划配置工具
- Android拓展系列(9)--Android视频录制screenrecord命令
- Google Chrome开发者工具
- LeetCode.4 两个有序数组的中位数问题
- 布局神器display:table-cell
- 高德开发 android 出现 key 鉴权失败
- LoadImage()的使用
- NET 2016
- 期望dp专题
- Lambda表达式补充
- WebSocket 和 Golang 实现聊天功能
- mongodb导出数据到csv
- Click One客户端安装后将安装目录删除,再从服务器下载安装无法安装解决办法
- Linux常用命令-文本查看篇
- Consul的应用
- linux 中mv命令
- [Octave] fminunc()
- Java RSA公钥加密,私钥解密算法的尝试
- matplotlib ----- 清空图片
- 编写高质量代码改善C#程序的157个建议——建议62:避免嵌套异常
热门文章
- Node.js文档-os
- 在 Nest.js 中使用 MongoDB 与 TypeORM
- JS中0.1+0.2!=0.3
- header.vue 调用变量,别的组件导入引用,组件方法事例实例
- Android中的Service基础
- 嵊州D2T4 十七个中毒的英国人 poisoning
- 剑指offer-面试题18-删除链表的节点-链表
- Maven修改test/rsource的output folder报错Test source folder 'src/test/java'... is not also used for main s
- ASP.NET常用内置对象(一)Request
- layui的跳转链接实现分页low