Encryption and Decryption of Strings in Python
In this tutorial, we will learn about encryption and decryption of strings in Python.
Encryption is the process of converting plain text into ciphertext. Ciphertext is not readable until it is converted to plain text. A secret key is required for encryption.
Decryption is the process of converting ciphertext into plain text. A secrte key is required for decryption.
Types of Encryption
There are two types of encryption:
- Symmetric Encryption - Symmetric encryption uses a single key to encrypt and decrypt a text. When communicating using the symmetric encryption, the key needs to be shared so that the person who is receiving the text is able to decrypt it for reading. Symmetric encryption is old technique for encrypting and decrypting a text. Some of the symmetric key encryption algorithm are AES (Advanced Encryption Standard), DES (Data Encryption Standard), etc.
- Asymmetric Encryption - Asymmetric encryption uses two keys - public key and secret key. A public key is made available to anyone who might want to encrypt a text. Anyone with a secret key can decrypt the encrypted text. The secret key is kept secret or exchanged over the internet. A text that is encrypted using the public key can also be decrypted using a secret key. Asymmetric encryption is relatively new technique to encrypt and decrypt a text. Some of the asymmetric key encryption algorithm are DSA, RSA, PKCS, ELGamal, ECC, Diffie-Hellman, etc.
Asymmetric encryption is considered to be more secure than symmetric encryption because asymmetric encrytion uses two keys for encryption and decryption.
Symmetric Encryption Example
Install the Python cryptography library using the following command:
pip install cryptography
The following code shows how to encrypt and decrypt a string using the Symmetric Encryption technique:
from cryptography.fernet import Fernet # Put this somewhere safe! key = Fernet.generate_key() def encrypt_text(plain_text): f = Fernet(key) encrypted_text = f.encrypt(bytes(plain_text, "UTF-8")) return encrypted_text.decode() def decrypt_text(encrypted_text): f = Fernet(key) return f.decrypt(bytes(encrypted_text,"UTF-8")).decode() #Testing plain_text = "Hello, this is an important message" encrypted_text = encrypt_text(plain_text) print("Encrypted message = %s" %(encrypted_text)) decrypted_text = decrypt_text(encrypted_text) print("Decrypted message = %s" %(decrypted_text))
Decrypted message = Hello, this is an important message
Asymmetric Encryption Example
Install the Python rsa library using the following command:
pip install rsa
The following code shows how to encrypt and decrypt a string using the Asymmetric Encryption technique:
import rsa public_key, private_key = rsa.newkeys(512) def encrypt_text(plain_text): plain_text = plain_text.encode('utf8') encrypted_text = rsa.encrypt(plain_text, public_key) return encrypted_text def decrypt_text(encrypted_text): decrypted_text = rsa.decrypt(encrypted_text, private_key) return decrypted_text.decode("utf-8") # testing plain_text = "Hello this is an important message" encrypted_text = encrypt_text(plain_text) print("Encrypted text is = %s" %(encrypted_text)) decrypted_text = decrypt_text(encrypted_text) print("Decrypted text is = %s" %(decrypted_text))
Decrypted text is = Hello this is an important message