Python RSA Decryption is throwing TypeErrors
<p>My RSA decrypt function:</p>

<pre><code>def decrypt(ctext,private_key):
    key,n = private_key
    text = [chr(pow(char,key)%n) for char in ctext]
    return "".join(text)
</code></pre>

<p>is sometimes throwing a <code>TypeError</code> which tells that <code>pow(char,key)%n</code> provides a <code>float</code>. Why is that? I cant explain it myself and it would be interesting to know why.</p>

<p>For example it happens when:</p>

<blockquote>
  <p>crypt-Text = [513300, 369218, 473524, 473524, 500307, 509880, 264366, 500307, 337068, 473524, 264834]<br>
  key = -159317<br>
  n = 540767</p>
</blockquote>

<p>It's hard to figure out much from a tiny fragment of code. You are getting float results because your variable <code>key</code> is negative number. It is clear from the description of <a href="https://docs.python.org/3/library/functions.html#pow" rel="nofollow">pow</a> that you will get float results, which is not what you want. You really should be using the 3-argument form of pow, and your exponent should always be positive. By using standard <a href="https://en.wikipedia.org/wiki/RSA_(cryptosystem)#Key_generation" rel="nofollow">RSA math</a> you can always make your exponent positive by adding an appropriate multiple of &Phi;(n). In your particular case, &Phi;(n) = (631 - 1) * (857 - 1) = 539280, so key = 379963 mod &Phi;(n). </p>

<p>You should correct the code that gives you a negative exponent.</p>

python python-3.x rsa typeerror precision