File tree Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ Algorithms from the book Cryptography & Network Security ~Behrouz A. Forouzan. S
22
22
1 . Monoalphabetic Ciphers
23
23
1 . [ Caesar Shift Cipher] ( ciphers/CaesarShiftCipher.py )
24
24
1 . [ Multiplicative Cipher] ( ciphers/MultiplicativeCipher.py )
25
- 1 . Affine Cipher
25
+ 1 . [ Affine Cipher] ( ciphers/AffineCipher.py )
26
26
1 . Polyalphabetic Ciphers
27
27
1 . Autokey Cipher
28
28
1 . [ Playfair Cipher] ( ciphers/PlayfairCipher.py )
Original file line number Diff line number Diff line change
1
+ from mathematics import multiplicative_inverse
2
+
3
+ class AffineCipher :
4
+ def __init__ (self , k_1 , k_2 ):
5
+ self .k_1 = k_1
6
+ self .k_2 = k_2
7
+
8
+ def char_to_num (self , letter ):
9
+ letter = letter .lower ()
10
+ return ord (letter ) - ord ('a' )
11
+
12
+ def encrypt (self , plaintext ):
13
+ return '' .join (
14
+ [chr ((self .char_to_num (letter ) * self .k_1 + self .k_2 ) % 26 + ord ('A' )) for letter in plaintext .lower ()])
15
+
16
+ def decrypt (self , ciphertext ):
17
+ return '' .join (
18
+ [chr (((self .char_to_num (letter ) - self .k_2 ) * multiplicative_inverse (self .k_1 , 26 )) % 26 + ord ('a' ))
19
+ for letter in ciphertext .lower ()]
20
+ )
Original file line number Diff line number Diff line change 3
3
from ciphers .PlayfairCipher import PlayFairCipher
4
4
from ciphers .CaesarShiftCipher import CaesarShiftCipher
5
5
from ciphers .MultiplicativeCipher import MultiplicativeCipher
6
+ from ciphers .AffineCipher import AffineCipher
You can’t perform that action at this time.
0 commit comments