You can encrypt and decrypt a text file in Python using the cryptography
library with symmetric encryption (e.g., Fernet) by reading the file’s content, encrypting or decrypting it, and then saving the result to a new file.
Key takeaways:
Encryption converts plaintext into ciphertext, ensuring data security during transfer.
Decryption converts ciphertext back to the original plaintext.
A single key is used for both encryption and decryption in symmetric encryption.
In contrast, asymmetric encryption uses a private key for decryption and a public key for encryption.
The
cryptography
package in Python simplifies encryption and decryption operations.Secure key management is crucial for maintaining the confidentiality of encrypted data.
Fernet encryption generates a key used to encrypt and decrypt data.
Without the correct key, data cannot be decrypted successfully.
Encryption and decryption play a vital role in protecting sensitive information. Encryption transforms plaintext into an unreadable format (ciphertext) using a key, while decryption reverses the process, converting ciphertext back into plaintext with the appropriate key. These techniques ensure that intercepted data remains inaccessible without the correct key.
In this Answer, we will explore how to perform text encryption and decryption in Python.
While sending data from your network to resources that are placed outside of your network, you might worry that the data is visible to outsiders during the transfer. One of the solutions is to encrypt your data before sending it to any resource that is outside your network. To encrypt your data means to make it so that even if someone can view your data, they will not be able to understand it. This process is called cryptography. Let’s define the basics of cryptography, which are encryption and decryption:
Encryption is the process of converting original text to random and meaningless text called ciphertext.
Decryption is the process of converting ciphertext back to the original text.
Encryption algorithms can be broadly classified into symmetric and asymmetric:
A single key is used for both encryption and decryption in symmetric encryption. This makes it efficient but necessitates safe key distribution because the same key is used to both encode and decode the data.
Example: A common example of symmetric encryption is AES (Advanced Encryption Standard), which is widely used for securing files, communications, and databases.
In contrast, asymmetric encryption uses two distinct keys: a private key for decryption and a public key for encryption. By enabling anyone to encrypt data using the public key and guaranteeing that only the intended recipient, who has the private key, may decrypt it, this method improves security.
Example: RSA (Rivest-Shamir-Adleman) is a well-known example of asymmetric encryption and is frequently used to secure digital signatures, emails, and SSL/TLS protocols.
In Python, we often use libraries like cryptography
for handling these operations.
We’ll use a Python package called cryptography
to perform encryption-decryption of text. Before that, you can run the following command to install the package:
pip install cryptography
Now let’s have a look at an example that will show us how we can encrypt and decrypt data:
from cryptography.fernet import Fernetkey = Fernet.generate_key()print('Your Key: ', key.decode())f = Fernet(key)data = b"A really secret message. Not for prying eyes."encrypted_data = f.encrypt(data)print(encrypted_data.decode())decrypted_data = f.decrypt(encrypted_data)print(decrypted_data.decode())
Line 1: We import the required package.
Line 3: We use Fernet
to generate a key that will be used to encrypt your data. (You need to securely save this key as this is the most important part of encrypting your data. If anyone gets this key, they will be able to decrypt your data).
Line 4: We print the generated key.
Line 6: We create a Fernet
object using that key.
Lines 8–10: We encrypt our data using the Fernet
object that we created in the above step.
Lines 12–13: We decrypt our data using the Fernet
object that we created in the above step.
So, if you do not create a Fernet
object using the correct key, you will not be able to decrypt your data. In this way, we can easily transform the original data to ciphertext and decode it on the receiver’s end with the above method.
Let’s attempt a short quiz to test our understanding of the concepts learned in this Answer.
Which option describes the process of encryption?
Converting ciphertext back to the original text
Converting plaintext into scrambled and meaningless text
Generating a key for decryption
Using a public key for encryption
Learn the basics with our engaging course!
Start your coding journey with the “Learn Python” course—the perfect course for absolute beginners! Whether you’re exploring coding as a hobby or building a foundation for a tech career, this course is your gateway to mastering Python—the most beginner-friendly and in-demand programming language. With simple explanations, interactive exercises, and real-world examples, you’ll confidently write your first programs and understand Python essentials. Our step-by-step approach ensures you grasp core concepts while having fun along the way. Join now and start your Python journey today—no prior experience is required!
Encryption and decryption are fundamental techniques for securing sensitive information. By using libraries like cryptography
, you can easily implement these processes in Python. However, secure key management is essential for the overall security of your system. Always ensure that encryption keys are stored and transmitted securely to maintain the confidentiality of your data.
Haven’t found what you were looking for? Contact Us