【发布时间】:2018-04-20 16:06:06
【问题描述】:
我试图通过这个 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
我该如何解决这个问题?其他输入字符串没有任何问题。
或者我需要将其转换为任何其他编码吗?
【问题讨论】:
-
1.当您嵌套函数调用而不是使用中间语句和变量时,它会使调试变得更加困难。 2. 你用的是什么语言和什么 AES 库?
-
语言是python,库是Crypto.cipher