2017-07-27 89 views
0

因此,我打算使用Jupyter Notbook(Python 3)進行一些數據分析,出於協作原因,我想將數據存儲在github repo上,但數據集很敏感。導入加密的csv到Python 3

因此,我想將數據(當前爲.csv)作爲加密文件存儲在回購站中,然後在運行時解密(我猜想有密碼提示)。

這樣做的最佳方法是什麼?

+0

可能出現[如何使用Python/PyCrypto以OpenSSL兼容方式對文件進行AES加密/解密?](https://stackoverflow.com/questions/16761458/how-to-aes-encrypt-decrypt-files - 使用 - 蟒-pycrypto功能於一個-OpenSSL兼容) –

回答

1

最後,我用python 3.6和SimpleCrypt來加密文件,然後上傳它。

認爲這是我用來加密該文件的代碼:

f = open('file.csv','r').read() 
ciphertext = encrypt('USERPASSWORD',f.encode('utf8')) #this .encode('utf8') is the bit im unsure about 
e = open('file.enc','wb') # file.enc doesn't need to exist, python will create it 
e.write(ciphertext) 
e.close 

這是我在運行時使用解密代碼,我跑getpass("password: ")作爲參數,所以我不必一個password變量存儲在存儲器

from io import StringIO 
import pandas as pd 
from simplecrypt import encrypt, decrypt 
from getpass import getpass 

# opens the file 
f = open('file.enc','rb').read() 

print('Please enter the password and press the enter key \n Decryption may take some time') 

# Decrypts the data, requires a user-input password 
CSVplaintext = decrypt(getpass("password: "), f).decode('utf8') 
print('Data have been Decrypted') 

#create a temp csv-like file to pass to pandas.read_csv() 
DATA=StringIO(CSVplaintext) 

# Makes a panda dataframe with the data 
df = pd.read_csv(DATA) 

注意,UTF-8編碼的行爲是在python 2.7不同,使代碼會稍有不同。