Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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
AES加密与熊猫读写-python 2.7_Python_Pandas_Encryption_Cryptography_Aes - Fatal编程技术网

AES加密与熊猫读写-python 2.7

AES加密与熊猫读写-python 2.7,python,pandas,encryption,cryptography,aes,Python,Pandas,Encryption,Cryptography,Aes,我正在尝试编写一个程序,它将数据文件作为输入,将其转换为数据帧,并使用包Crypto对给定列进行加密 输出应由两个数据帧/文件组成。第一个文件应该包含加密列的整个数据帧。第二个文件包含带有密钥的加密列 我的代码运行良好,因为它提供了所需的输出。解密时出现问题。不知何故,使用函数decryption从第二个文件复制密钥是不起作用的。然而,解密在程序之外工作,例如加密字符串“python”,加密字符串和密钥输出到控制台就可以了 我认为这与文件读写时的编码有关。我尝试过指定编码(“utf”),但没有帮

我正在尝试编写一个程序,它将数据文件作为输入,将其转换为数据帧,并使用包Crypto对给定列进行加密

输出应由两个数据帧/文件组成。第一个文件应该包含加密列的整个数据帧。第二个文件包含带有密钥的加密列

我的代码运行良好,因为它提供了所需的输出。解密时出现问题。不知何故,使用函数
decryption
从第二个文件复制密钥是不起作用的。然而,解密在程序之外工作,例如加密字符串“python”,加密字符串和密钥输出到控制台就可以了

我认为这与文件读写时的编码有关。我尝试过指定编码(“utf”),但没有帮助

任何帮助都将不胜感激。由于我对编码还不熟悉,欢迎对编程风格等提出任何类型的建设性批评

加密程序 读取文件时可能出现以下问题:

        def readfile_whole(self):
            if self.file_format == 'csv':
                    data = pd.read_csv(self.file_path, sep = self.separator)
                if self.file_format == 'xlsx' or self.file_format == 'xls':
                    data = pd.read_excel(self.file_path, sheetname = self.sheet_name)
                return data

        # Funktion um die Spalten zu lesen ,die verschlüsselt werden sollen
        def readfile_cols(self):
            print('Please provide a list of columns you want encrypted. When finished, press Enter without typing')        
            in_data = self.readfile_whole()   
            list_of_cols = []
            while True:
                inp = input().rstrip(' ')
                if inp == 'quit':
                    sys.exit('You quit the program')            
                if inp == '':
                    break
                if inp in list(in_data):
                    list_of_cols.append(inp)

            return in_data[list_of_cols]
或者在加密时:

def encryption(inputcol):
    block = 16 #bytes encryption
    padding = '{' 
    pad = lambda s: s + (block - len(s) % block) * padding 
    #string gets padded with this Padding depending on its size
    encryptAES = lambda c,s: base64.b64encode(c.encrypt(pad(s)))
    #declare function "encodeAES". A cipher (AES) is used to encrypt the 
    #padded string. Additionally, the encrypted string is encoded in b64 format
    keys = []  # initialize a keys column
    encryptedcol = []  # initialize a encrypted column
    for item in inputcol:
        key = base64.b64encode(os.urandom(16)) # generate a random key of size 16 bytes
        keys.append(key) 
    for key,item in zip(keys,inputcol):
        cipher = AES.new(key)  #creates a cipher out of the generated key
        encrypted_item = encryptAES(cipher, str(item))  #encrypt the 
        encryptedcol.append(encrypted_item)
    return [keys, encryptedcol]

要在csv文件上写入数据帧,将使用pandas函数
to_csv

检查PEP08[]以获得更具python风格的命名。函数应该用小写字母编写,类应该用PascalCase等。因为你的代码有点不合常规,这使得阅读起来比较困难。你能把你的问题缩减到有问题的行吗?这是相当多的代码需要消化,其中大部分可能没有必要帮助您解决问题。我认为问题发生在读取或写入数据帧时。我用熊猫做的。在字节级加密字符串和使用pandas时,有什么我应该考虑的吗?关于您的代码的另一个评论-在使用路径操作时,最好使用
os.path.join
os.path
模块的其他功能,因为它更安全并且跨平台工作。由于我手头没有真正的笔记本电脑,现在我无法逐行检查您的代码。避免此类混淆错误的一种方法是使用。按函数分解代码,并检查该部分是否工作。也许你可以提取填充函数,因为它对我来说可能很危险。一旦所有部件都单独进行了测试,然后进行组合。检查PEP08[]以获得更具python风格的命名。函数应该用小写字母编写,类应该用PascalCase等。因为你的代码有点不合常规,这使得阅读起来比较困难。你能把你的问题缩减到有问题的行吗?这是相当多的代码需要消化,其中大部分可能没有必要帮助您解决问题。我认为问题发生在读取或写入数据帧时。我用熊猫做的。在字节级加密字符串和使用pandas时,有什么我应该考虑的吗?关于您的代码的另一个评论-在使用路径操作时,最好使用
os.path.join
os.path
模块的其他功能,因为它更安全并且跨平台工作。由于我手头没有真正的笔记本电脑,现在我无法逐行检查您的代码。避免此类混淆错误的一种方法是使用。按函数分解代码,并检查该部分是否工作。也许你可以提取填充函数,因为它对我来说可能很危险。一旦所有零件都单独进行了测试,然后进行组合。
def encryption(inputcol):
    block = 16 #bytes encryption
    padding = '{' 
    pad = lambda s: s + (block - len(s) % block) * padding 
    #string gets padded with this Padding depending on its size
    encryptAES = lambda c,s: base64.b64encode(c.encrypt(pad(s)))
    #declare function "encodeAES". A cipher (AES) is used to encrypt the 
    #padded string. Additionally, the encrypted string is encoded in b64 format
    keys = []  # initialize a keys column
    encryptedcol = []  # initialize a encrypted column
    for item in inputcol:
        key = base64.b64encode(os.urandom(16)) # generate a random key of size 16 bytes
        keys.append(key) 
    for key,item in zip(keys,inputcol):
        cipher = AES.new(key)  #creates a cipher out of the generated key
        encrypted_item = encryptAES(cipher, str(item))  #encrypt the 
        encryptedcol.append(encrypted_item)
    return [keys, encryptedcol]