Encryption and Decryption using Python
In this tutorial, I am going to show you how can we encrypt and decrypt text using python:
Here is the code:
Create->. Main.py
copy and pest below code
alphabet = [
' ','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',' ','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
]
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
def encrypt(text, shift):
cyper_text = ""
for letter in text:
position = alphabet.index(letter)
new_position = alphabet[position + shift]
cyper_text += new_position
print(cyper_text)
def decrypt(text, shift):
decyper_text = ""
for letter in text:
position = alphabet.index(letter)
new_position = alphabet[-27 + position
- shift]
decyper_text += new_position
print(decyper_text)
if direction == 'encrypt':
encrypt(text=text, shift=shift)
else:
decrypt(text, shift)
Comments
Post a Comment