实际上,为了解密用公钥加密的消息,有了私有指数就足够了。
这也意味着您可以对消息进行签名,因为签名基本上就是用私钥对明文进行*de*加密,当用公钥对明文进行*en*加密时,私钥将再次给出明文。通常你会在明文之前使用一个哈希摘要并签名。。。
不能仅用n和d对消息进行解密的原因是,它在消息解密过程中执行blinding step,这是involves the public exponent,但解密并不真正需要。
但是通过使用对私有API的一些调用,可以绕过这一步。
因此,这应该起作用:from Crypto.PublicKey import RSA
from Crypto.Util.number import bytes_to_long, long_to_bytes
full = RSA.generate(2048)
# construct key using only n and d
try:
# pycrypto >=2.5, only tested with _slowmath
impl = RSA.RSAImplementation(use_fast_math=False)
partial = impl.construct((full.n, 0L))
partial.key.d = full.d
except TypeError:
# pycrypto <=2.4.1
partial = RSA.construct((full.n, 0L, full.d))
pub = full.publickey()
# create message with padding
# http://en.wikipedia.org/wiki/RSA_%28algorithm%29#Padding_schemes
cleartext = ...
signature = partial.sign(cleartext, None)
print "validating message: ", pub.verify(cleartext, signature)
message = pub.encrypt(cleartext, None)
# bypassing the blinding step on decrypt
enc_msg=map(bytes_to_long, message)
dec_msg = map(partial.key._decrypt, enc_msg)
print "decrypting: "
for m in dec_msg:
print long_to_bytes(m)