Fernet is a symmetric encryption library in Python that provides secure and easy-to-use encryption for sensitive data. One common use-case for Fernet is to encrypt data before storing it in a CSV file, ensuring only authorized parties can access it. However, decrypting these encrypted strings in a CSV file can be a little tricky, especially when using Pandas library.
In this article, we will discuss a solution to the problem of decrypting strings saved in a CSV file using Fernet and Pandas. We will provide a step-by-step explanation of the code, and delve into the relevant functions and libraries involved in the process.
To begin with, let’s discuss the problem in detail. When using Fernet encryption to secure data before storing it in a CSV file, it may be challenging to decrypt the data back while using Pandas for reading the file. The encrypted strings require proper handling to ensure their integrity during decryption.
Solution to the Problem
A potential solution to this problem is to use custom functions and apply them to the dataframe obtained from the CSV file. We will create a function to decrypt the encrypted strings using the Fernet library, and then apply this function to the Pandas dataframe containing the encrypted data.
Here is a step-by-step explanation of the code:
1. First, we need to import the necessary libraries:
import pandas as pd from cryptography.fernet import Fernet
2. Then, let’s generate a Fernet key and encrypt some sample data. Assume we have encrypted the following data and saved it in a CSV file named “encrypted_data.csv” with two columns “data” and “encrypted”:
key = Fernet.generate_key() cipher_suite = Fernet(key) data = "This is a sample text." encrypted_data = cipher_suite.encrypt(data.encode("utf-8"))
3. Now, let’s create a function to decrypt the encrypted strings with the given Fernet key:
def decrypt_string(encrypted_string, fernet_key): cipher_suite = Fernet(fernet_key) return cipher_suite.decrypt(encrypted_string.encode("utf-8")).decode("utf-8")
4. We can read the CSV file containing encrypted data using Pandas:
csv_data = pd.read_csv('encrypted_data.csv')
5. Finally, apply ‘decrypt_string’ function to the encrypted column of the dataframe using the ‘apply’ method and store the decrypted data in a new column. Note that you need to pass the key as an additional argument in the ‘apply’ method:
csv_data['decrypted'] = csv_data['encrypted'].apply(decrypt_string, fernet_key=key)
Fernet Library
Fernet is a popular cryptographic library in Python that provides easy-to-use methods for encrypting and decrypting data with AES symmetric-key cryptography. The library guarantees that data encrypted using Fernet cannot be further manipulated or read without the key, ensuring data confidentiality and integrity. Fernet uses URL-safe/base64 encoding for ciphertexts, which makes it suitable for storing encrypted data in files or databases.
Pandas Library
Pandas is an open-source data manipulation and data analysis library in Python. It provides data structures, such as Series and DataFrame, and various functions for handling, transforming, and visualizing data. Pandas is particularly useful for working with structured or tabular data, such as CSV files or SQL databases. This library simplifies many aspects of data manipulation, making it an essential tool for data analysis and machine learning.
In conclusion, decrypting encrypted strings saved in a CSV file using Fernet and Pandas can be achieved by following the steps provided in this article. By creating a custom decryption function and applying it to the dataframe, we can effectively decrypt the sensitive data stored in the CSV file.