-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciphers.py
More file actions
37 lines (35 loc) · 1.11 KB
/
Copy pathciphers.py
File metadata and controls
37 lines (35 loc) · 1.11 KB
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
class cipher:
def caesar(self,string,shift):
print('only use lowercase')
newStr = ''
for x in string:
charCode = ord(x)
if not x.isalpha():
newChar = x
elif charCode + shift > 122:
over = charCode + shift - 122
newChar = chr(97 + (over - 1))
else:
newChar = chr(charCode + shift)
newStr += newChar
return newStr
def vigenere(self,string,key):
print('only use lowercase')
newStr = ''
k = 0
for x in range(0,len(string)):
currKey = k % len(key)
charCode = ord(string[x])
if not string[x].isalpha():
newChar = string[x]
k -= 1
elif charCode + currKey > 122:
newChar = chr(charCode + currKey - 25)
else:
newChar = chr(charCode + currKey)
newStr += newChar
k += 1
return newStr
myCipher = cipher()
print(myCipher.caesar('hello world', 1))
print(myCipher.vigenere('hello world', 'abc'))