exception raised when decrypt with private key re-constructed by only n, d
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Python-Crypto |
New
|
Undecided
|
Unassigned |
Bug Description
the following code would raise the exception
python 3.5 64bit on windows 10 64bit
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as cipher_pkcs1
public_key = """-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb
G+NYvQAToihP+
2phryr+
-----END PUBLIC KEY-----"""
# original private key which contains n,d,e,p,q
# private_key = """-----BEGIN PRIVATE KEY-----
# MIICdgIBADANBgk
# 8s2lCUKgVOwb41i
# 9K8SsygwW4DamGv
# gYBq7qhnclBebzW
# stMjDYXfexfL+
# LAQsCzkS+
# O/NDmKrtSvATzuK
# ao+ntPDCyf93Njf
# uvO3Uw9tvFxSlJ3
# Lmvi/TWT5K/
# 64qQuwJD+
# OOLi6VEyjw==
# -----END PRIVATE KEY-----"""
#private key re-constructed by n d and p q not present
private_key = """-----BEGIN PRIVATE KEY-----
MIIBNgIBADANBgk
8s2lCUKgVOwb41i
9K8SsygwW4DamGv
au6oZ3JQXm81mSq
Iw2F33sXy/
LAs5EviED/
-----END PRIVATE KEY-----"""
message = 'abcdef'.encode()
pub_key = RSA.importKey(
cipher = cipher_
encrypted = cipher.
print("{} encrypted as {}".format(message, encrypted))
pri_key = RSA.importKey(
cipher = cipher_
sentinel = None
decrypted = cipher.
print("{} decrypted as {}".format(
the error looks like:
File "C:\Anaconda3\
m = self._key.
File "C:\Anaconda3\
return pubkey.
File "C:\Anaconda3\
plaintext=
File "C:\Anaconda3\
mp = self.key.
File "C:\Anaconda3\
m1 = pow(c, self.d % (self.p-1), self.p)
ValueError: pow() 3rd argument cannot be 0
using the original private key instead of the re-constructed one, everything is fine.
with the following modifications, the decryption would work:
1. _slowmath.py line 53
-if (hasattr(self,'p') and hasattr(self,'q') and hasattr(self,'u')):
+if (hasattr(self,'p') and hasattr(self,'q') and hasattr(self,'u') and self.p > 0 and self.q > 0):
2. RSA.py line 235
-r = getRandomRange(1, self.key.n-1, randfunc=
+r = 1
have no idea why the r has to be 1, 1 just works.