-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathVignereCipher.py
More file actions
20 lines (17 loc) · 781 Bytes
/
Copy pathVignereCipher.py
File metadata and controls
20 lines (17 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class VignereCipher:
def __init__(self, key: str):
self.key = key
def encrypt(self, plaintext: str) -> str:
ciphertext = ''
for index, letter in enumerate(plaintext.lower()):
ciphertext += chr((self.char_2_num(letter) + self.char_2_num(self.key[index % len(self.key)])) % 26 + ord('a'))
return ciphertext.upper()
def decrypt(self, ciphertext: str) -> str:
plaintext = ''
for index, letter in enumerate(ciphertext.lower()):
plaintext += chr(
(self.char_2_num(letter) - self.char_2_num(self.key[index % len(self.key)])) % 26 + ord('a'))
return plaintext.lower()
@staticmethod
def char_2_num(character: str) -> int:
return ord(character.lower()) - ord('a')