Skip to content

Commit

Permalink
adds affine cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
anishLearnsToCode committed Aug 24, 2020
1 parent f42a502 commit f1d09a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Algorithms from the book Cryptography & Network Security ~Behrouz A. Forouzan. S
1. Monoalphabetic Ciphers
1. [Caesar Shift Cipher](ciphers/CaesarShiftCipher.py)
1. [Multiplicative Cipher](ciphers/MultiplicativeCipher.py)
1. Affine Cipher
1. [Affine Cipher](ciphers/AffineCipher.py)
1. Polyalphabetic Ciphers
1. Autokey Cipher
1. [Playfair Cipher](ciphers/PlayfairCipher.py)
Expand Down
20 changes: 20 additions & 0 deletions ciphers/AffineCipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from mathematics import multiplicative_inverse

class AffineCipher:
def __init__(self, k_1, k_2):
self.k_1 = k_1
self.k_2 = k_2

def char_to_num(self, letter):
letter = letter.lower()
return ord(letter) - ord('a')

def encrypt(self, plaintext):
return ''.join(
[chr((self.char_to_num(letter) * self.k_1 + self.k_2) % 26 + ord('A')) for letter in plaintext.lower()])

def decrypt(self, ciphertext):
return ''.join(
[chr(((self.char_to_num(letter) - self.k_2) * multiplicative_inverse(self.k_1, 26)) % 26 + ord('a'))
for letter in ciphertext.lower()]
)
1 change: 1 addition & 0 deletions ciphers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from ciphers.PlayfairCipher import PlayFairCipher
from ciphers.CaesarShiftCipher import CaesarShiftCipher
from ciphers.MultiplicativeCipher import MultiplicativeCipher
from ciphers.AffineCipher import AffineCipher

0 comments on commit f1d09a5

Please sign in to comment.