<p>我得到了问题的答案。我把这个分享给其他可能面临这个问题的同事。在</p>
<p>实际上,用这些数据编码是有区别的。所以我所做的是,在填充之前将字符串编码成utf-8。然后用16的倍数填充,然后加密数据。之后,我解码并更新到数据库。这样,问题就为我解决了。在</p>
<p>代码如下:</p>
<pre><code>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")
</code></pre>
<p>所以只要把它编码成utf-8就可以解决这个问题。在</p>