Crypto module for AES encryption and decryption in Python

Source: Internet
Author: User
Tags decrypt

#Coding:utf8ImportSYS fromCrypto.cipherImportAES fromBinasciiImportB2a_hex, A2b_hexclassPrpcrypt ():def __init__(self, key): Self.key=Key Self.mode=AES. MODE_CBC#encryption function, if text is not a multiple of 16 "encrypted text must be a multiple of 16!" ", that would be a multiple of 16.    defEncrypt (self, text): Cryptor=aes.new (Self.key, Self.mode, Self.key)#The key must be a length of (AES-128), AES-192, or (AES-256) Bytes length. At present AES-128 enoughLength = 16Count=len (text)if(count% length! =0): Add= Length-(count%length)Else: Add=0 Text= text + (' /'*add) Self.ciphertext=cryptor.encrypt (text)#because the strings obtained by AES encryption are not necessarily ASCII character sets, there may be problems when outputting to the terminal or saving        #So here's a unified conversion of the encrypted string into a 16 string        returnB2a_hex (Self.ciphertext)#after decryption, remove the top-up spaces with strip ()    defDecrypt (self, text): Cryptor=aes.new (Self.key, Self.mode, Self.key) Plain_text=Cryptor.decrypt (A2b_hex (text))returnPlain_text.rstrip (' /')     if __name__=='__main__': PC= Prpcrypt ('Keyskeyskeyskeys')#initializing the keyE = Pc.encrypt ("0123456789ABCDEF") d=Pc.decrypt (e)PrintE, D e= Pc.encrypt ("00000000000000000000000000") d=Pc.decrypt (e)PrintE, D
About Crypto.cipher module, importerror:no module named ' Crypto ' solution
Please go to the official website https://www.dlitz.net/software/pycrypto/download Pycrypto.
After downloading, follow the instructions in the "Installation" section of the Readme for Pycrypto installation.

AES has many modes, and this time the CBC mode: Key and IV are generated by a cryptographic key and salt (which is disruptive) by the fixed algorithm (MD5). Then encrypt (plaintext) and decrypt (ciphertext) with key and IV (initial vector, encrypt first plaintext).

The following code implements the idea that encrypted text processing is encrypted in units such as 8*16 bits, and that every 16 bytes of data is encrypted into 16 byte-length ciphertext. In the following code, in order to simplify the code, the keys generated by the key and IV are replaced with 16-bit keys, in fact, can actually be different, but the number of bits can not be the same as not try.

Crypto module for AES encryption and decryption in Python

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.