Cryptography Help editing a Python function for cryptanalysis? |
Help editing a Python function for cryptanalysis? Posted: 02 Mar 2019 10:11 PM PST I am currently taking a cryptological mathematics course. Most of the work we do is by hand but we're allowed to create programs/functions to assist us if we want. I am trying to create a function for an affine cipher that takes the input from a frequency analysis of the english language (p1, p2) and a frequency analysis of the cipher text (C1, C2) and the alphabet lenght (AL) and it calculates the multiplicative and additive keys needed to decipher the cipher text. Here are two pictures from the textbook explaining the math behind calculating the multiplicative and additive keys from a cipher text but my implementation of this math in Python isn't working. Here is my code: def ModInverse(a, m): if gcd(a, m) != 1: raise ValueError('The input arguments, {} and {}, are not coprime.'.format(a, m)) # Calculate using the Extended Euclidean Algorithm: u1, u2, u3 = 1, 0, a v1, v2, v3 = 0, 1, m while v3 != 0: q = u3 // v3 v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3 return u1 % m def decryption_keys_affine(p1, p2, C1, C2, AL): s = p2 - p1 p3 = s * p1 p4 = (p3 % AL) p5 = C1 - p4 p6 = AL + p5 p7 = ModInverse(s, AL) p8 = p7 * p6 r = p8 % AL multi = ModInverse(p7, AL) add = AL - r print(multi, add) I do not understand why this doesn't work. If I am to input decryption_keys_affine(3, 20, 19, 20, 42) then the output should be 17, 16 but it's 17, 34 instead. I am also trying to apply it to this practice cipher text problem: X0N30NL8Q98G4N1T0G8I4M83QNR8WR8D .8WR8IQNLXWRYLQR,8WR1W4R498X08I4M84PPQWRL0182XW0Z8QZ8LX08R0I82NGPLQ4R4TGLW28KRWL,8SW 9 I know the multiplicative key should be 41 and the additive should be 30 but my function never outputs that no matter what combinations of letters I use from the frequency analysis of english and the cipher text. [link] [comments] |
Can it possible to digitally sign a DLL file using PFX file? Posted: 02 Mar 2019 01:11 PM PST |
You are subscribed to email updates from Cryptography news and discussions. To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google, 1600 Amphitheatre Parkway, Mountain View, CA 94043, United States |
No comments:
Post a Comment