gpt4 book ai didi

python - 如何在Python中使用变量进行加密和解密

转载 作者:行者123 更新时间:2023-12-05 07:21:58 51 4
gpt4 key购买 nike

我的目标是用 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 加密不适合我的项目目标,也许我应该使用其他加密/库?

最佳答案

您实际上并没有将 encrypt 分配给一个变量,您是在发送一个 encrypt 函数作为 decrypt 函数的参数。在代码末尾进行此更改:

encrypted = encrypt((str(Nonce)), password=(str(publickey)))  # Encrypting nonce with hashed pub.key
decrypted = decrypt((str(encrypted)), password=(str(publickey)))

print("This is the encrypted", encrypted)
print("This is the decrypted", decrypted)

decrypted 的输出将是您加密的 Nonce。

关于python - 如何在Python中使用变量进行加密和解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56703722/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com