How to use implement AES_DECRYPT() of MySQL by Python

喜你入骨 提交于 2021-02-08 08:14:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!