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
import base64
import os
import getpass
# Function to generate a key from the password
def key_from_password(password, salt):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(password.encode()))
return key
# Encrypt the message
def encrypt_message(message, password):
salt = os.urandom(16)
key = key_from_password(password, salt)
fernet = Fernet(key)
encrypted = fernet.encrypt(message.encode())
return encrypted, salt
# Decrypt the message
def decrypt_message(encrypted_message, password, salt):
key = key_from_password(password, salt)
fernet = Fernet(key)
try:
decrypted = fernet.decrypt(encrypted_message).decode()
return decrypted
except Exception as e:
return "Error: " + str(e)
# Main interface
if __name__ == "__main__":
choice = input("Do you want to encrypt or decrypt a message? (encrypt/decrypt): ")
if choice == "encrypt":
secret_message = input("Enter your secret message: ")
password = getpass.getpass("Enter a password for encryption: ")
encrypted_message, salt = encrypt_message(secret_message, password)
print("Your encrypted message is:")
print(encrypted_message.decode())
print("Your salt (needed for decryption) is:")
print(base64.b64encode(salt).decode())
elif choice == "decrypt":
encrypted_message_input = input("Enter the encrypted message: ")
salt_input = base64.b64decode(input("Enter the salt: "))
password = getpass.getpass("Enter the password for decryption: ")
decrypted_message = decrypt_message(encrypted_message_input.encode(), password, salt_input)
print("Your decrypted message is:")
print(decrypted_message)
else:
print("Invalid choice. Please type 'encrypt' or 'decrypt'.")
Example:Encrypted message is: gAAAAABmCsIhWZe1vm94Ma4p6v3hv8LmP5JuvhsA1qI65TK_X2LolyNqEFr2y2yZyORoi5KuVubjyay
nScTIfAKrqBka17jslzoHfVTB7-c1fgx-qjbjTbDEDUG5vlCE0qy5uOgqBYG4jt5b9gxnhBdVLL3qc2e7nw==-c1fgx-qjbjTbDEDUG5vlCE0qy5uOgqBYG4jt5b9gxnhBdVLL3qc2e7nw==
Salt (needed for decryption) is: IJZVtFg2B1u5r/A1bsk/TA==
The password for decryption is: password
Whoever manages to obtain the private key for Puzzle 66.
Is requested to encrypt it using this script and post the encrypted message here. Post only the encrypted message, and keep the salt and password to yourself.
Then, proceed to complete your transaction process after few days. If, for any reason, the transaction fails to validate and someone else hijacks it,
You are encouraged to post your salt and password here as evidence that you had solved Puzzle 66.
This will enable the entire community to potentially pursue legal action against the thief. Therefore, anyone attempting to undermine someone else's hard work is hereby warned.
@alberto or any other member, if you discover any vulnerabilities here or are capable of cracking this script, please inform us. Additionally, if there are any alternative implementations, kindly share those as well.