Python Tutorial: Encrypt and Decrypt Text Using Passwords with PBKDF2 and Fernet
Learn how to securely encrypt and decrypt messages in Python using a password-based approach. This tutorial demonstrates the use of PBKDF2HMAC for key derivation and Fernet for encryption, eliminating the need for key files.

def lemon():
92 views β’ Jun 1, 2025

About this video
In this quick and practical Python tutorial, learn how to encrypt and decrypt messages using only a password β no key files needed! We use PBKDF2HMAC to derive a secure encryption key from the password, and Fernet from the cryptography library to handle encryption.
Whole code:
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.fernet import Fernet
from base64 import urlsafe_b64encode
# Prompt for user action
want = input("1. Encrypt data 2. Decrypt with pass\n")
# Encryption part
if want == "1":
password = bytes(input("Password to encrypt to: "), 'utf-8')
salt = b"m1oPwOfs9rwh430wLlRmjg==" # Fixed salt
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100_000,
)
key = urlsafe_b64encode(kdf.derive(password))
f = Fernet(key)
message = bytes(input("Enter the message to encrypt: "), 'utf-8')
encrypted = f.encrypt(message)
print("Encrypted:", encrypted.decode())
# Decryption part
elif want == "2":
password = bytes(input("Enter password to decrypt: "), 'utf-8')
salt = b"m1oPwOfs9rwh430wLlRmjg==" # Same fixed salt
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100_000,
)
key = urlsafe_b64encode(kdf.derive(password))
f = Fernet(key)
# User provides the encrypted data
encrypted_data = bytes(input("Enter the encrypted message: "), 'utf-8')
try:
decrypted = f.decrypt(encrypted_data)
print("Decrypted:", decrypted.decode())
except Exception as e:
print("Decryption failed:", str(e))
Whole code:
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.fernet import Fernet
from base64 import urlsafe_b64encode
# Prompt for user action
want = input("1. Encrypt data 2. Decrypt with pass\n")
# Encryption part
if want == "1":
password = bytes(input("Password to encrypt to: "), 'utf-8')
salt = b"m1oPwOfs9rwh430wLlRmjg==" # Fixed salt
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100_000,
)
key = urlsafe_b64encode(kdf.derive(password))
f = Fernet(key)
message = bytes(input("Enter the message to encrypt: "), 'utf-8')
encrypted = f.encrypt(message)
print("Encrypted:", encrypted.decode())
# Decryption part
elif want == "2":
password = bytes(input("Enter password to decrypt: "), 'utf-8')
salt = b"m1oPwOfs9rwh430wLlRmjg==" # Same fixed salt
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100_000,
)
key = urlsafe_b64encode(kdf.derive(password))
f = Fernet(key)
# User provides the encrypted data
encrypted_data = bytes(input("Enter the encrypted message: "), 'utf-8')
try:
decrypted = f.decrypt(encrypted_data)
print("Decrypted:", decrypted.decode())
except Exception as e:
print("Decryption failed:", str(e))
Video Information
Views
92
Likes
3
Duration
1:04
Published
Jun 1, 2025
Related Trending Topics
LIVE TRENDSRelated trending topics. Click any trend to explore more videos.