import base64 as b64
from cryptography.fernet import Fernet
def encrypt(data):
key = Fernet.generate_key()
keyb64 = b64.urlsafe_b64encode(key).decode('utf-8')
cipher = Fernet(key)
encd_data = cipher.encrypt(data.encode())
encd_datab64 = b64.urlsafe_b64encode(encd_data).decode('utf-8')
return encd_datab64, keyb64
print('Your data:' + encrypt(input('what to encrypt\n')))
I want it to print out:
Your code: (encd_datab64), your key: (keyb64)
In the code above, it creates an error that says you cannot separate a tuple, but I don’t know what a tuple is. If I delete the 'Your data:' + part of the code, it just gives encrypted data and text.
>Solution :
I understand your requirement. It seems you want to print the encrypted data and the key in a specific format. You can achieve this by modifying the print statement to include the formatted strings. Here’s the updated code:
import base64 as b64
from cryptography.fernet import Fernet
def encrypt(data):
key = Fernet.generate_key()
keyb64 = b64.urlsafe_b64encode(key).decode('utf-8')
cipher = Fernet(key)
encd_data = cipher.encrypt(data.encode())
encd_datab64 = b64.urlsafe_b64encode(encd_data).decode('utf-8')
return encd_datab64, keyb64
encrypted_data, key = encrypt(input('Enter data to encrypt: '))
print(f'Your code: {encrypted_data}, your key: {key}')
In this code, I’ve used f-strings (formatted strings) to include the values of encrypted_data and key in the print statement. This way, it prints the desired output in the format you specified.