AES加密问题:“ValueError:输入字符串的长度必须是16的倍数”

2024-03-28 22:42:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用这个python代码用AES加密数据库中的一些敏感数据

from Crypto.Cipher import AES
import base64
import pymysql
import os
import hashlib    

def encryption(self, tableName):
    # opening db connection
    db_connection = self.getMySqlConnection()
    cursor = db_connection.cursor()

    print("\tTwo Way Encryption")

    # hashing
    cursor.execute("SELECT toMask FROM " + tableName + ";")
    row = cursor.fetchall()
    for item in row:
        string = str(item[0])
        padded = string.rjust(80)
        secret_key = self.key
        cipher = AES.new(secret_key, AES.MODE_ECB)
        encoded = base64.b64encode(cipher.encrypt(padded))
        encoded = encoded.decode('utf-8')
        stmt = "update " + tableName + " set toMask = '" + encoded + "' where toMask =\"" + string + "\";"
        cursor.execute(stmt)
        db_connection.commit()

    # closing db connection
    db_connection.close()

    print("\tDone")

但是我的数据包含了一些像这样的特殊字符

Å

所以我得到了这个错误:

ValueError: Input strings must be a multiple of 16 in length

我怎么解决这个问题?其他输入字符串不会产生任何问题。在

或者我需要把它转换成其他编码吗?在


Tags: keyimportselfexecutedbstringconnectioncursor
2条回答

Crypto.cipherdocumentation不完整,它没有指定如何处理填充。猜测是它希望调用者进行填充。(如果你写开源,请提供预期的功能和完整的文档。在这种情况下,应该支持PKCS#7填充,并且应该指定输入类型(binary、character string、hex、Base64)

它似乎还需要字节:cipher.encrypt(b'Attack at dawn'),注意b。在

因为必须填充并在中提供二进制文件,因此必须先转换为二进制文件,然后再填充,因此cipher的输入是块大小的倍数(AES为16字节)。在

空填充是不好的数据人曾经有过一个0x00字节作为最后一个字节,一般的填充是PKCS#7。在

咆哮:

如果你写开源,请提供预期的功能和完整的文档。在这种情况下,应该支持PKCS#7填充并指定输入类型(二进制、字符串、十六进制、Base64等)。在

我得到了问题的答案。我把这个分享给其他可能面临这个问题的同事。在

实际上,用这些数据编码是有区别的。所以我所做的是,在填充之前将字符串编码成utf-8。然后用16的倍数填充,然后加密数据。之后,我解码并更新到数据库。这样,问题就为我解决了。在

代码如下:

from Crypto.Cipher import AES
import base64
import pymysql
import os
import hashlib

    def encryption(self, tableName, paddingLength):
    # opening db connection
    db_connection = self.getMySqlConnection()
    cursor = db_connection.cursor()

    print("\tTwo Way Encryption")


    # hashing
    cursor.execute("SELECT toMask FROM " + tableName + ";")
    row = cursor.fetchall()
    for item in row:
        string = str(item[0])
        #convert to string 
        strng = str(string)
        #encode to utf-8
        string = string.encode('utf-8')
        #pad with length 80
        padded = string.rjust(80)
        secret_key = self.key
        cipher = AES.new(secret_key, AES.MODE_ECB)
        encoded = base64.b64encode(cipher.encrypt(padded))
        #decode utf-8
        encoded = encoded.decode('utf-8')
        encoded = str(encoded)
        # deselect strict mode
        cursor.execute("SET @@global.sql_mode= 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';")
        stmt = "update " + tableName + " set toMask = \"" + encoded + "\" where toMask =\"" + strng + "\";"
        # print(stmt)
        cursor.execute(stmt)
        db_connection.commit()

    # closing db connection
    db_connection.close()
    print("\tDone")

所以只要把它编码成utf-8就可以解决这个问题。在

相关问题 更多 >