python密码学RSA密码解密教程
作者:菜鸟教程 时间:2023-07-29 17:01:47
本章是前一章的延续,我们使用RSA算法逐步实现加密,并详细讨论它.
用于解密密文的函数是as跟随 :
def decrypt(ciphertext, priv_key):
cipher = PKCS1_OAEP.new(priv_key)
return cipher.decrypt(ciphertext)
对于公钥加密或非对称密钥加密,重要的是要保持两个重要的功能,即身份验证和授权.
授权
授权是确认发件人是唯一发送邮件的人的过程.以下代码解释了这个 :
def sign(message, priv_key, hashAlg="SHA-256"):
global hash
hash = hashAlg
signer = PKCS1_v1_5.new(priv_key)
if (hash == "SHA-512"):
digest = SHA512.new()
elif (hash == "SHA-384"):
digest = SHA384.new()
elif (hash == "SHA-256"):
digest = SHA256.new()
elif (hash == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()
digest.update(message)
return signer.sign(digest)
身份验证
身份验证可以通过验证方法解释,如下所示去;
def verify(message, signature, pub_key):
signer = PKCS1_v1_5.new(pub_key)
if (hash == "SHA-512"):
digest = SHA512.new()
elif (hash == "SHA-384"):
digest = SHA384.new()
elif (hash == "SHA-256"):
digest = SHA256.new()
elif (hash == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()
digest.update(message)
return signer.verify(digest, signature)
验证数字签名以及发件人和收件人的详细信息.为安全起见,这会增加更多的权重年龄.
RSA密码解密
您可以使用以下代码进行RSA密码解密 :
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5
from Crypto import Random
from base64 import b64encode, b64decode
hash = "SHA-256"
def newkeys(keysize):
random_generator = Random.new().read
key = RSA.generate(keysize, random_generator)
private, public = key, key.publickey()
return public, private
def importKey(externKey):
return RSA.importKey(externKey)
def getpublickey(priv_key):
return priv_key.publickey()
def encrypt(message, pub_key):
cipher = PKCS1_OAEP.new(pub_key)
return cipher.encrypt(message)
def decrypt(ciphertext, priv_key):
cipher = PKCS1_OAEP.new(priv_key)
return cipher.decrypt(ciphertext)
def sign(message, priv_key, hashAlg = "SHA-256"):
global hash
hash = hashAlg
signer = PKCS1_v1_5.new(priv_key)
if (hash == "SHA-512"):
digest = SHA512.new()
elif (hash == "SHA-384"):
digest = SHA384.new()
elif (hash == "SHA-256"):
digest = SHA256.new()
elif (hash == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()
digest.update(message)
return signer.sign(digest)
def verify(message, signature, pub_key):
signer = PKCS1_v1_5.new(pub_key)
if (hash == "SHA-512"):
digest = SHA512.new()
elif (hash == "SHA-384"):
digest = SHA384.new()
elif (hash == "SHA-256"):
digest = SHA256.new()
elif (hash == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()
digest.update(message)
return signer.verify(digest, signature)
来源:https://www.it1352.com/OnLineTutorial/cryptography_with_python/cryptography_with_python_rsa_cipher_decryption.html
标签:python,密码学,RSA,解密
0
投稿
猜你喜欢
用js实现放大镜的效果的简单实例
2024-04-30 08:51:30
Spring数据库多数据源路由配置过程图解
2024-01-26 11:23:55
python3实现飞机大战
2023-03-03 00:36:18
Python包,__init__.py功能与用法分析
2021-06-17 22:19:54
Go语言中的流程控制结构和函数详解
2024-02-14 10:27:24
分布式DBA:SQL存储过程知识总结
2009-02-24 17:17:00
js获取上传文件大小示例代码
2024-04-22 22:35:02
端午节将至,用Python爬取粽子数据并可视化,看看网友喜欢哪种粽子吧!
2023-08-23 06:29:31
Python字典取键、值对的方法步骤
2021-12-12 15:26:12
Python通过len函数返回对象长度
2023-05-05 06:04:12
疯狂上涨的Python 开发者应从2.x还是3.x着手?
2021-10-25 16:41:54
CentOS7.4 源码安装MySQL8.0的教程详解
2024-01-20 01:44:47
Python:slice与indices的用法
2021-09-10 22:31:09
mysql启动错误之mysql启动报1067错误解决方法
2024-01-26 07:52:56
ie和firefox中css自动换行实现方法
2008-04-08 12:49:00
python中的Json模块dumps、dump、loads、load函数用法详解
2023-11-09 20:01:30
pytorch中的卷积和池化计算方式详解
2021-03-31 19:26:32
PyTorch中关于tensor.repeat()的使用
2023-06-26 07:13:35
Mootools 1.2教程(7)——设置和获取样式表属性
2008-11-25 13:48:00
python判断一个集合是否包含了另外一个集合中所有项的方法
2022-01-15 19:08:06