Crypto another two pieces of encrypted decryption code

Source: Internet
Author: User
Tags decrypt

The first piece of code style-straightforward:

Import sysfrom crypto.cipher import aesfrom binascii import B2a_hex, A2b_hexclass Prpcrypt (): 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 def encrypt (self, text): Cryptor = Aes.new (Self.key, Self.mode, Self.key) # Here the key must be 16 long ( AES-128), (AES-192), or (AES-256) Bytes length. The current AES-128 is sufficient with length = Count = Len (text) add = Length-( Count% length) Text = text + (b ' + ' * add) Self.ciphertext = Cryptor.encrypt (text) # Because of the string that gets when AES encrypts Not necessarily the ASCII character set, the output to the terminal or the storage may be problematic # So here's a uniform conversion of the encrypted string into a 16 binary string return B2a_hex (self.ciphertext) # decryption, remove the top-up space with Strip () Remove Def decrypt (self, text): Cryptor = Aes.new (Self.key, Self.mode, self.key) Plain_text = Cryptor . Decrypt (A2b_hex (text)). Decode () return Plain_text.rstrip (' + ') if __name__ = = ' __main__ ': PC = Prpcrypt (b ' Keyske Yskeyskeys ') # Initialize key E = Pc.encrypt (b "qweqweqweQwe ") d = Pc.decrypt (e) print (E, d,type (d), Len (d)) E = Pc.encrypt (b" ADASDQWEASDADQWEASD ") d = Pc.decrypt (e) Print (E, d,type (d), Len (d))

the second paragraph-or straightforward:

From Crypto.cipher import aesfrom Crypto import randomfrom binascii import b2a_hex# to encrypt clear text data = ' East of West ' # key must be a length of (AES -128), (AES-192), or (AES-256) Bytes length. Key = B ' This is A + key ' # generates a non-repeatable key vector of length equal to the AES block Size IV = RANDOM.NEW (). Read (Aes.block _size) # Initializes the AES object with key and IV using MODE_CFB mode mycipher = Aes.new (Key, AES. MODE_CFB, IV) # Encrypted clear text length must be a multiple of 16, if the length is not a multiple of 16, you need to top up to a multiple of 16 # to add IV (key vector) to the beginning of the encrypted cipher, together transfer ciphertext = IV + MYCIPHER.ENCRYPT ( Data.encode ()) # decrypt the words to generate a new AES object with key and IV Mydecrypt = AES.NEW (Key, AES. MODE_CFB, Ciphertext[:16] # using the newly generated AES object, decrypt the encrypted ciphertext Decrypttext = Mydecrypt.decrypt (ciphertext[16:]) print (' Key k: ', key) Print (' IV is: ', B2a_hex (ciphertext) [: +]) print (' Encrypted data: ', B2a_hex (ciphertext) [[+]]) print (' Decrypted data: ', Decrypttext.decode ())

Crypto another two pieces of encrypted decryption code

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.