【问题标题】:How to calculate the Modular Multiplicative inverse of a number in the context of RSA encryption?如何在 RSA 加密的上下文中计算数字的模乘逆?
【发布时间】:2010-07-27 14:41:10
【问题描述】:

如何在RSA加密的上下文中计算一个数的模乘逆?

【问题讨论】:

    标签: security math cryptography rsa


    【解决方案1】:

    使用Extended Euclidean Algorithm,这在实践中比直接模幂运算要快得多。

    【讨论】:

      【解决方案2】:

      直接模幂运算

      直接模幂方法,作为扩展欧几里得算法的替代方法,如下:

      来源:http://en.wikipedia.org/wiki/Modular_multiplicative_inverse

      【讨论】:

      • 这个方法需要你取反的组的顺序。对于 RSA,您通常不知道这一点。
      • 如果模数是素数,你就知道了——它是 P-1。对于密钥生成时的 RSA 密钥,您可能也知道 (P-1)*(Q-1)。一旦创建了密钥对,模数的因式分解就被丢弃了,因为知道组的顺序是创建密钥对所必需的,并且相当于找到私钥。
      • 由于在RSA中,要找到私钥需要求e (mod φ(n))的逆,使用这种方法需要计算φ(φ(n)),相当于分解φ(n)。所以,@abc 是对的:你不能使用这个方法;之前我什至没有想到这一点。
      • 如果 RSA 素数是安全素数,则很容易计算 phi(phi(n))。请参阅我对stackoverflow.com/questions/3209665/… 的评论。但是想想和修改它很有趣,扩展的欧几里得算法总是运行得很快。
      【解决方案3】:

      Modular multiplicative inverse Wikipedia 文章中详细解释了两种算法。

      【讨论】:

        【解决方案4】:

        如果你需要为 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

        【讨论】:

          【解决方案5】:

          我算出了一个更简单的反函数

          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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-01-12
            • 1970-01-01
            • 2014-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多