-
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
f42a502
commit f1d09a5
Showing
3 changed files
with
22 additions
and
1 deletion.
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,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()] | ||
) |
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