## WHY DO WE NEED TO KNOW HOW TO ENCRYPT AND DECRYPT DATA IN PYTHON?

**FIRSTLY WHY DO WE NEED ENCRYPTION?**: Ever heard of CIA Triad before? It’s a common model that forms the basis for the development of security systems. The CIA which stands for ** Confidentiality, Integrity and Availability** is also used in analyzing vulnerabilities and finding solutions for them

In the light of the CIA approach, we can confidently say that encryption falls under the aspect of confidentiality. Our previous **post** before this was on hashing of data which falls under integrity as well, but we will stick to confidentiality in this post.

For some good reasons not everyone is supposed to have access to some certain information. If you are also familiar with the man in the middle attack concept you’ll appreciate the reason for confidentiality.

**ENCRYPTION: **In a nutshell, simply means the process of encoding(converting) data like strings or other form of data into cipher-text(unreadable format) using a key(encryption key).

**Decryption **on the other hand is the process of decoding the encrypted data into readable format using the same encryption key or a specified decryption key.

The aim is to keep the information which is being stored or transferred secret, only to be accessed by the right or designated user. This designated user will also have access to the encryption key which can be used to decrypt the information/data.

**THERE ARE 2 TYPES OF ENCRYPTION: Symmetric-key and Asymmetric-key encryption**. Symmetric-key encryption is when the same key is used for both encryption and decryption, while in Asymmetric-key encryption you use two different keys for encryption and decryption accordingly.

However, symmetric-key encryption is basically easier to use than asymmetric-key but less secure than asymmetric-key encryption.

There are some tools other than python which we can use for encryption, like CLI based program. However, with python we can write reusable codes which we can integrate in our future programs.

As a matter of fact, we have earlier written a **post **on the popular Caesar cipher encryption, which is also based on *how to encrypt and decrypt data in python,* you might want to check that out as well.

## WRITING A FUNCTION TO ENCRYPT AND DECRYPT DATA IN PYTHON:

Without further ado, let’s dive into coding things up with python. Firstly I’d like to add that we will only be discussing the asymmetric-key function since it’s more secure.

As you already know, we’ll be needing two different keys for encryption and decryption accordingly. The idea is that we can send only the encryption key to anyone without having to bother about it’s secrecy.

Firstly to encrypt and decrypt data in python we are going to be needing a module called **rsa**. So we ** pip install **from the CLI(pip install rsa), then we import it into our code.

```
#importing rsa
import rsa
```

Now let’s try encrypting and decrypting a simple string in python, we will use *public key* for encryption and use *private key* for decryption:

```
import rsa
msg = b'The president of the club will arrive at the hotel by 12 noon, make the arrangements '
public_key, private_key = rsa.newkeys(1000)
print(f'The public key is:\n{public_key}')
print(f'\n{type(public_key)}\n')
print(f'\nThe private key is:\n{private_key}')
print(f'\n{type(private_key)}')
encmsg = rsa.encrypt(msg, public_key)
print (f'\n\nThe encoded message is:\n{encmsg}')
decmsg = rsa.decrypt(encmsg, private_key).decode()
print(f'\n\nThe decoded message is:\n{decmsg}')
```

**OUTPUT:**

```
The public key is:
PublicKey(6500050540568313544191305683592734337070238636789232298393308704584858254212594256696757684653150523013819672213504838725515222426505257123693720641977268952578998732852693057875235266129751362502672597464208664648729590231235543527904033969646521884523462957869389066099561291444177389907569675584619, 65537)
<class 'rsa.key.PublicKey'>
The private key is:
PrivateKey(6500050540568313544191305683592734337070238636789232298393308704584858254212594256696757684653150523013819672213504838725515222426505257123693720641977268952578998732852693057875235266129751362502672597464208664648729590231235543527904033969646521884523462957869389066099561291444177389907569675584619, 65537, 689509000381935635735812702936306042560878572454624760645593818976668669351449643293953940883908363763859718345793759842833541759755016975508219367206589826717924862830525298253425286877478343286864751513679834229423375921052765919610516778131613958533540501526613384263510043876619482119788465285889, 5260821980393596764667413182094651454217998926835847273655199085026174961983413904181966254891439774913197999417699622791555892797915623221422876807597911447217, 1235557972650122238805957351175270120954219444770047814807196502825280369457582962223026992065969916126500986236389951084950859520487205817307)
<class 'rsa.key.PrivateKey'>
The encoded message is:
b"\x90q\x9c@u\xe0\xbb\x17\x8bDSm\x8a\xb6\xcfb\x0b\x95\x17\xdc_\x85\x14\xd4Jj\x03bZ9\x92\xaaK(\xdf\x1d\x85\xa3I\r\xcdf\xcek3\x8c\x00\xd2\xc0\xa1\xec[B\x8b#\xdfa2\x85\x0c\x1c\x8b`WyT\xd4\x8b&\\\xb2\x14hw\xe6\xf7! c;z\x1a\xd6\xe1\xc3\xf8\x8bn\xb0\xcfr\xb5\xda<\xc8\xebs\xb0\x8e\xfe\x01,\xf3'\x91\x1el\xb3\x07\xb6\x8b*h\xb9XkX\x08t%x\x9f8I\xa1"
The decoded message is:
The president of the club will arrive at the hotel by 12 noon, make the arrangements
```

**LET’S EXPLAIN THE CODE:**

Firstly I’d like to assume that you already have the basic knowledge of python, so you understand python simple concepts like f-string and printing.

You can only encrypt encoded data, so that was why we have our msg in bytes format. Basically the rsa.newkeys generates two values(publickey and privatekey). It takes key length as a parameter which should be at least 16.

We generated the two keys(publickey and privatekey) using 1000 as key length.

We can send the public key to anyone to encrypt information for us and send back to us, then we decrypt with our private key, so now you can see why asymmetric-key encryption is a better choice.

Then we encrypted the message with the public key and decrypted with the private key. Every other statement like printing with f-string is self-explanatory.

You can take things even further by encrypting strings or texts inside a file and also decrypt it. You just open the file in binary mode, read and assign the contents to a variable then encrypt.

You can also decrypt using the same format, only that you’d need to use the right key for each operation.

To get clues on how to open files in binary mode, check our previous **post** which was on hashing.

If you have questions on this tutorial, please don’t hesitate to ask me in the comment section, **chat me up** or **write me** directly. To also see more codes similar to * encrypt and decrypt data in python *kindly check our tutorial post

**.**

*page*