Hướng dẫn python aes encrypt file
Python Encrypt File
Let’s use Python to Encrypt a File with AES! ✨✨ We are going to write some Python Code to encrypt a file on disk using AES and then decrypt the file to retrieve our original plaintext file. We will be using Python 3.8.10 for this example. AES (Advanced Encryption Standard) was originally called Rijndael and is a symmetric block algorithm for encrypting or decrypting data. The standard was established by the U.S. National Institute of Standards and Technology (NIST) in 2001. AES has a fixed block size of 128 bits (16 bytes) and has three different key lengths: 128, 192, or 256 bits long. We are going to use 3 libraries in this example. Two of them may require installation which we can do as follows:
The other module os should come built into Python 3. Now, let’s write our code:
Let’s explain what is happening here:
When the above code executes we will get 2 new files on disk in the same folder as our script in addition to infile.docx:
So that’s it! We have successfully encrypted and decrypted a file in Python. Word of caution though, this is only an example. In practice you should absolutely use stronger and more robust means to encrypt your data, along with a stronger password. Use this code at your own risk! This was for demonstration purposes only. Thanks for reading! Check out our TwoFish Encryption Tutorial HERE. 👌👌👌 I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring. |