Python uses the M2crypto module for AES encryption

Source: Internet
Author: User
Tags decrypt

AES (Encryption Standard, Chinese: Advanced Encryption Standard), is a block encryption standards. AES is processed by dividing the raw data into multiple 4x4 byte matrices, with a pre-defined key that makes each byte in each byte matrix XOR, replaced, shifted, and linearly transformed to achieve the purpose of encryption. The key length can be 128,192 or 256 bits.
Here is an example of using the Python M2crypto library and using the AES_128_ECB algorithm for encryption and decryption. Let's start by introducing a few key points:
1, iv (initialization vector), that is, the initialization vector, to avoid the same data multiple encryption will produce the same ciphertext. The maximum length is 16 bytes, beyond the 16-byte portion is ignored, preferably randomly generated to increase the strength of the encryption.
2. ECB (Electronic CODEBOOK,ECB), which encrypts each 4x4 byte matrix with the same key and does not use IV. The advantage is that each byte matrix can be encrypted independently, so each byte matrix can be encrypted at the same time, and the disadvantage is that the ciphertext after encryption is the same for the related data.
3, Padding, because AES is a 4x4 byte matrix as a unit for processing, because the data to be encrypted must be a multiple of 16, if less than a multiple of 16, will be filled operation. The AES_128_ECB algorithm encrypts the default fill mode of PKCS5.

From M2CRYPTO.EVP import cipherfrom m2crypto import m2from m2crypto Import utilencrypt_op = 1 # encryption Operation Decrypt_op = 0 # decryption Operation I v = ' + ' * 16 # Initialize variable, useless for AES_128_ECB algorithm private_key = ' dd7fd4a156d28bade96f816db1d18609 ' # key def Encrypt (data):  ' using AES _128_ECB algorithm for data encryption '  cipher = cipher (ALG = ' AES_128_ECB ', key = Private_key, IV = IV, OP = encrypt_op)  buf = Cipher.up  Date (data)  buf = buf + cipher.final ()  del cipher  # streams clear text from byte to 16  output = ' For I in  buf:    output + = '%02x '% (ord (i))  return outputdef Decrypt (data):  ' decryption with the AES_128_ECB algorithm '  # Convert ciphertext from 16 to byte stream  data = UTIL.H2B (data)  cipher = cipher (ALG = ' AES_128_ECB ', key = Private_key, IV = IV, OP = decrypt_op)  buf = Cipher.up Date (data)  buf = buf + cipher.final ()  del cipher  

For more information, refer to the following links:
Http://ijecorp.blogspot.com/2013/08/python-m2crypto-aes-encrypt-decrypt.html

Python uses the M2crypto module for AES encryption

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.