So, why we are getting the wrong answer, if you use the expression "62**65 % 133" that matches the formula in encryption algorithm directly? It could be integer overflow on the intermediate result. I am not sure. You can try this in PHP script too: <?php # PowerModTest.php # ...