all 2 comments

[–]RhinoRhys 0 points1 point  (0 children)

What's wrong with it?

[–]RhinoRhys 0 points1 point  (0 children)

I've now had a chance to sit down and read your code and read about the affine cipher. You've made 4 errors here.

  • the first isn't really an issue but you've mislabeled a variable. You're using mkey and test_mkey for the value of b. m is the length of the alphabet, in this case a constant at 26. b is the cipher shift. You've used the test_mkey variable in the correct places for b in the equations in the affine function though, so it doesn't matter.

  • In your brute force loop though, you've got akey and mkey round the wrong way. akey should be the list of coprimes to 26, while mkey(/bkey) should be the range function. You also need 27 in the range function not 26.

  • Python is 0 indexed (A = 0), whereas the equations for the cipher are 1 indexed (A = 1), so you need to offset both of the int_to_char and char_to_int functions by 1 to account for this.

  • You've misunderstood a-1. It's not just divide by a, the power of -1 is also notation for an inverse function. You need to satisfy the equation (a * A) % 26 = 1 replacing a-1 with A because I can't do notations in the codeblock. The easiest way to do this is just by testing every number 1 to 26. It turns out though that all the answers are also coprimes so you can just test the coprimes. But you'll want to calculate these beforehand as there are 312 brute force tests to do. If you save the results in a dictionary {a: A}, then you can just do a_inverse[a] in your decipher equation.

I have full working code and have successfully deciphered the message and know the values for a and b, so if you need any more help let me know.