Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中加密JSON_Python_Json_Encryption_Data Security - Fatal编程技术网

如何在python中加密JSON

如何在python中加密JSON,python,json,encryption,data-security,Python,Json,Encryption,Data Security,我有一个JSON文件。我正在运行一个python程序,从JSON文件中提取数据。有没有办法用密钥加密JSON文件,这样如果有人随机打开该文件,它将是一堆字符,但是当密钥被输入程序时,它会解密并能够读取它?提前感谢。是的,您可以加密.json文件。确保通过键入 pip install cryptography 或 如果你在windows上 然后,你可以制作一个类似于我的程序: #this imports the cryptography package from cryptography.fer

我有一个JSON文件。我正在运行一个python程序,从JSON文件中提取数据。有没有办法用密钥加密JSON文件,这样如果有人随机打开该文件,它将是一堆字符,但是当密钥被输入程序时,它会解密并能够读取它?提前感谢。

是的,您可以加密.json文件。确保通过键入

pip install cryptography

如果你在windows上

然后,你可以制作一个类似于我的程序:

#this imports the cryptography package
from cryptography.fernet import Fernet

#this generates a key and opens a file 'key.key' and writes the key there
key = Fernet.generate_key()
file = open('key.key','wb')
file.write(key)
file.close()

#this just opens your 'key.key' and assings the key stored there as 'key'
file = open('key.key','rb')
key = file.read()
file.close()

#this opens your json and reads its data into a new variable called 'data'
with open('filename.json','rb') as f:
    data = f.read()

#this encrypts the data read from your json and stores it in 'encrypted'
fernet = Fernet(key)
encrypted=fernet.encrypt(data)

#this writes your new, encrypted data into a new JSON file
with open('filename.json','wb') as f:
    f.write(encrypted)
请注意,此块:

file = open('key.key','wb')
file.write(key)
file.close()

#this just opens your 'key.key' and assigns the key stored there as 'key'
file = open('key.key','rb')
key = file.read()
file.close()
没有必要。这只是一种将生成的密钥存储在安全位置并读回的方法。如果需要,可以删除该块


如果您需要进一步帮助,请告诉我:)

是的,您可以加密.json文件。确保通过键入

pip install cryptography

如果你在windows上

然后,你可以制作一个类似于我的程序:

#this imports the cryptography package
from cryptography.fernet import Fernet

#this generates a key and opens a file 'key.key' and writes the key there
key = Fernet.generate_key()
file = open('key.key','wb')
file.write(key)
file.close()

#this just opens your 'key.key' and assings the key stored there as 'key'
file = open('key.key','rb')
key = file.read()
file.close()

#this opens your json and reads its data into a new variable called 'data'
with open('filename.json','rb') as f:
    data = f.read()

#this encrypts the data read from your json and stores it in 'encrypted'
fernet = Fernet(key)
encrypted=fernet.encrypt(data)

#this writes your new, encrypted data into a new JSON file
with open('filename.json','wb') as f:
    f.write(encrypted)
请注意,此块:

file = open('key.key','wb')
file.write(key)
file.close()

#this just opens your 'key.key' and assigns the key stored there as 'key'
file = open('key.key','rb')
key = file.read()
file.close()
没有必要。这只是一种将生成的密钥存储在安全位置并读回的方法。如果需要,可以删除该块


如果您需要进一步帮助,请告诉我:)

检查,谢谢!我不知道你可以用Fernet密钥加密JSON文件!谢谢我不知道你可以用Fernet密钥加密JSON文件!