有没有办法将原始数据和加密后的值并排放置?

huangapple go评论84阅读模式
英文:

Is there a way to have the original data and the encrypted values side by side?

问题

我有一个CSV文件。我的教授希望提交包含原始数据和加密值并排的文件。我不知道她为什么要这样做,因为这违背了加密的目的,但这是她想要的。但我不知道如何做到这一点。我正在使用Google Colab。有什么建议吗?对于格式不佳的问题我表示抱歉。这是我的第一篇帖子。

首先,这是我正在处理的数据类型。教授希望加密值位于B列。

列A 列B
monkeys
phrases

这是我的代码。它可以分别读取、加密和解密文件。但这不是教授要求的提交方式。她希望所有内容都在一个CSV文件中。我的代码基本上 - 加密文件,然后您可以下载,解密文件(您也可以下载)。

pip install cryptography

from cryptography.fernet import Fernet

key = Fernet.generate_key()
f = Fernet(key)

# 生成密钥
with open('filekey.key', 'wb') as filekey:
  filekey.write(key)

# 打开密钥
with open('filekey.key', 'rb') as filekey:
  key = filekey.read()
# 打开CSV文件
import pandas as pd
from google.colab import files
upload = files.upload()

# 读取CSV文件
with open('nba_org.csv', 'rb') as file:
  original = file.read()

# 加密文件
encrypted = f.encrypt(original)

# 重写文件
with open('nba.csv', 'wb') as encrypted_file:
  encrypted_file.write(encrypted)

# 下载加密文件
from google.colab import files
files.download('nba.csv')
# 打开CSV文件
import pandas as pd
from google.colab import files
upload = files.upload()

# 读取CSV文件
with open('nba_org.csv', 'rb') as file:
  original = file.read()

# 解密文件
decrypted = f.decrypt(encrypted)

# 重写文件
with open('nba.csv', 'wb') as dec_file:
  dec_file.write(decrypted)

# 下载解密文件
from google.colab import files
files.download('nba.csv')
英文:

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')

答案1

得分: 0

你正在使用Pandas吗?

如果是的话,你可以从你的数据中创建一个数据帧,对列A中的值运行解密操作,然后将它们存储在列B中。完成后,将其写入为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.

huangapple
  • 本文由 发表于 2023年7月7日 06:25:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定