首页 > 解决方案 > 识别文本文件中的 PII 数据集并对其进行加密,以便以后解密?

问题描述

我有一个聊天文本文件,其中包含 PII 数据集,例如姓名、身份证号码、电话号码等。我必须在源头加密 PII 信息并在目的地使用 Python 3.7 再次解密。我使用 cryptography.fernet 进行转换,但它也用于使用单个密钥转换整个文件。可能的解决方案是什么?

from cryptography.fernet import Fernet
key = Fernet.generate_key()
key = b'aA1QRKHyk9lV8LM740tj_dcn045KkOSU7SM0hVPYfhQ=' #the key is random 
input_file = 'test.txt'
output_file = 'test.encrypted'

with open(input_file, 'rb') as f:
data = f.read()

fernet = Fernet(key)
encrypted = fernet.encrypt(data)

with open(output_file, 'wb') as f:
f.write(encrypted)

这里的问题是它加密整个文件并解密整个文件,并且使用我在代码中硬编码的同一个密钥,这不是正确的方法。

测试文件(非结构化)有内容:

hello john with id no 345678 with phone no 12345678
hello peter with id no 87654 with phone no 45678990

我需要在这里加密姓名、身份证号和电话号码。什么是最好的解决方案,用可以分别识别姓名、身份证和电话号码的密钥单独加密 PII 数据,然后再解密?更好的图书馆使用?示例代码会有所帮助

标签: python-3.xencryptioncryptographypii

解决方案


推荐阅读