Encryption is the process of converting plain old data or information into a secret message such that it is meaningless to the casual observer. Some useful terminologies as follows:
Plain Text – This is the actual data that can be understood by all users.
Cipher Text – This is the encrypted data that seems to be meaningless but hides the actual data.
Encryption – Also called encoding, this is the process of using mathematical algorithms or other means to encrypt plain text into cipher text. Generally a key is used to encrypt plain text and the key is shared securely between the sender and receiver.
Decryption – Also called decoding, this is the process of getting back the actual readable data or plain text from the cipher text. The user needs a valid key to decrypt a cipher text.
Python has many powerful libraries available for providing out of the box encryption decryption facilities. So, using only a few lines of code you can easily create a simple encryption and decryption application in python within minutes. You can also create your own customized library or choose from already available ones, each of them having their own pros and cons. Some of the popular packages available are:
Hashlib (MD5 & SHA1)
Steps to Encrypt & Decrypt in Python
No matter which library you use the following steps can help you get started:
- Install the library in python using the command
pip install <library name>
- Import the library in your python application. Example:
from simplecrypt import encrypt, decrypt
- Decide on the key – Most encryption algorithms also require you to pass a key. This key can be generated once, saved in a file and reused again. The key can be shared between the sender and receiver. This way the sender can encrypt the data using the key and the receiver can decrypt and read the data using the same key. The key can be a sequence of characters and must be stored and shared securely.
- Each library will have its own set of encrypt and decrypt method. You can look at the documentation for the specific library to get the details on the usage and features provided by that particular library.
You need to decide on which encryption library to use based on several factors. The stronger the encryption algorithm the more time and processing power it will consume to encrypt and decrypt. On the other hand, while simple encryption libraries are faster they are also less secure.