Skip to content

Instantly share code, notes, and snippets.

@3lpsy
Created December 5, 2017 18:13
Show Gist options
  • Save 3lpsy/889839b56caf3bd187fe627e45ba44d1 to your computer and use it in GitHub Desktop.
Save 3lpsy/889839b56caf3bd187fe627e45ba44d1 to your computer and use it in GitHub Desktop.
Symmetric Decryption with
# Encrypt / Decrypt
import base64
import sys
from cryptography.fernet import Fernet
action = input('Encrypt or Decrypt [e/d]?')
if action.lower() == 'encrypt' or action.lower() == 'e':
secret = input('Secret Key: ')
secret_bytes = int(secret).to_bytes(32, byteorder=sys.byteorder)
key = base64.urlsafe_b64encode(secret_bytes)
msg = input("Message: ")
f = Fernet(key)
cipher_text = f.encrypt(msg.encode())
print("Using Secret: ", str(key))
print("CipherText: ", str(cipher_text))
elif action.lower() == 'decrypt' or action.lower() == 'd':
secret = input('Secret Key: ')
secret_bytes = int(secret).to_bytes(32, byteorder=sys.byteorder)
key = base64.urlsafe_b64encode(secret_bytes)
f = Fernet(key)
cipher_text = input("Cipher Text: ")
print("Message: ", f.decrypt(cipher_text.encode()))
else:
print("Enter 'e' or 'd' ")
# Brute Force
import base64
import sys
from cryptography.fernet import Fernet
from cryptography.fernet import InvalidToken
cipher_text = input("Cipher Text: ")
mod = int(input("Mod Limit: "))
print("...")
for i in range(1, mod):
try:
secret_bytes = int(i).to_bytes(32, byteorder=sys.byteorder)
key = base64.urlsafe_b64encode(secret_bytes)
f = Fernet(key)
decoded_cipher = cipher_text.encode()
decrypted = f.decrypt(decoded_cipher)
print("Success")
print("Secret {}: ".format(i), decrypted)
except InvalidToken as e:
print("Invalid {}".format(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment