Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Last active January 22, 2024 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruanbekker/7f8a122340adaeb558a84aed9bc7dfe0 to your computer and use it in GitHub Desktop.
Save ruanbekker/7f8a122340adaeb558a84aed9bc7dfe0 to your computer and use it in GitHub Desktop.
Encryption / Decryption with Python and Simple-Crypt
# https://github.com/andrewcooke/simple-crypt
import base64
import argparse
from flata import Flata, Query, where
from flata.storages import JSONStorage
from simplecrypt import encrypt, decrypt
from getpass import getpass
db_init = Flata('mydb.json', storage=JSONStorage)
db_init.table('collection1', id_field = 'uid')
db = db_init.get('collection1')
def base64_encode(string):
encoded_string = base64.b64encode(string)
return encoded_string
def base64_decode(string):
decoded_string = base64.b64decode(string)
return decoded_string
def check_passphrase(password):
if len(password) < 6:
raise SystemExit('Passphrase needs to be more than 6 charachters')
def check_description(description):
if description == None:
raise SystemExit('Description is needed when encryption mode is used')
def MainApp():
parser = argparse.ArgumentParser(description='Utility to Encrypt/Decrypt')
parser.add_argument('-t', '--type', help='option: encrypt/decrypt/list', required=True)
parser.add_argument('-c', '--cipher', help='option: cipher to decrypt', required=False)
parser.add_argument('-s', '--string', help='option: string to encrypt', required=False)
parser.add_argument('-d', '--description', help='option: descryption for secret', required=False)
args = parser.parse_args()
if args.type == 'encrypt':
secret_string = args.string
password = getpass()
check_passphrase(password)
check_description(args.description)
secret_desc = args.description
cipher = encrypt(password, secret_string)
encoded_string = base64_encode(cipher)
db.insert({'Description': secret_desc, 'EncryptedString': encoded_string})
return encoded_string
if args.type == 'decrypt':
try:
secret_string = args.cipher
password = getpass()
decoded_string = base64_decode(secret_string)
decrypted_string = decrypt(password, decoded_string)
return decrypted_string
except Exception as e:
return e
if args.type == 'list':
try:
db_list = db.all()
return db_list
except Exception as e:
return e
if __name__ == '__main__':
val = MainApp()
print(val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment