From: Neil Smith Date: Tue, 27 Mar 2018 09:30:23 +0000 (+0100) Subject: Updated for posting affine ciphers on blog X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=eb9d341079d637d3af35c03b6fc02bfd0768bd43;p=cipher-tools.git Updated for posting affine ciphers on blog --- diff --git a/blog-images/jon-davey-334700-unsplash.jpg b/blog-images/jon-davey-334700-unsplash.jpg new file mode 100644 index 0000000..fd4fbae Binary files /dev/null and b/blog-images/jon-davey-334700-unsplash.jpg differ diff --git a/cipher/affine.py b/cipher/affine.py index 8a9e584..9c521b9 100644 --- a/cipher/affine.py +++ b/cipher/affine.py @@ -3,13 +3,19 @@ from support.language_models import * from logger import logger -modular_division_table = [[0]*26 for _ in range(26)] -for a in range(26): - for b in range(26): - c = (a * b) % 26 - modular_division_table[b][c] = a +# modular_division_table = [[0]*26 for _ in range(26)] +# for a in range(26): +# for b in range(26): +# c = (a * b) % 26 +# modular_division_table[b][c] = a +modular_division_table = { + (multiplier, (multiplier * plaintext) % 26): plaintext + for plaintext in range(26) + for multiplier in range(26) + } + def affine_encipher_letter(accented_letter, multiplier=1, adder=0, one_based=True): @@ -75,9 +81,12 @@ def affine_decipher_letter(letter, multiplier=1, adder=0, one_based=True): if letter in string.ascii_letters: cipher_number = pos(letter) if one_based: cipher_number += 1 + # plaintext_number = ( + # modular_division_table[multiplier] + # [(cipher_number - adder) % 26]) plaintext_number = ( - modular_division_table[multiplier] - [(cipher_number - adder) % 26]) + modular_division_table[multiplier, (cipher_number - adder) % 26] + ) if one_based: plaintext_number -= 1 if letter in string.ascii_uppercase: return unpos(plaintext_number).upper() diff --git a/cipher/hill.py b/cipher/hill.py index 24bac7f..8233de7 100644 --- a/cipher/hill.py +++ b/cipher/hill.py @@ -42,7 +42,7 @@ def hill_decipher(matrix, message, fillvalue='a'): 'hellothereaa' """ adjoint = linalg.det(matrix)*linalg.inv(matrix) - inverse_determinant = modular_division_table[int(round(linalg.det(matrix))) % 26][1] + inverse_determinant = modular_division_table[int(round(linalg.det(matrix))) % 26, 1] inverse_matrix = (inverse_determinant * adjoint) % 26 return hill_encipher(inverse_matrix, message, fillvalue)