Last active
April 5, 2023 16:22
-
-
Save shravanasati/5723a777ec9df925b17dafc4cd42d2ab to your computer and use it in GitHub Desktop.
Caeser cipher and Vigenere cipher in python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
class CaesarCipher: | |
def __init__(self, shift: int = 11) -> None: | |
self.shift = shift | |
self.keys_upper = string.ascii_uppercase | |
self.keys_lower = string.ascii_lowercase | |
def __perform_shift(self, text: str, positive: bool) -> str: | |
if positive: | |
shift = self.shift | |
else: | |
shift = -self.shift | |
shifted_text = "" | |
for char in text: | |
if char.isupper(): | |
to_consider = self.keys_upper | |
elif char.islower(): | |
to_consider = self.keys_lower | |
else: | |
shifted_text += char | |
continue | |
index = to_consider.index(char) | |
index += shift | |
if abs(index) >= 26: | |
index %= 26 | |
shifted_text += to_consider[index] | |
return shifted_text | |
def encrypt(self, text: str) -> str: | |
encrypted_text = self.__perform_shift(text, True) | |
return encrypted_text | |
def decrypt(self, text: str) -> str: | |
decrypted_text = self.__perform_shift(text, False) | |
return decrypted_text | |
class VigenereCipher: | |
def __init__(self, passphrase: str): | |
self.keys_upper = string.ascii_uppercase | |
self.keys_lower = string.ascii_lowercase | |
if self.__verify_passphrase(passphrase): | |
self.passphrase = passphrase | |
else: | |
raise Exception("Invalid passphrase! passphrase can't contain any symbols or numbers.") | |
def __verify_passphrase(self, passphrase: str) -> bool: | |
passed = False | |
for char in passphrase: | |
if char in self.keys_lower or char in self.keys_upper: | |
continue | |
else: | |
break | |
else: # if the for loop finishes without the break statement | |
passed = True | |
return passed | |
@staticmethod | |
def __get_appropriate_passphrase(text:str, passphrase:str) -> str: | |
passphrase_len = len(passphrase) | |
text_len = len(text) | |
quotient = text_len // passphrase_len | |
rem = text_len % passphrase_len | |
passphrase = passphrase * quotient + passphrase[:rem] | |
return passphrase | |
def __perform_shift(self, text:str, positive: bool) -> str: | |
passphrase = self.__get_appropriate_passphrase(text, self.passphrase) | |
shifted_text = "" | |
for i, char in enumerate(text): | |
if char.isupper(): | |
to_consider = self.keys_upper | |
elif char.islower(): | |
to_consider = self.keys_lower | |
else: | |
shifted_text += char | |
continue | |
if positive: | |
shift = to_consider.index(char) + to_consider.index(passphrase[i]) | |
else: | |
shift = to_consider.index(char) - to_consider.index(passphrase[i]) | |
if abs(shift) >= 26: | |
shift %= 26 | |
shifted_text += to_consider[shift] | |
return shifted_text | |
def encrypt(self, text: str) -> str: | |
return self.__perform_shift(text, True) | |
def decrypt(self, text: str) -> str: | |
return self.__perform_shift(text, False) | |
if __name__ == "__main__": | |
# cipher = CaesarCipher() | |
# text = "Caesar Cipher isn't useful at all nowadays. It can be easily broken." | |
# encrypted = cipher.encrypt(text) | |
# print(encrypted) | |
# decrypted = cipher.decrypt(encrypted) | |
# print(decrypted) | |
c = VigenereCipher("mysupersecretkey") | |
b = (c.encrypt("gallium ded haha this so fucking boring amiright")) | |
print(b) | |
print(c.decrypt(b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment