- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python3非对称加密算法RSA实例详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了python3非对称加密算法rsa。分享给大家供大家参考,具体如下:
python3 可以使用 crypto.publickey.rsa 和 rsa 生成公钥、私钥.
其中 python3.6 crypto 库的安装方式请参考前面一篇《python3对称加密算法aes、des3》 。
rsa 加解密的库使用 pip3 install rsa 就行了 。
c:\windows\system32>pip3 install rsa collecting rsa downloading https://files.pythonhosted.org/packages/e1/ae/baedc9cb175552e95f3395c43055a6a5e125ae4d48a1d7a924baca83e92e/rsa-3.4.2-py2.py3-none-any.whl (46kb) 100% |████████████████████████████████| 51kb 99kb/s collecting pyasn1>=0.1.3 (from rsa) downloading https://files.pythonhosted.org/packages/a0/70/2c27740f08e477499ce19eefe05dbcae6f19fdc49e9e82ce4768be0643b9/pyasn1-0.4.3-py2.py3-none-any.whl (72kb) 100% |████████████████████████████████| 81kb 289kb/s installing collected packages: pyasn1, rsa successfully installed pyasn1-0.4.3 rsa-3.4.2 。
使用 crypto.publickey.rsa 生成公钥、私钥:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import
crypto.publickey.rsa
import
crypto.random
x
=
crypto.publickey.rsa.generate(
2048
)
a
=
x.exportkey(
"pem"
)
# 生成私钥
b
=
x.publickey().exportkey()
# 生成公钥
with
open
(
"a.pem"
,
"wb"
) as x:
x.write(a)
with
open
(
"b.pem"
,
"wb"
) as x:
x.write(b)
y
=
crypto.publickey.rsa.generate(
2048
, crypto.random.new().read)
# 使用 crypto.random.new().read 伪随机数生成器
c
=
y.exportkey()
# 生成私钥
d
=
y.publickey().exportkey()
#生成公钥
with
open
(
"c.pem"
,
"wb"
) as x:
x.write(c)
with
open
(
"d.pem"
,
"wb"
) as x:
x.write(d)
|
使用 crypto.publickey.rsa.importkey(private_key) 生成公钥和证书:
1
2
3
4
5
6
7
8
9
|
import
crypto.publickey.rsa
with
open
(
"a.pem"
,
"rb"
) as x:
xx
=
crypto.publickey.rsa.importkey(x.read())
b
=
xx.publickey().exportkey()
# 生成公钥
with
open
(
"b.pem"
,
"wb"
) as x:
x.write(b)
a
=
xx.exportkey(
"der"
)
# 生成 der 格式的证书
with
open
(
"a.der"
,
"wb"
) as x:
x.write(a)
|
使用 rsa 生成公钥、私钥:
1
2
3
4
5
6
7
8
|
import
rsa
f, e
=
rsa.newkeys(
2048
)
# 生成公钥、私钥
e
=
e.save_pkcs1()
# 保存为 .pem 格式
with
open
(
"e.pem"
,
"wb"
) as x:
# 保存私钥
x.write(e)
f
=
f.save_pkcs1()
# 保存为 .pem 格式
with
open
(
"f.pem"
,
"wb"
) as x:
# 保存公钥
x.write(f)
|
rsa非对称加密算法实现:
使用crypto模块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import
crypto.publickey.rsa
import
crypto.cipher.pkcs1_v1_5
import
crypto.random
import
crypto.signature.pkcs1_v1_5
import
crypto.
hash
y
=
b
"abcdefg1234567"
with
open
(
"b.pem"
,
"rb"
) as x:
b
=
x.read()
cipher_public
=
crypto.cipher.pkcs1_v1_5.new(crypto.publickey.rsa.importkey(b))
cipher_text
=
cipher_public.encrypt(y)
# 使用公钥进行加密
with
open
(
"a.pem"
,
"rb"
) as x:
a
=
x.read()
cipher_private
=
crypto.cipher.pkcs1_v1_5.new(crypto.publickey.rsa.importkey(a))
text
=
cipher_private.decrypt(cipher_text, crypto.random.new().read)
# 使用私钥进行解密
assert
text
=
=
y
# 断言验证
with
open
(
"c.pem"
,
"rb"
) as x:
c
=
x.read()
c_rsa
=
crypto.publickey.rsa.importkey(c)
signer
=
crypto.signature.pkcs1_v1_5.new(c_rsa)
msg_hash
=
crypto.
hash
.sha256.new()
msg_hash.update(y)
sign
=
signer.sign(msg_hash)
# 使用私钥进行'sha256'签名
with
open
(
"d.pem"
,
"rb"
) as x:
d
=
x.read()
d_rsa
=
crypto.publickey.rsa.importkey(d)
verifer
=
crypto.signature.pkcs1_v1_5.new(d_rsa)
msg_hash
=
crypto.
hash
.sha256.new()
msg_hash.update(y)
verify
=
verifer.verify(msg_hash, sign)
# 使用公钥验证签名
print
(verify)
|
运行结果:
true 。
使用 rsa 模块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
rsa
y
=
b
"abcdefg1234567"
with
open
(
"e.pem"
,
"rb"
) as x:
e
=
x.read()
e
=
rsa.privatekey.load_pkcs1(e)
# load 私钥
with
open
(
"f.pem"
,
"rb"
) as x:
f
=
x.read()
f
=
rsa.publickey.load_pkcs1(f)
# load 公钥,由于之前生成的私钥缺少'rsa'字段,故无法 load
cipher_text
=
rsa.encrypt(y, f)
# 使用公钥加密
text
=
rsa.decrypt(cipher_text, e)
# 使用私钥解密
assert
text
=
=
y
# 断言验证
sign
=
rsa.sign(y, e,
"sha-256"
)
# 使用私钥进行'sha256'签名
verify
=
rsa.verify(y, sign, f)
# 使用公钥验证签名
print
(verify)
|
运行结果:
true 。
希望本文所述对大家python程序设计有所帮助.
原文链接:https://blog.csdn.net/qq_42486920/article/details/80863322 。
最后此篇关于Python3非对称加密算法RSA实例详解的文章就讲到这里了,如果你想了解更多关于Python3非对称加密算法RSA实例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我目前正在使用 Crypto++ 为数据块生成签名。我希望签名是 20 个字节(SHA 1 Hash),因为我的理解是签名只是一个加密的哈希。但是当检查 maxsignaturelength 和 si
我不是加密专家,所以我对这些事情的了解接近于零。我必须与使用 RSA 加密的系统进行互操作。使用他们的 key 时,我遇到了为相同的输入/ key 获取不同密码的问题。图书馆是https://code
我想利用 postman 来测试需要使用 RSA 加密对输入字段之一进行加密的 REST API。 我看到 postman 通过 require('crypto-js') 提供了功能使用 AES 加密
我正在尝试在我的 (Java) 应用程序中实现一个(简化的)类似 RSA 的验证过程。客户端发送一个请求(数据+私钥签名),服务器要么拒绝他的请求,要么处理它——取决于签名的有效性。 但是我不明白验证
我正在尝试在我的 (Java) 应用程序中实现一个(简化的)类似 RSA 的验证过程。客户端发送一个请求(数据+私钥签名),服务器要么拒绝他的请求,要么处理它——取决于签名的有效性。 但是我不明白验证
下面是我想要做的最小、完整且可验证的示例。 基本上我想实现一个集成一些 CUDA 代码的 OpenSSL RSA 引擎。 CUDA 部分应该执行模幂运算,但在本例中并不重要,因此我只是按顺序使用了 B
是否有任何非常简单的跨平台 C++ 库可以进行不对称加密?不需要高效,只要工作。我想它可能只是 .h 文件中的 3-4 个函数,它们可以执行任意精度的数学运算,仅此而已。 我相信在这里使用 OpenS
我使用以下命令创建了私钥和公钥, openssl genrsa -out privatekey.pem 1024 openssl req -new -x509 -key privatekey.pem
我在 .net 环境(所有版本)中工作并使用 vb.net。我想根据密码生成 RSA 公钥和私钥。 我对 RSA 算法的理解仅限于使用 .net 提供的类,即 System.Security.Cryp
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我有 ssh-keygen 生成的 id_rsa.pub key 。 如何以编程方式将 id_rsa.pub 文件转换为 RSA DER 格式的 key ? 最佳答案 如果使用 ssh-keygen
我在 JWT(JSON Web Token)方案的帮助下实现了一个登录系统。基本上,在用户登录/登录后,服务器对 JWT 进行签名并将其传递给客户端。 然后客户端在每个请求中返回 token ,服务器
我使用的是 WAS 6.1,我的服务器过去可以正常启动,但突然间无法正常工作.. 当我尝试在我的本地 RSA 中启动我的服务器时。我收到以下错误 我已经重新启动了系统,RSA,杀死了所有 Java 进
我正在开发一个支付网关,他们有一个正在运行的 Java 演示,但我想用 php 来实现它。 支付网关使用 3DES 和随机生成的 key 来加密有效负载。该 key 使用支付网关的公钥通过 RSA 进
这是此处未回答问题的副本:Using an RSA Public Key to decrypt a string that was encrypted using RSA Private Key 您可
我一直无法将 RSA 加密字节编码为字符串,然后在另一端(远程主机)将它们恢复为字节,另一端抛出“错误数据”异常。 我试过谷歌,没有运气。 到目前为止我的代码: 客户: array^Test=Enco
我需要一些帮助来解决我的问题。 问题:我想用 Android 平台的公共(public) RSA key 加密一个数字 (A),然后用私钥在 PHP 服务器上解密它。在每个平台上,我都可以加密和解密数
当我尝试将参数传递给函数时出现以下错误。 error[E0243]: wrong number of type arguments: expected 1, found 0 --> src/mai
我尝试将公共(public) RSA key 加载到我的程序中。我在 C 中使用 openssl 库。 key 在 header crypt.h 文件中定义: #define PUBLIC_KEY
Bouncy CaSTLe 加密库中有两种不同的密码可以传递给 PKCS1Encoding:NativeRSAEngine 和 RSAEngine。这两个变体之间有区别吗? 编辑: 正如 Maarte
我是一名优秀的程序员,十分优秀!