问题
I am trying to write a python code which has same functionarities of AES_ENCRYPT
and AES_DECRYPT
of MySQL.
https://dev.mysql.com/doc/refman/5.6/ja/encryption-functions.html
I want to encrypt and decrypt data between MySQL and Python.
For example, I want to decrypt data by python, which is encrypted by AES_ENCRYPT
of MySQL.
And I want to decrypt data by AES_DECRYPT
of MySQL, which is encrypted by Python vice versa.
I found a example of AES_ENCRYPT
in Python.
https://www.maykinmedia.nl/blog/2012/nov/15/mysql-aes_encrypt-python/
Does anyone know how to implement the decryption part?
回答1:
I did it finally.
def mysql_aes_decrypt(val, key):
def mysql_aes_key(key):
final_key = bytearray(16)
for i, c in enumerate(key):
final_key[i % 16] ^= ord(key[i])
return bytes(final_key)
k = mysql_aes_key(key)
cipher = AES.new(k, AES.MODE_ECB)
return cipher.decrypt(val).decode()
来源:https://stackoverflow.com/questions/51134744/how-to-use-implement-aes-decrypt-of-mysql-by-python