Today, we see data from a variety of sources, and the final problem is making sure of its security and privacy.
This includes not just large datasets held by organizations, but also AI/ML models, algorithms, and, eventually, data with projections.
The volume of data is increasing as the number of companies using data science approaches in their decision-making expands.
There have been various organizations discovered in the field of encryption and security for AI/ML and cloud computing, which are now allowing calculations on encrypted data.
In the ever-changing world of data and information communication, one of the most pressing problems for businesses is the security of file contents.
Some information (emails, logins) can be password secured.
However, other information sent by email or FTP is inefficient if protected by a keyword.
This is where file encryption comes into play, providing the security and convenience that parties involved in information transfers require.
What is file encryption?
Individual files or file systems are protected by file encryption, which encrypts them with a unique key and makes them available only to the keyholder.
The purpose is to prevent harmful or unauthorized individuals from gaining access to files on the hard drive.
An operating system or file system can provide file encryption support. The sensitive files can only be accessed with a decryption key.
If a user has to communicate individual files securely over the Internet or save them on portable media such as a USB stick, file encryption comes in handy.
Cryptography is the term for the process of encrypting and decrypting data.
Let’s look at how we can use Python to encrypt and decode some of our data. We’ll use symmetric encryption, which means we’ll encrypt and decrypt the data with the same key.
To go along with this tutorial, we’ll need the Python library for cryptography.
Steps for encrypting & decrypting a file
We will be following the steps given below.
- Installing library
- Creating a key
- Loading a key
- Encrypting a file
- Decrypting a file
1. Installing library
Please open “Command Prompt” (on Windows) and type the following code to install them:
We’ll need an example file to work with to get started. Here’s a sample .csv file including information about students’ grades.
3. Creating a key
We’ll use a symmetric equation in our example. Fernet is a type of authenticated encryption that requires a “key” to read and/or alter a file. Now we’ll make the key and put it in the same directory as our data file:
If you go in the directory where your Python code is located, you should find the mykey.key file. The file should only have one line, which is a string of characters in some sequence. You can look at my key below, but yours will be different.
4. Loading a key
We would need to load the encryption key into our environment once we have produced it in order to encrypt/decrypt the files. The following step is fairly straightforward, requiring only the opening of the mykey.key file and its storage in local memory:
The encryption key is now locally saved as the key variable.
5. Encrypting a file
We’ll construct a function to use the encryption key and return the encrypted file now that we have the file to encrypt and the encryption key. We store the Fernet object as a local variable f when we create it.
Following that, we imported our original data (grades.csv) into the original. The data is then encrypted with the Fernet object and stored as encrypted.
Finally, we save it as “enc_grades.csv” in a new.csv file. The encrypted file can be viewed here:
6. Decrypting a file
You’ll want to access the file after you’ve encrypted it and, for example, successfully moved it to another place. That information is now in encrypted format.
The next step is to restore the original material by decrypting it. The procedure we’ll use now is the inverse of the encryption we used in the previous section.
We’ll follow the identical steps as before, but this time we’ll go from an encrypted to a decrypted file:
Finally, we save it as “dec_grades.csv” in a new.csv file. The encrypted file can be seen below:
We learned how to encrypt and decode a file and the data contained inside it using the symmetric type of file encryption in this post using the Python programming language and the cryptography package.
Encryption and decryption of files is a simple process using this library.
We don’t need to use our logical method.
Instead, we can generate a key, encrypt the file, and then decode it using the key – it’s secure and straightforward.