Calculating pow(a,b) mod n

后端 未结 14 1144
执念已碎
执念已碎 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:05

    Usually it's something like this:

    while (b)
    {
        if (b % 2) { res = (res * a) % n; }
    
        a = (a * a) % n;
        b /= 2;
    }
    
    return res;
    

提交回复
热议问题