So, I have a cvs file. My prof wants the submission to contain both, the original data and the encrypted values side by side. I don't know why she wants that as it defeats the purpose of encryption, but that's what she wants. But I have no idea how to do this. I'm using Google Colab. Any ideas? And sorry for bad formatting. This is my first post.
First of all, this is the kind of data I'm working with. The prof wants the encryption value to be on column B.
Column A | Column B |
---|---|
monkeys | |
phrases |
And this is my code. It can read, encrypt and decrypt the files separately. But this is not what the prof. want as submission. she wants it all on one cvs file. My code basically - encrypts the file which you then download, and decrypts the file (which you can also download).
pip install cryptography
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
#generate key
with open ('filekey.key', 'wb') as filekey:
filekey.write(key)
#open key
with open ('filekey.key', 'rb') as filekey:
key = filekey.read()
#open cvs file
import pandas as pd
from google.colab import files
upload = files.upload()
#read csv file
with open('nba_org.csv', 'rb') as file:
original = file.read()
#encrypt file
encrypted = f.encrypt(original)
#rewrite file
with open('nba.csv', 'wb') as encrypted_file:
encrypted_file.write(encrypted)
#download encrypted file
from google.colab import files
files.download('nba.csv')
#open cvs file
import pandas as pd
from google.colab import files
upload = files.upload()
#read csv file
with open('nba_org.csv', 'rb') as file:
original = file.read()
#decrypt file
decrypted = f.decrypt(encrypted)
#rewrite file
with open ('nba.csv', 'wb') as dec_file:
dec_file.write(decrypted)
#download decrypted file
from google.colab import files
files.download('nba.csv')
Are you using Pandas?
If so you can create a dataframe from your data, run your decryption on the values within column A and store them within column B. Once done write it as a csv.