【发布时间】:2021-04-22 00:17:51
【问题描述】:
当素数值太高时,我的 RSA MPrime 无法解密
我的解密代码如下:
def descripto(msg, d, num, N):
ds = []
ms = []
MSG = 0
for n in range(0, len(num)):
ds.append(d % (num[n]-1))
for n in range(0, len(num)):
ms.append(pow(msg, ds[n]) % num[n])
for n in range(0, len(num)):
MSG = MSG + (N/num[n]) * ms[n] * pow(int(N/num[n]), -1, int(num[n]))
MSG = MSG % N
return int(MSG)
N 是所有 num 个数(质数)的乘积。 msg 是一个随机数。 d 是私钥。
一些测试:
Primes: [1181, 1543, 149]
Phi: 269294880
Public Key: <271520167, 7>
Private Key: <271520167, 230824183>
Message: 132
Crypto message: 30346111
Uncrypto message: 132
Primes: [1181, 1543, 149, 2143]
Phi: 576829632960
Public Key: <581867717881, 11>
Private Key: <581867717881, 314634345251>
Message: 583
Crypto message: 424678919465
Uncrypto message: 583
Primes: [27361, 27449, 27481, 149]
Phi: 3054254636851200
Public Key: <3075227812833541, 7>
Private Key: <3075227812833541, 436322090978743>
Message: 108
Crypto message: 171382426877952
Uncrypto message: 44
Primes: [27361, 27449, 149]
Phi: 111144637440
Public Key: <111903781261, 7>
Private Key: <111903781261, 79389026743>
Message: 113
Crypto message: 38799834195
Uncrypto message: 113
Primes: [27361, 27449, 27481]
Phi: 20636855654400
Public Key: <20639112837809, 7>
Private Key: <20639112837809, 2948122236343>
Message: 19477
Crypto message: 4955135338363
Uncrypto message: 19574
Primes: [1181, 1543, 149, 2143, 7]
Phi: 3460977797760
Public Key: <4073074025167, 11>
Private Key: <4073074025167, 314634345251>
Message: 5
Crypto message: 48828125
Uncrypto message: 5
Primes: [1181, 27449, 149, 2143, 7]
Phi: 61606302589440
Public Key: <72457426388081, 11>
Private Key: <72457426388081, 44804583701411>
Message: 5
Crypto message: 48828125
Uncrypto message: 5
加密算法没问题,解密好像也没问题。我知道我在测试中使用了一小部分数字,但问题不在于这里。当它使用超过 3 个超过 10000 的素数时似乎有问题,我猜这是因为计算机无法处理如此高的数字。此外,当解密失败时,您可以看到“苹果离树不远”。谁能确认一下?
【问题讨论】:
-
这是 python 2 还是 python 3?如果是 python 3,您需要使用
//进行整数除法,而不是/。应该没有理由在其中的任何地方使用int()。 -
@PresidentJamesK.Polk 它是 P3 但仍使用此更改(经过测试)将返回相同的结果
标签: python cryptography rsa