OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python 3 Fernet Decryption Succeeds with Slightly Modified Encryption Key

  • Thread starter Thread starter OGM
  • Start date Start date
O

OGM

Guest
Code:
def encrypt_message(message, key) -> bytes:
  """Encrypts a message using Fernet."""
  f = Fernet(key)
  encrypted_message = f.encrypt(message.encode())
  return encrypted_message


def decrypt_message(encrypted_message, key) -> str:
  """Decrypts a message using Fernet."""
  f = Fernet(key)
  decrypted_message = f.decrypt(encrypted_message).decode()
  return decrypted_message


message_ = "This is a secret message!"

encrypted = encrypt_message(message_, b"HeYt_XX4wRLvnDP9fC9-7hlMgasQ270tpWSrjKy0jRE=")
# The encrypted message will change in every encryption process which is OK
print("Encrypted:", encrypted)

decrypted = decrypt_message(encrypted, b"HeYt_XX4wRLvnDP9fC9-7hlMgasQ270tpWSrjKy0jRF=")
print("Decrypted:", decrypted)

When calling decrypt_message() I'm modifying the decryption key, I'm changing the 'E' for an 'F' (the same with G, H) and it still succeeds in the decryption process. I don't know why, in change if I put an 'I' for example it fails. As I understand it, symmetric encryption relies on using the exact same key for both encryption and decryption. I'm a newbie in the encryption/decryption world.

Output:

Code:
Encrypted: b'gAAAAABmcc7eNiwRP4lYl7gM0mthNdlvYsPUs_nttJMchmZ_mfJNMS4D2BC3QZHZZiZ7vrPhPwvBqK2DDOXSuB5wQkk9t-tU0PYefGvNy2IoDZNGNQMcdc0='

Code:
Decrypted: This is a secret message!

Process finished with exit code 0
<pre><code>def encrypt_message(message, key) -> bytes:
"""Encrypts a message using Fernet."""
f = Fernet(key)
encrypted_message = f.encrypt(message.encode())
return encrypted_message


def decrypt_message(encrypted_message, key) -> str:
"""Decrypts a message using Fernet."""
f = Fernet(key)
decrypted_message = f.decrypt(encrypted_message).decode()
return decrypted_message


message_ = "This is a secret message!"

encrypted = encrypt_message(message_, b"HeYt_XX4wRLvnDP9fC9-7hlMgasQ270tpWSrjKy0jRE=")
# The encrypted message will change in every encryption process which is OK
print("Encrypted:", encrypted)

decrypted = decrypt_message(encrypted, b"HeYt_XX4wRLvnDP9fC9-7hlMgasQ270tpWSrjKy0jRF=")
print("Decrypted:", decrypted)
</code></pre>
<p>When calling decrypt_message() I'm modifying the decryption key, I'm changing the 'E' for an 'F' (the same with G, H) and it still succeeds in the decryption process. I don't know why, in change if I put an 'I' for example it fails. As I understand it, symmetric encryption relies on using the exact same key for both encryption and decryption. I'm a newbie in the encryption/decryption world.</p>
<p>Output:</p>
<pre class="lang-none prettyprint-override"><code>Encrypted: b'gAAAAABmcc7eNiwRP4lYl7gM0mthNdlvYsPUs_nttJMchmZ_mfJNMS4D2BC3QZHZZiZ7vrPhPwvBqK2DDOXSuB5wQkk9t-tU0PYefGvNy2IoDZNGNQMcdc0='
</code></pre>
<pre class="lang-none prettyprint-override"><code>Decrypted: This is a secret message!
</code></pre>
<p>Process finished with exit code 0</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top