aes - AES:加密有效,但CSV文件中的列不解密

我正在加密csv文件中的某些列,然后使用Python3.7中的加密列创建一个csv文件。

然后,我从csv文件中读取了加密的列,并尝试对其进行解密,但出现以下错误

Traceback (most recent call last):
  File "clean.py", line 169, in <module>
    decryption(df, decrypt_list, password)
  File "clean.py", line 138, in decryption
    decrypted_dict = decrypt_cols(encrypted_dict,password)
  File "clean.py", line 98, in decrypt_cols
    plain_text = decrypt(encrypted_dict[dict_key][index], key_salt_pair[0]).decode('utf-8')
  File "clean.py", line 56, in decrypt
    return unpad(cipher.decrypt(enc[16:]),BLOCK_SIZE)
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\Crypto\Cipher\_mode_cbc.py", line 246, in decrypt
    raise ValueError("Data must be padded to %d byte boundary in CBC mode" % self.block_size)
ValueError: Data must be padded to 16 byte boundary in CBC mode

当我直接解密而不将密文存储在excel工作表中时,该代码似乎可以工作。

import base64
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Util.Padding import pad, unpad

BLOCK_SIZE = 16
'''
# AES 256 encryption/decryption using pycrypto library
Credits: https://www.quickprogrammingtips.com/python/aes-256-encryption-and-decryption-in-python.html
https://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256
''' 

def encrypt(raw, private_key):
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(private_key, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(pad(raw.encode('utf-8'),BLOCK_SIZE)))


def decrypt(enc, private_key):
    enc = base64.b64decode(enc)
    iv = enc[:16]
    cipher = AES.new(private_key, AES.MODE_CBC, iv)
    return unpad(cipher.decrypt(enc[16:]),BLOCK_SIZE)


def encrypt_cols(encryption_dict,password):
    encrypted_dict = {}
    encrypted_dict['salt'] = []
    for dict_key in encryption_dict.keys():
        encrypted_dict[dict_key] = []
        for index in range(0,len(encryption_dict[dict_key])):
            try:
                key_salt_pair = get_private_key_salt(password,encrypted_dict['salt'][index])
            except IndexError:
                key_salt_pair = get_private_key_salt(password)
                encrypted_dict['salt'].append(key_salt_pair[1])
            cipher_text = encrypt(str(encryption_dict[dict_key][index]), key_salt_pair[0])
            encrypted_dict[dict_key].append(cipher_text)
            encrypt_managers(encryption_dict[dict_key][index],cipher_text)
    return encrypted_dict


def decrypt_cols(encrypted_dict,password):
    decrypted_dict = {}
    decrypted_dict['salt'] = encrypted_dict['salt']
    for dict_key in encrypted_dict.keys():
        decrypted_dict[dict_key] = []
        if dict_key != 'salt':
            for index in range(0,len(encrypted_dict[dict_key])):
                key_salt_pair = get_private_key_salt(password,encrypted_dict['salt'][index])                
                plain_text = decrypt(encrypted_dict[dict_key][index], key_salt_pair[0]).decode('utf-8')
                #XXX ValueError: Data must be padded to 16 byte boundary in CBC mode
                decrypted_dict[dict_key].append(plain_text)
                decrypt_managers(encrypted_dict[dict_key][index],plain_text)
    return decrypted_dict

我尝试将密文的类型从字符串更改为字节。 从excel表中读取时,我试图找到其他错误。 我不确定我要去哪里错。

如果您想查看整个代码,请告诉我。 我不想放太多不必要的代码。

救命:

  1. 我需要修复错误的帮助。

  2. 另外,更重要的是,我进行加密的方式正确吗?

我在这里详细说明了整个代码: 链接

转载请注明来自askonline.tech,本文标题:aes - AES:加密有效,但CSV文件中的列不解密


 Top