【问题标题】:negative exponent in modular exponentiation for RSARSA模幂中的负指数
【发布时间】:2017-06-13 11:55:29
【问题描述】:

出于教育目的,我正在尝试在 python3.6 中编写 RSA 代码。

密钥生成和消息加密工作正常,但解密有问题。据我了解,解密算法是 M = Cd mod n,其中 M 是消息,C 是加密消息(使用接收者的公钥),d 是接收者的私钥.问题是当 d 为负数时,根据我的经验,这种情况很常见。我正在使用从右到左的算法进行模幂运算,但我不知道如何使它与负指数一起工作。这是我的 m 代码。即:

def mod_pow(b, e, m):
if m == 1:
    return 0
res = 1
b = b % m
while e > 0:
    if e % 2 == 1:
        res = (res * b) % m
    e = e >> 1
    b = (b * b) % m
return res

【问题讨论】:

  • d 不应为负数。如果是,那你就做错了。
  • 参见Computing modular inverses:“如果 t
  • 谢谢!现在终于成功了!

标签: python-3.x encryption rsa exponentiation modular-arithmetic


【解决方案1】:

James K Polk 回答了这个问题,我只需将其添加到扩展的欧几里得算法代码中:

if t < 0:
    t += n

【讨论】:

    猜你喜欢
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多