Math.Pow 在 C# 中不是 return 正确的值
Math.Pow in C# not return correct value
我想使用 C# 来演示 RSA 加密。我选择 p=17 和 q=11。所以我得到 N=187 和 e=7 和 d=23.
当我选择字符 X(ASCII 中十进制的 88)并对其进行加密时:
double enCrypt = Math.Pow(88, 7) % 187;
它return值enCrypt =11
那么,我要解密,所以我计算:
double deCrypt = Math.Pow(11, 23) % 187;
但不是return原来的值88而是deCrypt=149
!
我该如何解决这个问题?
您需要自己使用 BigInteger
if you want to do the mod and pow steps separately. (If you want to use a fixed-size integer type, you need to implement modpow,因为它内置于 .NET 中的唯一位置是 BigInteger
。)double
只能明确地存储高达 2^53− 的整数1,而11^23更大。
我想使用 C# 来演示 RSA 加密。我选择 p=17 和 q=11。所以我得到 N=187 和 e=7 和 d=23.
当我选择字符 X(ASCII 中十进制的 88)并对其进行加密时:
double enCrypt = Math.Pow(88, 7) % 187;
它return值enCrypt =11
那么,我要解密,所以我计算:
double deCrypt = Math.Pow(11, 23) % 187;
但不是return原来的值88而是deCrypt=149
!
我该如何解决这个问题?
您需要自己使用 BigInteger
if you want to do the mod and pow steps separately. (If you want to use a fixed-size integer type, you need to implement modpow,因为它内置于 .NET 中的唯一位置是 BigInteger
。)double
只能明确地存储高达 2^53− 的整数1,而11^23更大。