top of page

Encryption/Decryption Files

How to encrypt and decrypt files in Python

When you need to encrypt text, it is important to understand the different options you have. There are different ways to encrypt text, like using a password, using a key, and using a symmetric cipher. When you use a password, you are encrypting the text with an algorithm that uses a password to make text unreadable. A key is another way of encrypting text. A key is a piece of information that is used to encrypt and decrypt text. With a key, you must know the key in order to decrypt text. Using a symmetric cipher is another way to encrypt text. A symmetric cipher encrypts data using the same key.

Using Python and the symmetric cryptography function ciphers, you can encrypt and decrypt text files or any other files. This is one of the simplest ways to encrypt and decrypt files.Encrypting files is a difficult task, but you can use Python to do it. You will have to use a password to encrypt the file. You will also have to use a salt to encrypt the file. A salt is a random string of letters and numbers that is used to encrypt your file. Here is how you can encrypt a file using Python.

Encryption is a process that allows you to transform information, typically in a way that makes it unreadable to people who do not have the key.

Required Modules:

pip install cryptography

Cryptography is a package which provides cryptographic recipes and primitives to Python developers. The main goal is for it to be your “cryptographic standard library”. It supports Python 3.6+ and PyPy3 7.2+.

Cryptography includes both high level recipes and low level interfaces to common cryptographic algorithms such as symmetric ciphers, message digests, and key derivation functions.


from cryptography.fernet import Fernet

key = Fernet.generate_key()

with open('filekey.key', 'wb') as filekey:

with open('filekey.key', 'rb') as filekey:
	key =

fernet = Fernet(key)

with open('file-name.txt', 'rb') as file:
	original =

encrypted = fernet.encrypt(original)

with open('file-name.txt', 'wb') as encrypted_file:


from cryptography.fernet import Fernet

with open('filekey.key', 'rb') as filekey:
    key =

fernet = Fernet(key)

with open('file-name.txt', 'rb') as enc_file:
	encrypted =

decrypted = fernet.decrypt(encrypted)

with open('file-name.txt', 'wb') as dec_file:

file-name.txt normal file:

file-name.txt encrypted:




32 views0 comments

Recent Posts

See All

Mobile | Secret Phone Codes

Our smartphones have become extensions of ourselves, facilitating much of our communication and storing troves of private data. If someone wants to monitor your activity, tapping your phone is a viabl

Mouse & Keyboard Bot

This article illustrates how to automate mouse and keyboard movements using pyautogui module in python. This module is not preloaded with python. So to install it run the following command: The code s

Edit a Website with a Java console

Edit a website – even if it’s not yours! This is how you can edit any website directly from your browser: #1) Open a Webpage; #2) Right-click on any spot in the website, select “Inspect Element”; #3)


bottom of page