TypeError: unsupported operand parent(s) for '+': 'Ring of integers modulo x' and 'Ring of integers modulo y'

TypeError: unsupported operand parent(s) for '+': 'Ring of integers modulo x' and 'Ring of integers modulo y'

我正在尝试使用中国剩余定理解密 RSA 消息:

p = random_prime(2^512-1, False, 2^511)
q = random_prime(2^512-1, False, 2^511)
n = p * q
phi = (p - 1) * (q - 1)
m = 235432543543345
e = 65537
bezout = xgcd(e, phi)
d = Integer(mod(bezout[1], phi))
c = 0
m_decrypted = 0

def encrypt_rsa():
    global c
    c = mod(m ^ e, n)
    print 'c = ', c

def decrypt_rsa_crt():
    global m_decrypted
    c1 = p * inverse_mod(p % q, q)
    c2 = q * inverse_mod(q % p, p)
    n1 = ((c % p) ^ (d % (p - 1))) % p
    n2 = ((c % q) ^ (d % (q - 1))) % q
    m_decrypted = (n1 * c1 + n2 * c2) % n

encrypt_rsa()
decrypt_rsa_crt()
print 'm_decrypted = ', m_decrypted

但是我收到了这个错误:

Error in lines 40-40
Traceback (most recent call last):
  File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
    exec compile(block+'\n', '', 'single') in namespace, locals
  File "", line 1, in <module>
  File "", line 15, in decrypt_rsa_crt
  File "sage/structure/element.pyx", line 1700, in sage.structure.element.RingElement.__add__ (/projects/sage/sage-6.9/src/build/cythonized/sage/structure/element.c:16570)
    return coercion_model.bin_op(left, right, add)
  File "sage/structure/coerce.pyx", line 1070, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (/projects/sage/sage-6.9/src/build/cythonized/sage/structure/coerce.c:9739)
    raise TypeError(arith_error_message(x,y,op))
TypeError: unsupported operand parent(s) for '+': 'Ring of integers modulo 13171665913556629928579831399279531345454405227507363358767433731944129138880990155192020899186190552971738190528920869626603617749599336864307309151600773' and 'Ring of integers modulo 7040259687366349269925712273121165166901616380486072254231627613746850969231618371638459623994615760442997840722561781585598181676625437161073288718538017'

错误行是m_decrypted = (n1 * c1 + n2 * c2) % n行。我不确定我在这里做错了什么以及如何解决它?

inverse_mod 在您的代码中将事物放入整数环 modulo qp 中。但是没有添加 c1c2 的规范方法,因为它们位于不同的整数环中,如错误消息所示。我想你可能想尝试让这些成为整数 mod n,或者甚至更好地只获得其中一个整数。