Home >Backend Development >Python Tutorial >Python--AES encryption and decryption method guidance
Origin:
When the video was downloaded and parsed to a website, it was found that the video ID was encrypted with AES and this library was used.
Decrypt a very simple js code:
t.video = CryptoJS.AES.decrypt(t.video, secret).toString(CryptoJS.enc.Utf8);
I originally thought it would be simple and found a python code to decrypt, but I didn’t expect to try it over and over again. How to write it There are so many different things, but I can’t figure them out, and it takes a lot of effort!
How simple? I only need to verify the following string encryption and decryption:
# data = '-85297962_172051801'# key = '583a01a9ba901a3adda7252ebca42c09'# encrypt_data = 'U2FsdGVkX192df0Gxgia8s93zZp85f9m2nU1VIGU+RZQDtViB1LPBnE0CBWgVDBj'
1. Python Cryptography Toolkit (pycrypto)
It is needed for encryption and decryption. Its URL is:
The latest version is 2.6.1. How to install it is a very simple demo. It is all on the page, and its usage can be found everywhere on the Internet, but it just can’t solve my problem. I think I used it wrong, but which one is right?
crypto-js should use the AES default mode, AES.MODE_CBC. The js code is also difficult to understand, I always keep trying!
2. Encryption and decryption
Just go to the code, it meets the needs:
# coding=utf-8import base64from Crypto.Cipher import AESfrom Crypto import Randomfrom hashlib import md5 BLOCK_SIZE = AES.block_sizedef pad(data): length = BLOCK_SIZE - (len(data) % BLOCK_SIZE)return data + (chr(length) * length).encode()def unpad(data):return data[:-(data[-1] if type(data[-1]) == int else ord(data[-1]))]def bytes_to_key(my_data, salt, output=48):# extended from assert len(salt) == 8, len(salt) my_data += salt key = md5(my_data).digest() final_key = keywhile len(final_key) < output: key = md5(key + my_data).digest() final_key += keyreturn final_key[:output]def encrypt(message, passphrase): salt = Random.new().read(8) key_iv = bytes_to_key(passphrase, salt, 32 + 16) key = key_iv[:32] iv = key_iv[32:] aes = AES.new(key, AES.MODE_CBC, iv)return base64.b64encode(b"Salted__" + salt + aes.encrypt(pad(message)))def decrypt(data, password):if len(data) <= BLOCK_SIZE:return data data = base64.b64decode(data) salt = data[8:16] key_iv = bytes_to_key(password, salt, 32 + 16) key = key_iv[:32] iv = key_iv[32:] cipher = AES.new(key, AES.MODE_CBC, iv)return unpad(cipher.decrypt(data[BLOCK_SIZE:]))if __name__ == '__main__':# data = '-85297962_172051801'# key = '583a01a9ba901a3adda7252ebca42c09'# encrypt_data = 'U2FsdGVkX192df0Gxgia8s93zZp85f9m2nU1VIGU+RZQDtViB1LPBnE0CBWgVDBj'key = '583a01a9ba901a3adda7252ebca42c09'data = '-85297962_172051801'encrypt_data = encrypt(data, key)print encrypt_data# encrypt_data = 'U2FsdGVkX192df0Gxgia8s93zZp85f9m2nU1VIGU+RZQDtViB1LPBnE0CBWgVDBj'decrypt_data = decrypt(encrypt_data, key)print 'decrypt_data:', decrypt_data
For the same string, it is found that the encrypted string is different every time. I don’t have much research on AES, which is strange!
3. Packaging and publishing
If only some functions of Crypto are used, such as the aes decryption we use, you can extract them Required code to avoid breaking into the entire Crypto library.
The strange thing is that there is a problem with the reference path when referencing the dynamic library _AES.pyd. After consulting the information, it turns out that Crypto has programmed its reference path, and its page code reads:
#ifdef IS_PY3Km = PyModule_Create(&moduledef);#elsem = Py_InitModule("Crypto.Cipher." _MODULE_STRING, modulemethods);#endif
and uses py2exe to package and extract it. , it renamed the Crypto\Cipher\_AES.pyd file to Crypto.Cipher._AES.pyd and placed it in the release directory, which is quite insightful
The above is the detailed content of Python--AES encryption and decryption method guidance. For more information, please follow other related articles on the PHP Chinese website!