如何在 C# 中执行模乘和求幂?

How can modular multiplication and exponentiation be performed in C#?

如果我知道参数 akp 那么我如何在 C# 中计算它?

s=a*k^-1 mod p

它用于加密目的,我是新手。如果问题不合适,请不要感到被冒犯。

请注意 k^-1k (mod p) 的模逆而不是幂运算符。

由于问题是关于模逆的,我想求知者再看看SO question

答案的关键是-

Net 4.0+ implements BigInteger with a special modular arithmetics function ModPow (which produces “X power Y modulo Z”), you don't need a third-party library to emulate ModInverse. If m is a prime, all you need to do is to compute:

在 C# 中,根据 MSDN documentation 这被定义为

public static BigInteger ModPow(
    BigInteger value,
    BigInteger exponent,
    BigInteger modulus
)

使用这个我们可以做一些事情,比如计算 k 模 p 的倒数 喜欢

BigInteger bi= ModPow(k, -1, p );
int b= (int) bi;
s= (a* bi )%p;

public static double DoMath(double a, double k, double p)

    {
        return (a * Math.Pow(k, -1)) % p;
    }