Skip to content

Commit

Permalink
solves multiplicative cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
anishLearnsToCode committed Aug 24, 2020
1 parent a0d23cc commit f42a502
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Algorithms from the book Cryptography & Network Security ~Behrouz A. Forouzan. S
## 🔑 [Traditional Symmetric Key Ciphers](notebooks/3-symmetric-key-ciphers.ipynb)
1. Monoalphabetic Ciphers
1. [Caesar Shift Cipher](ciphers/CaesarShiftCipher.py)
1. Multiplicative Cipher
1. [Multiplicative Cipher](ciphers/MultiplicativeCipher.py)
1. Affine Cipher
1. Polyalphabetic Ciphers
1. Autokey Cipher
Expand Down
23 changes: 23 additions & 0 deletions ciphers/MultiplicativeCipher.py
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'))
1 change: 1 addition & 0 deletions ciphers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from ciphers.VignereCipher import VignereCipher
from ciphers.PlayfairCipher import PlayFairCipher
from ciphers.CaesarShiftCipher import CaesarShiftCipher
from ciphers.MultiplicativeCipher import MultiplicativeCipher
4 changes: 2 additions & 2 deletions test.py
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))

0 comments on commit f42a502

Please sign in to comment.