Encryption is a fundamental concept in the realm of security and privacy. It ensures that sensitive data remains confidential and is protected from unauthorized access or eavesdropping.
Plain Text: This is the original, readable message or data fed into the encryption process.
- Encryption Algorithm: This is a method or formula used for turning plain text into an encrypted message.
- Encryption Key: This is a piece of information, often a string of random characters, used with the encryption algorithm to convert the plain text into an encrypted form. The security of encrypted data is largely dependent on the strength and secrecy of the encryption key.
- Cipher Text: This is the scrambled or unreadable output produced after encrypting the plain text. Without the appropriate decryption key, this text should ideally be meaningless and unreadable.
Decryption: This is the reverse process of encryption. It involves converting the cipher text back into its original form (plain text) using a decryption key, which may or may not be the same as the encryption key, depending on the encryption scheme used.
In this method, the same key is used for both encryption and decryption. AES (Advanced Encryption Standard) is a commonly used symmetric encryption algorithm.
The data is encoded and decoded in symmetric-key encryption with the same key. This is the easiest way of encryption, but also less secure. The receiver needs the key for decryption, so a safe way needs to transfer keys. Anyone with the key can read the data in the middle.
Install the Python cryptography library with the following command:
pip install cryptography
from cryptography.fernet import Fernet message = "Database is key" key = Fernet.generate_key() fernet = Fernet(key) encMessage = fernet.encrypt(message.encode()) print("original string: ", message) print("encrypted string: ", encMessage) decMessage = fernet.decrypt(encMessage).decode() print("decrypted string: ", decMessage)
original string: Database is key encrypted string: b'gAAAAABk0i99dYJbzSIKAdADPSVTP6eR3jPRgArfPblpaYyRrQzgiy_j BsGoav74wjpSVSRfLhWqp_p1z0h8x6ktbHcQLevF7w==' decrypted string: Database is key
This method involves two keys: a public key and a private key. The public key is used for encryption, while the private key is used for decryption. RSA (Rivest–Shamir–Adleman) is a widely used asymmetric encryption algorithm.
The public key is used to encrypt the data, and the private key is used to decrypt the data. By the name, the public key can be public (can be sent to anyone who needs to send data). No one has your private key, so no one in the middle can read your data.
Install the Python rsa library with the following command.
pip install rsa
import rsa publicKey, privateKey = rsa.newkeys(512) message = "Database is key" encMessage = rsa.encrypt(message.encode(), publicKey) print("original string: ", message) print("encrypted string: ", encMessage) decMessage = rsa.decrypt(encMessage, privateKey).decode() print("decrypted string: ", decMessage)
I hope this helps!