-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0d23cc
commit f42a502
Showing
4 changed files
with
27 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from mathematics import multiplicative_inverse | ||
|
||
|
||
class MultiplicativeCipher: | ||
def __init__(self, key): | ||
self.key = key | ||
|
||
def encrypt(self, plaintext: str) -> str: | ||
return ''.join([self.num_2_char((self.char_2_num(letter) * self.key) % 26) for letter in plaintext.lower()]) | ||
|
||
def decrypt(self, ciphertext: str) -> str: | ||
return ''.join( | ||
[self.num_2_char((self.char_2_num(letter) * multiplicative_inverse(self.key, 26)) % 26) | ||
for letter in ciphertext.lower()] | ||
) | ||
|
||
@staticmethod | ||
def char_2_num(character: str) -> int: | ||
return ord(character.lower()) - ord('a') | ||
|
||
@staticmethod | ||
def num_2_char(number: int) -> str: | ||
return chr(number + ord('a')) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from ciphers import CaesarShiftCipher | ||
from ciphers import MultiplicativeCipher | ||
|
||
caesar_cipher = CaesarShiftCipher(10) | ||
caesar_cipher = MultiplicativeCipher(7) | ||
ciphertext = caesar_cipher.encrypt('helloworld') | ||
print(ciphertext) | ||
print(caesar_cipher.decrypt(ciphertext)) |