Encryption in Python
An encryption algorithm is a mathematical process used to convert plaintext (unencrypted data) into ciphertext (encrypted data). The purpose of encryption is to protect the confidentiality of data by making it unreadable to anyone except those who have the decryption key. Encryption algorithms are a key element of modern computer and communication systems, as they allow individuals and organizations to securely transmit and store sensitive information.
Encryption algorithms can be classified into two main categories: symmetric and asymmetric. Symmetric algorithms use the same key for both encryption and decryption, while asymmetric algorithms use a pair of keys: a public key for encryption and a private key for decryption. There are many different encryption algorithms available, each with its own set of strengths and weaknesses. The choice of algorithm depends on the specific security needs of the system being used.
In this tutorial, you will learn how to create a simple encryption algorithm in Python using the Caesar cipher method:
Caesar Cipher
Caesar cipher is a simple substitution cipher named after Julius Caesar, who used it to communicate with his officials. It is a type of symmetric-key algorithm, which means that the same key is used for both encryption and decryption.
In a Caesar cipher, each letter of the plaintext message is shifted by a certain number of positions in the alphabet. For example, if the key is 3 and the plaintext message is "hello", the encrypted ciphertext would be "khoor". To decrypt the message, the recipient simply shifts the letters back by the same key value.
While the Caesar cipher is very easy to implement, it is not secure and can be easily broken by a cryptanalytic attack. There are many more advanced and secure encryption algorithms available that should be used instead.
Encryption Code
This Python function takes in a plaintext message and a key (an integer value) and returns the encrypted ciphertext. The algorithm works by shifting each letter of the plaintext message by the key value. For example, if the key is 3 and the plaintext message is "hello", the encrypted ciphertext would be "khoor".
# Define the encryption function
def encrypt(plaintext, key):
ciphertext = ""
for letter in plaintext:
if letter.isalpha():
shift = ord(letter) + key
if letter.isupper():
if shift > ord('Z'):
shift -= 26
elif shift < ord('A'):
shift += 26
else:
if shift > ord('z'):
shift -= 26
elif shift < ord('a'):
shift += 26
ciphertext += chr(shift)
else:
ciphertext += letter
return ciphertext
# Prompt user for a message and a key
pt = input("Type the plaintext message: ")
k = input("Enter a key value: ")
# Call the encryption function providing message and a key
ct = encrypt(pt, int(k))
print(ct)
Keep in mind that this is a very basic example of an encryption algorithm and is not secure for real-world use. There are many more advanced and secure encryption algorithms available.
Decryption Code
This Python function takes in a ciphertext message and a key (an integer value) and returns the decrypted plaintext. The algorithm works by shifting each letter of the ciphertext message back by the key value. For example, if the key is 3 and the ciphertext message is "khoor", the decrypted plaintext would be "hello".
# Define the decryption function
def decrypt(ciphertext, key):
plaintext = ""
for letter in ciphertext:
if letter.isalpha():
shift = ord(letter) - key
if letter.isupper():
if shift > ord('Z'):
shift -= 26
elif shift < ord('A'):
shift += 26
else:
if shift > ord('z'):
shift -= 26
elif shift < ord('a'):
shift += 26
plaintext += chr(shift)
else:
plaintext += letter
return plaintext
# Prompt user for a message and a key
ct = input("Type the ciphertext message: ")
k = input("Enter the key value: ")
# Call the decryption function providing ciphertext and key
pt = decrypt(ct, int(k))
print(pt)