简体   繁体   中英

Math.Pow in C# not return correct value

I want to use C# to demonstrate RSA cryptography. I pick p=17 and q=11. So I get N=187 and e=7 and d=23.

When I pick character X (88 in decimal in ASCII) and encrypt it:

double enCrypt = Math.Pow(88, 7) % 187;

and it return value enCrypt =11

Then, I want to decrypt it, so I calculate:

double deCrypt = Math.Pow(11, 23) % 187;

but it's not return to the original value 88 but deCrypt=149 !

How can I troubleshoot this problem?

You need to use a 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 yourself, since the only place it's built into .NET is BigInteger .) double can only store integers unambiguously up to 2^53−1, and 11^23 is larger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM