【发布时间】:2010-07-27 14:41:10
【问题描述】:
如何在RSA加密的上下文中计算一个数的模乘逆?
【问题讨论】:
标签: security math cryptography rsa
如何在RSA加密的上下文中计算一个数的模乘逆?
【问题讨论】:
标签: security math cryptography rsa
使用Extended Euclidean Algorithm,这在实践中比直接模幂运算要快得多。
【讨论】:
【讨论】:
e (mod φ(n))的逆,使用这种方法需要计算φ(φ(n)),相当于分解φ(n)。所以,@abc 是对的:你不能使用这个方法;之前我什至没有想到这一点。
Modular multiplicative inverse Wikipedia 文章中详细解释了两种算法。
【讨论】:
如果你需要为 DSA 算法计算 w,你可以使用这个:
w = s^-1 mod q
其实是
w = s^(q-2) mod q
见:http://en.wikipedia.org/wiki/Modular_multiplicative_inverse#Using_Euler.27s_theorem
【讨论】:
我算出了一个更简单的反函数
def privateExponent(p,q,e):
totient=(p-1)*(q-1)
for k in range(1,e):
if (totient*k+1) % e==0:
return (totient*k+1)/e
return -1 # shouldnt get here
方程 d*e=1 (mod totient) 可以重写为 d*e=1+k*totient (对于 k 的某个值),程序只搜索 k 的第一个值,这使得方程可以被e(公共指数)。如果 e 很小(通常推荐),这将起作用。
我们可以将所有 bignum 操作移出循环以提高其性能。
def privateExponent(p,q,e):
totient=(p-1)*(q-1)
t_mod_e=totient % e
k=0
total=1
while total!=0:
k+=1
total=(total+t_mod_e) % e
return (k*totient+1)/e
事实证明,对于 e=3,我们真的不需要搜索,因为答案总是 2*((p-1)*(q-1)+1)/3
【讨论】: