简体   繁体   English

我有模数和私有指数。 如何构建RSA私钥并签名消息?

[英]I have modulus and private exponent. How to construct RSA private key and sign a message?

I am newbie in cryptography and pycrypto. 我是密码学和pycrypto的新手。

I have modulus n and private exponent d . 我有模数n和私有指数d From what I understand after reading some docs private key consists of n and d . 根据我在阅读一些文档后的理解, 私钥nd

I need to sign a message and I can't figure out how to do that using pycrypto . 我需要签署一条消息,我无法弄清楚如何使用pycrypto RSA.construct() method accepts a tuple. RSA.construct()方法接受一个元组。 But I have to additionally provide public exponent e to this method (which I don't have). 但我还要为这种方法提供公共指数e (我没有)。

So here is my question. 所以这是我的问题。 Do I have to compute e somehow in order to sign a message? 我是否必须以某种方式计算e才能签署消息?

It seems I should be able to sign a message just by using n and d (that constitute private key). 似乎我应该能够通过使用nd (构成私钥)来签署消息。 Am I correct? 我对么? Can I do this with pycrypto ? 我可以用pycrypto做到这pycrypto吗?

Thanks in advance. 提前致谢。

Actually for decrypting a message encrypted with the public key it's enough to have the private exponent. 实际上,对于用公钥加密的消息进行解密,它就足以拥有私有指数。

That also means you can sign a message, because signing basically is just *de*crypting the plaintext with the private key, which when *en*crypted with the public key will give the plaintext again. 这也意味着您可以签署一条消息,因为签名基本上只是用私钥加密明文,当使用公钥加密* en *时,将再次给出明文。 Usually you use a hash digest on the plaintext before and sign that... 通常你之前在明文上使用哈希摘要并签名...

The reason why you can't decrypt a message uing only n and d with pyrcypto is that it does a blinding step during message decryption, which involves the public exponent , but isn't really needed for the decryption. 你不能解密只有ndpyrcypto的消息的原因是它在消息解密过程中做了一个盲目的步骤 ,它涉及公共指数 ,但实际上并不需要解密。

But by using some calls to the private API this step can be bypassed. 但是通过使用对私有API的一些调用,可以绕过此步骤。

Therefore this should work: 因此这应该工作:

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)

No, you can't compute e from d . 不,你不能从d计算e

RSA is symmetric in d and e : you can equally-well interchange the roles of the public and the private keys. RSA在de是对称的:您可以同样很好地交换公钥和私钥的角色。 Of course, we choose one specially to be private and reveal the other -- but theoretically they do the same thing. 当然,我们选择一个专门为私人揭示另一个 - 但理论上他们做同样的事情。 Naturally, since you can't deduce the private key from the public, you can't deduce the public key from the private either. 当然,由于您无法从公众中推断出私钥,因此您无法从私有中推断出公钥。

Of course, if you have the private key that means that you generated the keypair, which means that you have the public key somewhere. 当然,如果您拥有私钥,这意味着生成了密钥对,这意味着您在某处拥有公钥。

If you don't have the public exponent you may be able to guess it. 如果你没有公共指数,你可能会猜到它。 Most of the time it's not a random prime but a static value. 大多数情况下,它不是随机素数而是静态值。 Try the values 65537 (hex 0x010001 , the fourth number of Fermat), 3, 5, 7, 13 and 17 (in that order). 尝试值65537(十六进制0x010001 ,第四个费马数), 0x010001和17( 0x010001顺序)。

[EDIT] Simply sign with the private key and verify with the public key to see if the public key is correct. [编辑]只需使用私钥进行签名,然后使用公钥进行验证,以查看公钥是否正确。

Note: if it is the random prime it is as hard to find as the private exponent; 注意:如果它是随机素数,那么很难找到私有指数; which means you would be trying to break RSA - not likely for any key sizes > 512 bits. 这意味着你将尝试打破RSA - 不太可能是任何密钥大小> 512位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个 RSA 公钥指数和模数。 如何使用 Python 加密字符串? - I have a RSA public key exponent and modulus. How can I encrypt a string using Python? 根据模数和公共指数重新生成公钥和私钥 - Regenerating Public and Private Key based on modulus and public exponent 如何在 Python 中使用以太坊私钥签署消息? - How can I sign message using an Ethereum private key in Python? Python代码,使用私有指数和模数以64位的块解密RSA加密文件 - Python code that decrypts RSA encrypted file in chunks of 64 bit using a private exponent and modulus Python 如何使用模数 + 指数使用 RSA 进行编码 - Python how to encode with RSA using modulus + exponent 在 Python 密码学库中提取模数 n、私有指数和公共指数 - Extract modulus n, private exponent and public exponent in Python cryptography library 如何使用 python 中的 RSA 私钥进行加密? - How can I encrypt with a RSA private key in python? 如何在python中使用加密的RSA私钥(AES-256-CBC)对数据进行签名 - How to sign data with encrypted RSA private key (AES-256-CBC) in python 如何在PyCrypto中使用加密的RSA私钥? - How to use encrypted RSA private key with PyCrypto? 如何在Python中计算RSA私钥 - How to calculate RSA private key in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM