我有 p
、q
、n
和 e
的值,我想计算私有(private)键 d
。我该怎么做,有人可以给我示例 C# 代码吗?我正在使用 BigInteger
类来表示 p
、q
、n
和 e< 的值
所以我假设 d 也是一个 BigInteger
。
最佳答案
来自 Wikipedia :
Determine d (using modular arithmetic) which satisfies the congruence relation
- Stated differently, ed − 1 can be evenly divided by the totient (p − 1)(q − 1).
- This is often computed using the extended Euclidean algorithm.
- d is kept as the private key exponent.
扩展欧几里德算法允许您找到满足以下条件的整数:
The extended Euclidean algorithm is particularly useful when a and b are coprime, since x is the modular multiplicative inverse of a modulo b.
在此公式中,将 a
设置为 e
,将 b
设置为 (p-1)(q-1)
和 gcd(a, b)
到 1(因为在 RSA 算法中要求 e 和 φ(pq) 互质)并求解 x
这给了你d
。
extended Euclidean algorithm 上的维基百科页面有关如何编写求解 x 和 y 的算法的更多详细信息。例如,您可以使用这个递归函数(在伪代码中):
function extended_gcd(a, b)
if a mod b = 0
return {0, 1}
else
{x, y} := extended_gcd(b, a mod b)
return {y, x-(y*(a div b))}
在 .NET 中,如果您只想生成一些 RSA key ,则不必自己实现 RSA 算法。您可以使用 .NET 框架中的 RSA 实现。
关于c# - C#生成RSA私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3740874/