Python AES encryption module Usage Analysis, pythonaes encryption module
This example describes the usage of the Python AES encryption module. We will share this with you for your reference. The details are as follows:
AES is a new encryption module. Last time I introduced how to use DES in OpenSSL in C language. This time, let's take a look at how the Python library uses AES for encryption and decryption. In fact, the principles of the two are still very similar, but it is easier to do this in python than in C, but it is a bit cool than in C #/Java. In the C #/JAVA language, padding generally implements the encryption of source data. I also handled this problem in the last C language. In the python library, you also need to handle this by yourself.
From Crypto. cipher import AES # padding algorithm BS = 16pad = lambda s: s + (BS-len (s) % BS) * chr (0) unpad = lambda s: s [0: -ord (s [-1])] # convert a string to a binary buff block def parse_hex (hex_str): l = int (math. ceil (len (hex_str)/2) buf = ''for I in range (0, l): s = hex_str [(I * 2) :( (I + 1) * 2)] buf = buf + chr (int (s, 16) return buf # parse encrypted keykey = parse_hex ("encrypted") iv = parse_hex ("encrypted ") # create an AES object aes_obj = AES. new (key, AES. MODE_CBC, iv) # Do byte alignment padding_zero = pad (raw_buf) # Start encryption encrypt_buf = encrypt (padding_zero) # decrypt buff = aes_obj.decrypt (encrypt_buf)
In this Code, padding is still very beautiful. If you implement this in C, you still need to write a lot.
PS: if you are interested in encryption and decryption, refer to the online tools on this site:
Online text encryption and decryption tools (including AES, DES, and RC4 ):
Http://tools.jb51.net/password/txt_encode
MD5 online encryption tool:
Http://tools.jb51.net/password/CreateMD5Password
Online hash/hash algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tools:
Http://tools.jb51.net/password/hash_md5_sha
Online sha1/shaloud/sha256/sha384/sha512 encryption tool:
Http://tools.jb51.net/password/sha_encode