Created
March 31, 2021 22:53
-
-
Save a3r0id/cdb784bb37e12dad12445d79e11a6b10 to your computer and use it in GitHub Desktop.
Encode/decode (secret) encrypted data/messages in any binary file using fernet encryption.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Usage (Encryption): | |
python3 main.py encrypt fileIn.png fileOut.png "Hello World!" | |
Usage (Decryption): | |
python3 main.py decrypt fileIn.png DECRYPTION_KEY | |
OR | |
python3 main.py decrypt fileIn.png DECRYPTION_KEY --out=fileOut.txt | |
""" | |
from os import write | |
from sys import argv, stdout | |
from cryptography.fernet import Fernet | |
indiceSig = b"\x29\x2f\xd1\x2f\xa1\x2f\x28" | |
# FERNET ENCRYPT | |
def fencrypt(key, value): | |
f = Fernet(key) | |
return f.encrypt(value) | |
# FERNET DECRYPT | |
def fdecrypt(key, evalue): | |
f = Fernet(key) | |
return f.decrypt(evalue) | |
def usage(code=0): | |
stdout.write( | |
"\r\n\n"\ | |
"Usage (Encryption): \n"\ | |
"python3 main.py encrypt fileIn.png fileOut.png \"Hello World!\"\n\n"\ | |
"Usage (Decryption): \n"\ | |
"python3 main.py decrypt fileIn.png DECRYPTION_KEY --out=fileOut.txt\n\n"\ | |
"A script by Aero\r\n\n" | |
) | |
exit(0) | |
# CHECK USAGE TYPE | |
try: | |
if (argv[1].lower() == "encrypt"): | |
encrypt = True | |
elif (argv[1].lower() == "decrypt"): | |
encrypt = False | |
else: | |
usage() | |
except: | |
usage() | |
if encrypt: | |
# GENERATE KEY | |
key = Fernet.generate_key().decode() | |
# GET ARGS | |
try: | |
fileIn = argv[2] | |
fileOut = argv[3] | |
message = argv[4] | |
except: | |
usage() | |
# OPEN INPUT FILE & GET CONTENT BUFFER | |
with open(fileIn, "rb") as f: | |
_in = f.read() | |
# OPEN OUTPUT FILE | |
with open(fileOut, "wb") as f: | |
f.write( | |
_in | |
+ indiceSig | |
+ fencrypt(key, bytes(message, 'utf-8')) | |
+ indiceSig | |
) | |
stdout.write( | |
f"\r\nSuccessfully embedded encrypted message based on {fileIn} to {fileOut}!"\ | |
f"\nDecryption Key: {key}" | |
) | |
# DECRYPT | |
else: | |
# GET ARGS | |
try: | |
fileIn = argv[2] | |
key = argv[3] | |
except: | |
usage() | |
try: | |
fileOut = argv[4].strip("--out=") | |
except: | |
fileOut = None | |
# OPEN INPUT FILE | |
with open(fileIn, "rb") as f: | |
buffer = f.read() | |
try: | |
message = fdecrypt(key, buffer.split(indiceSig)[1].split(indiceSig)[0]).decode() | |
except: | |
stdout.write("\r\nInvalid Decryption Key!") | |
# WRITE TO CONSOLE IF OUTPUT FILE NOT SPECIFIED | |
if (fileOut is None): | |
stdout.write(f"\r\nDecrypted Message!\n\nContent:\n{message}") | |
else: | |
with open(fileOut, "w+") as f: | |
f.write(message) | |
stdout.write(f"\r\nDecrypted message written to {fileOut}!") | |
stdout.write('\r\n\nHave a nice day! :)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment