-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceasar.py
62 lines (60 loc) · 1.92 KB
/
ceasar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def encrypt_caesar(plaintext):
"""
Encrypts plaintext using a Caesar cipher.
>>> encrypt_caesar("PYTHON")
'SBWKRQ'
>>> encrypt_caesar("python")
'sbwkrq'
>>> encrypt_caesar("Python3.6")
'Sbwkrq3.6'
>>> encrypt_caesar("")
''
"""
ciphertext = ""
for i in range(0, len(plaintext)):
if ('a' <= plaintext[i] <= 'z') or ('A' <= plaintext[i] <= 'Z'):
c = plaintext[i].islower()
if c == True:
q = ord(plaintext[i])+3
if q > ord('z'):
while q > ord('z'):
q = (q-ord('z'))+(ord('a')-1)
else:
q = ord(plaintext[i])+3
if q > ord('Z'):
while q > ord('Z'):
q = (q-ord('Z'))+(ord('A')-1)
ciphertext = ciphertext+chr(q)
else:
ciphertext = ciphertext+plaintext[i]
return ciphertext
def decrypt_caesar(ciphertext):
"""
Decrypts a ciphertext using a Caesar cipher.
>>> decrypt_caesar("SBWKRQ")
'PYTHON'
>>> decrypt_caesar("sbwkrq")
'python'
>>> decrypt_caesar("Sbwkrq3.6")
'Python3.6'
>>> decrypt_caesar("")
''
"""
plaintext = ""
for i in range(0, len(ciphertext)):
if ('a' <= plaintext[i] <= 'z') or ('A' <= plaintext[i] <= 'Z'):
c = ciphertext[i].islower()
if c == True:
q = ord(ciphertext[i])-3
if q < ord("a"):
while q < ord("a"):
q = (ord('z')+1)-(ord('a')-q)
else:
q = ord(ciphertext[i])-3
if q < ord("A"):
while q < ord("A"):
q = (ord('Z')+1)-(ord('A')-q)
plaintext = plaintext+chr(q)
else:
plaintext = plaintext+ciphertext[i]
return plaintext