Calculating pow(a,b) mod n

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

    I'm using this function:

    int CalculateMod(int base, int exp ,int mod){
        int result;
        result = (int) pow(base,exp);
        result = result % mod;
        return result;
    }
    

    I parse the variable result because pow give you back a double, and for using mod you need two variables of type int, anyway, in a RSA decryption, you should just use integer numbers.

提交回复
热议问题