Calculating pow(a,b) mod n

后端 未结 14 992
执念已碎
执念已碎 2020-11-22 16:25

I want to calculate ab mod n for use in RSA decryption. My code (below) returns incorrect answers. What is wrong with it?

unsigned long i         


        
相关标签:
14条回答
  • 2020-11-22 17:23

    For my code a^k mod n in php:

    function pmod(a, k, n)
    {
        if (n==1) return 0;
        power = 1;
        for(i=1; i<=k; $i++)
        {
            power = (power*a) % n;
        }
        return power;
    }
    
    0 讨论(0)
  • 2020-11-22 17:25
    #include <cmath>
    ...
    static_cast<int>(std::pow(a,b))%n
    

    but my best bet is you are overflowing int (IE: the number is two large for the int) on the power I had the same problem creating the exact same function.

    0 讨论(0)
提交回复
热议问题