【发布时间】:2019-11-04 07:43:10
【问题描述】:
我的目标是用 Python 编写代码,执行以下操作: 1.生成公钥。 2. 散列公钥。 3. 生成一个随机字符串。 4. 使用散列公钥对随机字符串进行加密解密。
这是我写的代码:
from Crypto.PublicKey import RSA
import random
import string
import hashlib
import base64
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
def encrypt(plaintext, password):
f = Fernet(base64.urlsafe_b64encode(
PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=b'abcd', iterations=1000,
backend=default_backend()).derive(password.encode())))
return f.encrypt(plaintext.encode()).decode()
def decrypt(ciphertext, password):
f = Fernet(base64.urlsafe_b64encode(
PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=b'abcd', iterations=1000,
backend=default_backend()).derive(password.encode())))
return f.decrypt(ciphertext.encode()).decode()
def randomString(strlength = 16): #Random code
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(strlength))
key = RSA.generate(2048) # Private key creation
code = 'nooneknows'
privatekey = key.exportKey(passphrase=code, pkcs=8)
publickey = key.publickey().exportKey()
result = hashlib.md5(publickey) #Hashing the Publickey
publickey = result.digest()
Nonce=randomString() # Creating Nonce
encrypt((str(Nonce)), password=(str(publickey))) # Encrypting nonce with hashed pub.key
decrypt((str(encrypt)), password=(str(publickey)))
print("This is the decrption", encrypt)
print("This is the decrption", decrypt)
当我运行它时,我得到了错误:
D:\Anaconda3\python.exe C:/Users/AVIV/.PyCharmCE2019.1/config/scratches/test.py
Traceback (most recent call last):
File "D:\Anaconda3\lib\site-packages\cryptography\fernet.py", line 87, in _get_unverified_token_data
data = base64.urlsafe_b64decode(token)
File "D:\Anaconda3\lib\base64.py", line 133, in urlsafe_b64decode
return b64decode(s)
File "D:\Anaconda3\lib\base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/AVIV/.PyCharmCE2019.1/config/scratches/test.py", line 39, in <module>
decrypt((str(encrypt)), password=(str(publickey)))
File "C:/Users/AVIV/.PyCharmCE2019.1/config/scratches/test.py", line 21, in decrypt
return f.decrypt(ciphertext.encode()).decode()
File "D:\Anaconda3\lib\site-packages\cryptography\fernet.py", line 74, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "D:\Anaconda3\lib\site-packages\cryptography\fernet.py", line 89, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
有没有办法解决这个错误? 我的猜测是,问题出在字节的解码和编码上。我尝试对其进行多次解码/编码,但总是以错误告终。 另外,我猜它的填充有问题,但我不知道如何解决它。 我在想,也许 Fernet 加密不适合我的项目目标,也许我应该使用其他加密/库?
【问题讨论】:
-
您能否提供该错误的完整回溯(将其编辑到问题中;不要尝试将其放在评论中)?此外,您可能可以删除很多代码,同时仍然留下一个重现错误的完整示例。
-
@glibdud 感谢您的回复。我添加了我收到的错误的完整回溯。我也尽量缩短它。抱歉,如果它仍然太长。
-
我认为您在这里混淆了两种加密方法。 RSA 密钥对用于非对称加密,发送方使用接收方的公钥加密数据,接收方使用他们的私钥解密。这里你使用的是Fernet,它进行对称加密,所以加密和解密使用相同的密钥,这个密钥必须是只有发送者和接收者知道的秘密。这个秘密基于任何一个人的公共 RSA 密钥是没有意义的。
-
@SamuelRice 感谢您的回复,塞缪尔。事实上,你是对的。如果第三方用户/黑客可以访问公钥(这是众所周知的),他将能够解密消息,并且连接将不会受到保护。然而,这只是一个测试项目,这里的错误是代码本身,我的目标只是用散列的公钥加密和解密一个字符串。但是您可以使用私钥进行解密,然后连接将受到保护:)
标签: python encryption