- 教程
- 教程
- 双倍强度加密(Double Strength Encryption)
- Python Overview and 安装
- Reverse Cipher
- Caesar Cipher
- ROT13算法(ROT13 Algorithm)
- 换位密码(Transposition Cipher)
- 转置密码的加密(Encryption of Transposition Cipher)
- 换位密码的解密(Decryption of Transposition Cipher)
- 加密文件(Encryption of files)
- 解密文件(Decryption of files)
- Base64编码和解码(Base64 Encoding & Decoding)
- XOR Process
- 乘法密码(Multiplicative Cipher)
- Affine Ciphers
- 黑客单字母密码(Hacking Monoalphabetic Cipher)
- 简单的替代密码(Simple Substitution Cipher)
- 简单替换密码的测试(Testing of Simple Substitution Cipher)
- 简单替换密码的解密(Decryption of Simple Substitution Cipher)
- Python Modules of Cryptography
- 了解Vigenere Cipher(Understanding Vignere Cipher)
- 实现Vigenere Cipher(Implementing Vignere Cipher)
- 一次性密码(One Time Pad Cipher)
- 一次性密码密码的实现(Implementation of One Time Pad Cipher)
- 对称和非对称密码学(Symmetric & Asymmetric Cryptography)
- 理解RSA算法(Understanding RSA Algorithm)
- 创建RSA密钥(Creating RSA Keys)
- RSA密码加密(RSA Cipher Encryption)
- RSA密码解密(RSA Cipher Decryption)
- 黑客攻击RSA密码(Hacking RSA Cipher)
- 资源
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
RSA密码解密(RSA Cipher Decryption)
本章是前一章的延续,我们使用RSA算法逐步实现加密,并详细讨论它。
用于解密密文的函数如下 -
def decrypt(ciphertext, priv_key):
cipher = PKCS1_OAEP.new(priv_key)
return cipher.decrypt(ciphertext)
对于公钥加密或非对称密钥加密,重要的是保持两个重要的功能,即Authentication和Authorization 。
授权 (Authorization)
授权是确认发件人是唯一传送邮件的人的过程。 以下代码解释了这一点 -
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)
身份验证 (Authentication)
验证方法可以通过验证方法进行验证,如下所述 -
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论