Python RSA
# -*- 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
Python RSA的更多相关文章
- (转)Python rsa 签名与验证 sign and verify
转自:http://wawehi.blog.163.com/blog/static/143780306201371361120515/ 网上一搜一大把的 python rsa 相关的东西,python ...
- python RSA加密、解密、签名
python RSA加密.解密.签名 python中用于RSA加解密的库有好久个,本文主要讲解rsa.M2Crypto.Crypto这三个库对于RSA加密.解密.签名.验签的知识点. 知识基础 加密是 ...
- python rsa 加密解密 (编解码,base64编解码)
最近有需求,需要研究一下RSA加密解密安全:在网上百度了一下例子文章,很少有文章介绍怎么保存.传输.打印加密后的文本信息,都是千篇一律的.直接在一个脚本,加密后的文本信息赋于变量,然后立马调用解密.仔 ...
- Python rsa公私钥生成 rsa公钥加解密(分段加解密)-私钥加签验签实战
一般现在的SAAS服务提供现在的sdk或api对接服务都涉及到一个身份验证和数据加密的问题.一般现在普遍的做法就是配置使用非对称加密的方式来解决这个问题,你持有SAAS公司的公钥,SAAS公司持有你的 ...
- python RSA加密解密及模拟登录cnblog
1.公开密钥加密 又称非对称加密,需要一对密钥,一个是私人密钥,另一个则是公开密钥.公钥加密的只能私钥解密,用于加密客户上传数据.私钥加密的数据,公钥可以解密,主要用于数字签名.详细介绍可参见维基百科 ...
- rsa字符串格式公钥转换python rsa库可识别的公钥形式
在爬虫分析的时候,经常在网页上看到如下格式的rsa公钥: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21 ...
- Python RSA操作
公钥加密.私钥解密 # -*- coding: utf-8 -*- import rsa # rsa加密 def rsaEncrypt(str): # 生成公钥.私钥 (pubkey, privkey ...
- python rsa 加密
rsa 非对称加密, 公钥加密, 私钥解密, 有公钥无法推导出私钥, 私钥保密 import rsa n = 1024 # n 越大生成公钥, 秘钥及加密解密所需时间就越长 key = rsa.new ...
- python RSA 加密与签名
PyCrypto装起来就简单多了,我是直接 sudo easy_install pycrypto 直接搞定的 先生成rsa的公私钥:打开控制台,输入 openssl 再输入 genrsa -out p ...
随机推荐
- Numerical Testing Reportes of A New Conjugate Gradient Projection Method for Convex Constrained Nonlinear Equations
Numerical Testing Reportes of A New Conjugate Gradient Projection Method for Convex Constrained Nonl ...
- Post方式 前后端分离开发postman工具首次使用心得及注意事项
使用前:2009年以前,一直用asp(非asp.net)语言开发网站,网页调用数据等操作,是通过asp标签<%%>嵌入到HTML标签语言中.相隔八年后,听说最近都是MVC后又什么前后端分离 ...
- WAF的基础绕过
方法分类: 1.HTTP参数污染绕过 2.HTTP Header头部欺骗绕过 3.HTTP参数溢出绕过 4.HTTP分块传输绕过 5.HTTP数据编码绕过 6.HTTP Pipline绕过(Keep- ...
- Vue中在template标签中进行判断时注意比较元素
(一)比较的元素,一个是data元素,另外一个是常量,如下图所示: 编译正常,运行正常,效果在期望中,会显示Hello World,结果如下: (二)比较的元素,一个是data元素,另外一个是cons ...
- magento2.2.3 根据产品ID获取栏目名称的正确调用方式
根据product_id 获取 category_ids : /** * @param $product_id * @return array */ public function mc_getCat ...
- 使用SSM 或者 springboot +mybatis时,对数据库的认证信息(用户名,密码)进行加密。
通常情况下,为了提高安全性,我们需要对数据库的认证信息进行加密操作,然后在启动项目的时候,会自动解密来核对信息是否正确.下面介绍在SSM和springboot项目中分别是怎样实现的. 无论是使用SSM ...
- 使用vegrant安装centos7
1.首先去安装需要先安装好 Vagrant 和 VirtualBox. 安装好需要重启电脑. 可能网速会很慢,建议复制链接到迅雷下载,或者国内随便找个下载也可以. 2.在电脑创建vagrant_vm目 ...
- python笔记09
今日内容 三元运算 函数 内容详细 三元运算(三目运算) v = 前面 if 条件 else 后面 if 条件: v = '前面' else: v = '后面' # 让用户输入值,如果值是整数,则转换 ...
- JavaScript:JSON对象
一.JSON对象概念 JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧. JSON 是 JavaScript 原生格式,这意味着在 JavaScri ...
- 141.内置上下文处理器debug、request、auth、messages、media、static、csrf
上下文处理器 上下文处理器可以返回一些数据,在全局模板中都可以使用,比如登录后的用户数据,在很多页面中都需要使用,那么我们就可以方在上下文处理器中,就没有必要在每个视图中返回这个对象了. 在setti ...