【发布时间】: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