0

Here's my code, I'm trying to make a Fernet decoder but whenever I try to run the program I get this error:

ValueError: Fernet key must be 32 URL-safe base64-encoded bytes.
import dearpygui.dearpygui as dpg
from cryptography.fernet import Fernet
import base64

def GetCrypto():
   Crypt = dpg.get_value("CG")
   Pass =  dpg.get_value("CP")
   fernet = Fernet(Pass)


   decMessage = fernet.decrypt(Crypt).decode('utf-8')
   print(decMessage)


dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()
dpg.create_viewport(title='Custom Title', width=600, height=200)
with dpg.window(label="KEIKAS", width=600, height=250):
    dpg.add_input_text(tag="CG", label="Cryptografy")
    dpg.add_input_text(tag="CP", label="Cryptografy Password")
    dpg.add_button(label="Decrypt", callback = GetCrypto)
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

key: b'KnX5YN3hvP54jOIMkacWdqxFX1RKk8cjqVZjGJbAscM='

KEIKAS
  • 11
  • 2
  • always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. – furas Sep 10 '22 at 20:43
  • you import `base64` but you never use `base64.b64encode()` to create `URL-safe base64-encoded bytes` – furas Sep 10 '22 at 20:43
  • 1
    you could reduce code and remove all `dearpygui` because it is not important in this problem. You could also put example data directly in code (without GUI) – furas Sep 10 '22 at 20:46

1 Answers1

1

I have no idea what values you put in your GUI but this works for me:

Maybe you used wrong values in wrong Input Text.
OR maybe you forgot to enocode string to bytes.

from cryptography.fernet import Fernet

Pass   = b'KnX5YN3hvP54jOIMkacWdqxFX1RKk8cjqVZjGJbAscM='
#Pass   = 'KnX5YN3hvP54jOIMkacWdqxFX1RKk8cjqVZjGJbAscM='.encode('utf-8')
fernet = Fernet(Pass)

text = "Hello World!"

Crypto = fernet.encrypt(text.encode('utf-8')).decode('utf-8')
print('Crypto:', Crypto)

decMessage = fernet.decrypt(Crypto.encode('utf-8')).decode('utf-8')
print('decMessage:', decMessage)

Result:

Crypto: gAAAAABjHPqIoajV-v1V7pginMhkwFBtJfZCLpJjmsT7AxHpc3guBX5otRqJWK5gCWxLh9Iy_9LhoQTzk3en2DDr_OwcWT276A==
decMessage: Hello World!
furas
  • 134,197
  • 12
  • 106
  • 148