Example of implementing AES encryption in python3.6 (pyCryptodome ),

Source: Internet
Author: User
Tags gz file

Example of implementing AES encryption in python3.6 (pyCryptodome ),

Cause

The Python warehouse receiving script written on the front end reads the content of the configuration file (including the ip address of the database, the user name of the database, and the password of the database ), because the database password in the configuration file is displayed in plain text, it is not safe and therefore encrypted.

Coding path

Programming Environment

  1. Python3.6
  2. Third-party library-pyCryptodome

Introduction and download of third-party Libraries

1. in the previous AES encryption, python2 or 3.4 used the pyCyrpto module. However, it failed to be installed after a great deal of waste yesterday, after a large number of queries, it is found that the library has stopped maintenance. During the installation process, although pip install pycryto can be used to download the corresponding tar.gz file, an error will be reported during automatic installation, it is said that there is no tool environment for Microsoft 2014.

2. after further reading, we found that the subsequent branches of the pyCyrpto library have a library called pyCryptodome, which is an extension of the previous generation. The following provides a comprehensive address, if pip cannot be installed, you can download the library from this website.

Https://www.lfd.uci.edu /~ Gohlke/pythonlibs/

Of course, I also recommend using the following command to download and install pip, which is easy to use...

pip install pycryptodome

The verification code starts.

Not much nonsense. Go to the demo directly.

# AES-demo "@ author: sy @ file: python_AES.py @ time: @ desc: AES encryption" from Crypto. cipher import AES # key. Here, you need to convert the string into a byte key = B 'abcdefgh' # The encrypted content must contain up to 16 characters. Therefore, we need to splice the space into def pad (text ): while len (text) % 16! = 0: text + = B 'return text # the encryption key must be up to 16 characters long. Therefore, use a space to splice def pad_key (key): while len (key) % 16! = 0: key + = B ''return key # encryption algorithm. In ECB mode, the 16-bit key is uploaded to aes = AES. new (pad_key (key), AES. MODE_ECB) # encrypt the content. Here, convert the string to byte text = B 'woshijiamineirong' # splice the content with 16 characters and pass it into the encryption class. The result is byte-type encrypted_text = aes. encrypt (pad (text) print (encrypted_text) # This is to verify whether the bytes can be converted into strings and then decrypted successfully. # In fact, a is encrypted_text, that is, the encrypted content a = B '\ xb9K \ xe8 _. q \ x1c! \ X9f \ xa2 \ xc8 \ x06 \ xf5 \ xc1 \ xd07 '# Use the aes object for decryption and convert the byte type to the str type. The error code does not include de = str (aes. decrypt (a), encoding = 'utf-8', errors = "ignore") # obtain the string length from str 0 to text content. Print (de [: len (text)])

Conclusion

For AES encryption, we can see that the project has been used in java, but for Python, this is a first experience ..... It's a long experience .... The embarrassment is that the encrypted format is Byte ...... Maybe there is something wrong with where I wrote it. If so, I hope someone else will point it out ..

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.