<jupyter_start><jupyter_text>Configure Notebook  Import Libraries<jupyter_code>import base64
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from io import BytesIO
import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Define Functions<jupyter_code>def load_encrypted_dataset(encrypted_file, decryption_key_path):
    ''' A function uses a decryption key to decrypt a file.

    Parameters:
    encrypted_file (str): The path to a file which has been encrypted.
    decryption_key_path (str): The path to the corresponding decryption key.

    Returns:
    decrypted_dataset (Pandas DataFrame): A decrypted version of the encrypted file.
    '''
    
    file = open(decryption_key_path, 'rb') # Open file.
    decryption_key = file.read() # Read decryption key.
    file.close() # Close file.
       
    fernet = Fernet(decryption_key)
    
    with open(encrypted_file, 'rb') as f:
        encrypted_file = f.read()
    
    decrypted_file = fernet.decrypt(encrypted_file) # Decrypt file.
    decrypted_dataset = pd.read_csv(BytesIO(decrypted_file), index_col=0) # Convert bytes data to Pandas DataFrame.
    
    return decrypted_dataset<jupyter_output><empty_output><jupyter_text>Test  Generate Random Dataset<jupyter_code>unsecure_df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))

unsecure_df.to_csv('unsecure.csv') # Export corrected dataset.

unsecure_df<jupyter_output><empty_output><jupyter_text>Generate Encryption Key<jupyter_code>password_provided = 'password' # Provide password.
password = password_provided.encode() # Encode password.

salt = b"<PASSWORD>" # Provide salt key.

kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
                length=32,
                salt=salt,
                iterations=100000,
                backend=default_backend())

key = base64.urlsafe_b64encode(kdf.derive(password))

file = open(f'encryption_key.key', 'wb')
file.write(key)
file.close()<jupyter_output><empty_output><jupyter_text>Encrypt Dataset<jupyter_code>file = open(f'encryption_key.key', 'rb')
key = file.read()
file.close()

# Open the file to encrypt
with open(f'unsecure.csv', 'rb') as f: # open dataset file.
    dataset = f.read()

fernet = Fernet(key)
encrypted_dataset = fernet.encrypt(dataset)

# Write the encrypted file
with open(f'secure.csv.encrypted', 'wb') as f: # Encrypt dataset csv file.
    f.write(encrypted_dataset)
    
encrypted_dataset<jupyter_output><empty_output><jupyter_text>Decrypt Dataset<jupyter_code>decrypted_dataset = load_encrypted_dataset(encrypted_file='secure.csv.encrypted', decryption_key_path='encryption_key.key')

decrypted_dataset<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10042283298097252, 'fraction_numerical': 0.0102184637068358, 'mean_word_length': 5.0276008492569, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '583093', 'license': 'MIT', 'n_tokens_mistral': 984, 'n_tokens_neox': 946, 'n_words': 197, 'path': 'encrypt_dataframe.ipynb', 'repo_name': 'deepbakes/encrypted_dataframes', 'size': '2838'}