首页 > 解决方案 > RSA中素数和消息的大小

问题描述

我正在玩玩具 RSA 源代码。我观察到,如果大小消息改变,则保持素数 p 和 q 的大小固定,然后解密的文本会改变。如果消息的大小增加到超过某些值,那么解密后我将无法获得原始明文。所以请建议对 p ,q 和消息大小的值有什么限制。意味着给定 ap 和 q 我应该选择特定大小的明文吗?示例源代码如下

# Write Python3 code here 
from decimal import Decimal


def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


p = int(input('Enter the value of p = '))
q = int(input('Enter the value of q = '))
no = int(input('Enter the value of text = '))
n = p * q
t = (p - 1) * (q - 1)

for e in range(2, t):
    if gcd(e, t) == 1:
        break

for i in range(1, 10):
    x = 1 + i * t
    if x % e == 0:
        d = int(x / e)
        break
print('pvt key= ', d)

ct = pow(no,e,n)

dt = pow(ct,d,n)

print('n = ' + str(n) + ' e = ' + str(e) + ' t = ' + str(t) + ' d = ' + str(d) + ' cipher text = ' + str(ct) + ' decrypted text = ' + str(dt))

标签: rsa

解决方案


推荐阅读