【问题标题】:RSA MPrime broken when the values are too high in Python当 Python 中的值太高时 RSA MPrime 损坏
【发布时间】: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


【解决方案1】:

是的,这些数字相乘可能会超过可能是 64 位的 int 的最大值。然而在 Python 3 中,int 的大小是无限的,因此它取决于机器。请参阅此post 了解更多信息。

【讨论】:

    【解决方案2】:

    您使用的似乎是 Python 3.8 或更高版本(否则在调用 pow() 时会出现负指数错误。

    在 Python 版本 3 中,/ 运算符表示浮点除法,这将为您提供双精度浮点结果。你不想这样。

    尝试用N//num[n] 替换N/num[n](使用整数除法运算符//)。并摆脱 int 演员表。它们是不必要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 2012-11-07
      • 2017-10-09
      • 1970-01-01
      相关资源
      最近更新 更多