Skip to content

Instantly share code, notes, and snippets.

@mouadkhiat
Last active February 20, 2024 13:26
Show Gist options
  • Save mouadkhiat/2d81326037791fb23d23dcd04a2382c3 to your computer and use it in GitHub Desktop.
Save mouadkhiat/2d81326037791fb23d23dcd04a2382c3 to your computer and use it in GitHub Desktop.
Caesar Cipher Python [Encrypt / Decrypt]
############ Made By Mouad KHIAT / Python 3.7
############ if you have another idea to write this famous cipher put it in the comment
############ It's started with DOHD MDFWD HVW ==> ALEA JACTA EST (key = 3)
def transform(x):
x = list(x)
for i,n in enumerate(x):
if n in ABC:
x[i] = ABC.index(n)
return x
def crypt(x,key):
x = transform(x)
x = [(n + key)%26 if type(n) == int else n for i,n in enumerate(x)]
return x
def decrypt(x,key):
x = transform(x)
d = []
if key == None:
for k in range(26):
d.append(crypt(x,-k))
x = d
return x
else:
return crypt(x,-key)
def caesar(x,key,mod):
v = []
if mod:
x = crypt(x,key)
else:
x = decrypt(x,key)
for i,n in enumerate(x):
if type(n) == int:
x[i] = ABC[n]
elif type(n) == list:
for j,m in enumerate(n):
if type(m) == int:
n[j] = ABC[m]
value = ''.join(n)
#v.append('==> key = {} : {}'.format(i,value)) #for old version
v.append(f'==> key = {i} : {value}')
if len(v) != 0:
return '\n'.join(v)
#return "==> {}.format(''.join(x))" #for old version
return f"==> {''.join(x)}"
ABC = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
if input('==> Encrypt or Decrypt ? : ').lower() == 'encrypt':
x = input('Enter what you want to encrypt => ').upper()
key = int(input('Enter the key [0-25] => '))
mod = True
else :
x = input('Enter what you want to decrypt => ').upper()
if input('Do you have the key ? (y/n) :').lower() == 'y':
key = int(input('Enter the key => '))
else: key=None
mod = False
print(caesar(x,key,mod))
@GLITCHRS
Copy link

ascii_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-."

def encrypt(text: str, key: int) -> str:
return "".join(ascii_letters[(ascii_letters.index(plain_letter) + key) % len(ascii_letters)] for plain_letter in text)

def decrypt(encrypted_text: str, key: int) -> str:
return "".join(ascii_letters[(ascii_letters.index(plain_letter) - key) % len(ascii_letters)] for plain_letter in encrypted_text)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment