|
| 1 | +# Encrypt and Decrypt a string of data |
| 2 | +import os |
| 3 | +from cryptography.fernet import Fernet |
| 4 | + |
| 5 | + |
| 6 | +# Generate a key to encrypt and decrypt messages |
| 7 | +def generate_key(): |
| 8 | + """ |
| 9 | + Generates a key.key file with the encryption password inside the password_provided variable |
| 10 | + :return: None |
| 11 | + """ |
| 12 | + # import here for optimization |
| 13 | + import base64 |
| 14 | + from cryptography.hazmat.backends import default_backend |
| 15 | + from cryptography.hazmat.primitives import hashes |
| 16 | + from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC |
| 17 | + |
| 18 | + password_provided = "root" # This is input in the form of a string |
| 19 | + password = password_provided.encode() |
| 20 | + salt = b'salt_' # CHANGE THIS - recommend using a key from os.urandom(16), must be of type bytes |
| 21 | + kdf = PBKDF2HMAC( |
| 22 | + algorithm=hashes.SHA256(), |
| 23 | + length=32, |
| 24 | + salt=salt, |
| 25 | + iterations=100000, |
| 26 | + backend=default_backend() |
| 27 | + ) |
| 28 | + key = base64.urlsafe_b64encode(kdf.derive(password)) # Can only use kdf once |
| 29 | + file = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'key.key'), 'wb') |
| 30 | + file.write(key) # The key is type bytes still |
| 31 | + file.close() |
| 32 | + |
| 33 | + |
| 34 | +def encrypt(msg): |
| 35 | + """ |
| 36 | + Encrypts a string of characters using the key.key encryption password |
| 37 | + :param msg: A string of characters |
| 38 | + :return Encrypted string of characters |
| 39 | + """ |
| 40 | + if not os.path.exists((os.path.join(os.path.dirname(os.path.realpath(__file__)), 'key.key'))): |
| 41 | + print('Call generate_key() to generate an encryption key first') |
| 42 | + else: |
| 43 | + file = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'key.key'), 'rb') |
| 44 | + key = file.read() # The key will be type bytes |
| 45 | + file.close() |
| 46 | + f = Fernet(key) |
| 47 | + return f.encrypt(msg) |
| 48 | + |
| 49 | + |
| 50 | +def decrypt(msg): |
| 51 | + """ |
| 52 | + Decrypts a string of characters using the key.key encryption password |
| 53 | + :param msg: An encrypted string of characters |
| 54 | + :return Decrypted string of characters |
| 55 | + """ |
| 56 | + if not os.path.exists((os.path.join(os.path.dirname(os.path.realpath(__file__)), 'key.key'))): |
| 57 | + print('Call generate_key() to generate an encryption key first') |
| 58 | + else: |
| 59 | + file = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'key.key'), 'rb') |
| 60 | + key = file.read() # The key will be type bytes |
| 61 | + file.close() |
| 62 | + f = Fernet(key) |
| 63 | + return f.decrypt(msg) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + # Test functionality |
| 68 | + generate_key() |
| 69 | + message = 'hello' |
| 70 | + one = encrypt(message.encode()) |
| 71 | + two = decrypt(one) |
| 72 | + print(message) |
| 73 | + print(one) |
| 74 | + print(two) |
0 commit comments