pobcrypt/pobcrypt-cli/pobcrypt.py
#!/usr/bin/env python3
from cryptography.fernet import Fernet
import sys,os
def generateKey(f):
key = Fernet.generate_key()
with open(f, 'wb') as filekey:
filekey.write(key)
def enc():
with open(sys.argv[2], 'rb') as filekey:
key = filekey.read()
fernet = Fernet(key)
with open(sys.argv[3], 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original)
with open(sys.argv[3]+'.enc', 'wb') as encrypted_file:
encrypted_file.write(encrypted)
def dec():
with open(sys.argv[2], 'rb') as filekey:
key = filekey.read()
fernet = Fernet(key)
with open(sys.argv[3], 'rb') as enc_file:
encrypted = enc_file.read()
decrypted = fernet.decrypt(encrypted)
name=os.path.splitext(sys.argv[3])[0]
with open(name, 'wb') as dec_file:
dec_file.write(decrypted)
try:
mode=sys.argv[1]
except IndexError:
print("Usage: pobcrypt [enc/dec/genkey] key file")
sys.exit(-1)
if (mode=="genkey"):
generateKey(sys.argv[2])
elif (mode=="enc") :
enc()
elif (mode=="dec"):
dec()
elif (mode=="--help" or mode=="-h" or mode=="help"):
print("Usage: pobcrypt [enc/dec/genkey] key file")
else:
print("Usage: pobcrypt [enc/dec/genkey] key file")