Sign in Sign up
Instantly share code, notes, and snippets.
mouadkhiat / caesar.py
Last active 3 years ago
Star
Code Revisions 2 Stars 1
Caesar Cipher Python [Encrypt / Decrypt]
caesar.py Raw
1 ############ Made By Mouad KHIAT / Python 3.7
2 ############ if you have another idea to write
3 ############ It's started with DOHD MDFWD HVW =
4 def transform(x):
5 x = list(x)
6 for i,n in enumerate(x):
7 if n in ABC:
8 x[i] = ABC.index(n)
9 return x
10 def crypt(x,key):
11 x = transform(x)
12 x = [(n + key)%26 if type(n) == int else n
13 return x
14
15 def decrypt(x,key):
16 x = transform(x)
17 d = []
18 if key == None:
19 for k in range(26):
20 d.append(crypt(x,-k))
21 x = d
22 return x
23 else:
24 return crypt(x,-key)
25
26 def caesar(x,key,mod):
27 v = []
28 if mod:
29 x = crypt(x,key)
30 else:
31 x = decrypt(x,key)
32 for i,n in enumerate(x):
33 if type(n) == int:
34 x[i] = ABC[n]
35 elif type(n) == list:
36 for j,m in enumerate(n):
37 if type(m) == int:
38 n[j] = ABC[m]
39 value = ''.join(n)
40 #v.append('==> key = {} : {}'.forma
41 v.append(f'==> key = {i} : {value}'
42 if len(v) != 0:
43 return '\n'.join(v)
44 #return "==> {}.format(''.join(x))" #for ol
45 return f"==> {''.join(x)}"
46
47 ABC = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
48 if input('==> Encrypt or Decrypt ? : ').lower()
49 x = input('Enter what you want to encrypt =
50 key = int(input('Enter the key [0-25] => '
51 mod = True
52 else :
53 x = input('Enter what you want to decrypt =
54 if input('Do you have the key ? (y/n) :').l
55 key = int(input('Enter the key => '))
56 else: key=None
57 mod = False
58 print(caesar(x,key,mod))
Sign up for free to join this conversation on
GitHub. Already have an account? Sign in to
comment
Terms Privacy Security Status Docs Contact GitHub
Pricing API Training Blog About
© 2022 GitHub, Inc.