I have this function that I have significantly simplified that calculates the multiplicative and additive keys for decrypting an affine cipher and it works in some cases but there is this one case where it is throwing me an error and I am unsure why. Here is my code:
def InverseMod(a, m):
for i in range(1,m):
if ( m*i + 1) % a == 0:
return ( m*i + 1) // a
return 'These are not co-prime.'
def decryption_keys_affine(p1, p2, C1, C2, AL):
s = InverseMod(p1 - p2, AL) * (C1 - C2)
r = (InverseMod(s, AL) * C2 - p2) % AL
print("INV(S):", InverseMod(s, AL), "\n" + "R:", r)
When I give it this input, it outputs the correct answers which are 17 and 26:
>>> decryption_keys_affine(3, 20, 19, 20, 42)
INV(S): 17
R: 26
When I give it this input though, it throws me this error:
>>> decryption_keys_affine(5, 20, 9, 26, 26)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
decryption_keys_affine(5, 20, 9, 26, 26)
File "C:\Users\Herman\Desktop\crypto_math_functions.py", line 96, in decryption_keys_affine
r = (InverseMod(s, AL) * C2 - p2) % AL
TypeError: unsupported operand type(s) for -: 'str' and 'int'
It should output:
>>> decryption_keys_affine(5, 20, 9, 26, 26)
INV(S): 7
R: 20
r = (InverseMod(s, AL) * C2 - p2) % AL
- is causing the error. I imaginep2
has become astr
at that point and the result of(InverseMod(s, AL) * C2
is an int - or vis versa. Print stuff just befor that statement to see what they are or Catch the error and inspect/print relevant data in the except suite. Maybe think about learning the pdb module.str
andint
:r = ('These are not co-prime.' - p2) % AL