如何在python中保留模?

2024-09-29 01:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图对公钥和私钥算法做一个简单的解释。加密部分工作正常,但是解密部分有点不正常。我只想要模(2^7,33)的剩余值29。到目前为止,我的情况是:

p= 3
q= 11
n = p*q
z = (p-1)*(q-1)
e = 7
d = (z + 1)/e
a= divmod(d*e,z)

PublicKey = (e,n)
PrivateKey = (d,n)

#Encrypt
message = 2
c = divmod(2**e,n)
print c

#Decrypt
decryption = divmod((c**2),n)
print decryption

错误是:

    Traceback (most recent call last):
    File "C:/Python27/rsa2.py", line 17, in <module>
    decryption = divmod((c**2),n)
    TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'             

Tags: 算法messagemost错误情况encrypt私钥print