Here's my blog post to end my hiatus. I decided to make a program that encrypts and decrypts text given in the form of a Caesar cipher. So I did just that in 43 lines of Python (the main portion in 27 lines). On Pastebin like usual.
alphabet = "abcdefghijklmnopqrstuvwxyz"
def encrypt(s, shift):
news = ""
for i in s:
if i.lower() in alphabet:
if i.upper() == i:
c = alphabet[(alphabet.index(i.lower()) + shift) % len(alphabet)]
news = news + c.upper()
else:
c = alphabet[(alphabet.index(i) + shift) % len(alphabet)]
news = news + c
else:
news = news + i
print(news)
def decrypt(s, shift):
news = ""
for i in s:
if i.lower() in alphabet:
if i.upper() == i:
c = alphabet[(alphabet.index(i.lower()) - shift) % len(alphabet)]
news = news + c.upper()
else:
c = alphabet[(alphabet.index(i) - shift) % len(alphabet)]
news = news + c
else:
news = news + i
print(news)
def gettodo():
a = input("What do you want to do? E for encrypt, D for decrypt: ").lower()
if a[0] == "e":
b = input("Enter a plaintext: ")
c = int(input("Enter a shift: "))
encrypt(b, c)
gettodo()
elif a[0] == "d":
b = input("Enter a ciphertext: ")
c = int(input("Enter a shift: "))
decrypt(b, c)
gettodo()
else:
print("Invalid input.")
gettodo()
gettodo()
No comments:
Post a Comment