On RSA, encyption as well as decryption require
Modular Exponentiation, i.e.
determine xc mod n.
This can be done in c-1 modulo multiplications
but is very inefficient when c is large.
The "square-and-multiply" algalgorithmus reduces the amount
of modulo multiplications needed to at most 2l, where l is
the number of bits in the binary representation of c.
Since l <= k, it is possible to find xc mod n in O(k3).
Thus RSA encryption and decryption can be performed in polynomial time.
If we have b as bl-1...b1b0 (i.e. least valuable bit on the right), the square-and-multiply algorithm for determining z = xb
mod n is: