# Cryptography with Python using Fernet

Let us look at how to encrypt text and files using Python.
For this we are going to be using [Fernet](https://cryptography.io/en/latest/fernet.html) which is a part of python's cryptography package

So let us get right into it

Ok firstly we need to downloaded the cryptography package using `pip`

On Windows:

```
pip install cryptography
```

On Linux/macOS:

```
pip3 install cryptography
```

After we have executed the command in the terminal, we are ready to start coding.

You might want to use your favourite code editor (vscode, sublime text, atom etc...) or any IDE.

Now let us first import the required library

```python
from cryptography.fernet import Fernet
```

Then we are going to define a function that will write the key to a file

```python
def write_key():
    key = Fernet.generate_key() # Generates the key
    with open("key.key", "wb") as key_file: # Opens the file the key is to be written to
        key_file.write(key) # Writes the key
```

We will also write down a function that will help with reading and loading the key into a variable

```python
def load_key():
    return open("key.key", "rb").read() #Opens the file, reads and returns the key stored in the file
```

Now let us take the message we are going to encode as user input and encode it to bytes because that is how fernet works

```python
message = input("Message: ").encode() # Takes the message as user input and encodes it
```

Now we need to write are top secret key to the key file. We are going to run this only once and then comment the line out because we don't want it to write a new key down every time the code is run. 

```python
write_key() # Writes the key to the key file
```
Now we need to load the key i.e. read the key from the key file and store it in a variable called as `key` though you can name it whatever you want

```python
key = load_key() # Loads the key and stores it in a variable
```

Now we need to initialize the fernet object by passing in the key we just loaded

```python
f = Fernet(key)
```

Now let us get to the main part, encrypting the message
Also let us print it out

```python
encrypted_message = f.encrypt(message)
print(encrypted_message)
```

Output:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1618045556166/NkVXFrg-7.png)

You see that the encrypted message cannot be understood by anyone so it is safe now and will be the same unless decrypted with the same key

To decrypt, it is a very similar process
```python
decrypted_message = f.decrypt(encrypred_message)
print(decrypted_message)
```

Output:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1618045676137/GQuPm-UeO.png)
So here, first the message is encrypted and is printed out and then we decrypt it and print it in the next line. If the initial message and the decrypted message are the exact same, our code has worked!!!

So that was it for this tiny blog (and it was my first blog ever so leave down some feedback!!!)

# Video Tutorial:
[Link](https://youtu.be/P2k4f1tu-ss)

%[https://www.youtube.com/watch?v=P2k4f1tu-ss]
