是否可以使用.net RSACryptoServiceProvider 使用私钥进行加密?
我知道RSACryptoServiceProvider可以用公钥加密,然后可以用私钥解密。
是否可以使用 RSACryptoServiceProvider 使用私钥加密并使用公钥解密?
I know that RSACryptoServiceProvider can encrypt with the public key, then it can be decrypted with the private key.
Is it possible to encrypt with the private key and decrypt with the public key using the RSACryptoServiceProvider ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
只是为了澄清一下:
RSA 可以用于加密(确保 Eve 无法读取 Alice 发送给 Bob 的消息)或用于签名(确保如果 Alice 向 Bob 发送消息,Bob 知道实际上是 Alice 发送的)发送消息,而不是假装是 Alice 的 Eve)
RSA 生成一对密钥 - 公钥和私钥。 RSA 的设计使得如果您应用公钥然后应用私钥(反之亦然),您将得到相同的消息。 并且可以从私钥推导出公钥,但反之则不可能。
为了使用 RSA 进行加密,Alice 使用 Bob 的公钥对消息进行加密。 读取此消息的唯一方法是使用 Bob 的私钥,只有他拥有该私钥。 因此 Eve 无法读取该消息,因为他没有此密钥。 另一方面,这不提供消息来源的验证。 Eve 还可以获取 Bob 的公钥(因为它是公开的)并假装是 Alice 向 Bob 发送消息。
要使用 RSA 进行签名,Alice 获取消息的哈希值,使用她自己的私钥加密该哈希值,并将结果(这是签名)附加到消息中。 当然,Eve 仍然可以使用 Alice 的公钥对其进行解密。 但是,Bob 可以使用 Alice 的公钥解密签名并查看其是否匹配。 如果是这样,那么它一定是使用 Alice 的私钥加密的,而只有她拥有该私钥,因此它一定来自 Alice。
现在,我不熟悉 .NET 加密 API,因此我不确定它是否完全按照此处所述工作。 但这个解释可能会帮助您理解您得到的一些答案。
Just to clear things up a bit:
RSA can be used either for encryption (ensuring that Eve cannot read messages that Alice sends to Bob) or for signing (ensuring that if Alice sends a message to Bob, Bob knows that it was actually Alice that sent the message, and not Eve pretending to be Alice)
RSA generates a pair of keys - a public key and a private key. RSA is designed so that if you apply the public key and then apply the private key, or vice versa, you will get the same message back. And the public key can be derived from the private key, but the opposite is impossible.
To use RSA for encryption, Alice encrypts the message using Bob's public key. The only way to read this message is with Bob's private key, which only he has. Thus Eve can't read the message because he does not have this key. On the other hand, this provides no authentication of the source of the message. Eve can also get Bob's public key (since it's public) and send messages to Bob, pretending to be Alice.
To use RSA for signing, Alice takes a hash of the message, encrypts the hash using her own private key, and appends the result (this is the signature) to the message. Eve can of course still decrypt this using Alice's public key. However, Bob can decrypt the signature using Alice's public key and see if it matches. If it does, it must have been encrypted using Alice's private key, which only she has, so it must have come from Alice.
Now, I'm not familiar with the .NET cryptography API, so I'm not sure if it works exactly as described here. But this explanation might help you understand some of the answers you are getting.
编辑:我应该在这个答案的前言中说,特定的 .NET
RSACyrptoServiceProvider
可能不会支持这一点,因为货物崇拜“知识”认为这是不可能的,或者更务实的知识认为这很少有用在实践中做。原文:
每个声称不存在这样的东西的人要么不知道 RSA 是如何工作的,要么就是陷入了“签名”的困境。
使用私钥加密是完全可能的,也是完全有意义的。 是的,这与签名类似,但这根本不是大多数现代图书馆所认为的签名。 对他们来说,这意味着计算消息摘要(HMAC),并使用私钥进行加密。 将私钥加密比作签名,就像说将文档放在保险箱中并将密钥放在周围一样,是签署文档的替代方法。
是的,它正在加密,因为它是相同的操作。 私钥加密的密文与公钥加密的密文一样难以辨认; 需要两个密钥才能解密密文。
请参阅http://fringe.davesource.com/Fringe/Crypt/RSA/Algorithm .html 供 RSA 算法参考。
EDIT: I should preface this answer by saying that the specific .NET
RSACyrptoServiceProvider
likely will not support this, due the cargo cult "knowledge" that this is impossible or the more pragmatic knowledge that this is rarely useful to do in practice.ORIGINAL:
Everyone claiming that there is no such thing either doesn't know how RSA works, or they are stuck in the "signing" rut.
It is entirely possible, and makes complete sense, to encrypt with the private key. Yes, this is similar to signing, but this is NOT at all what most modern libraries take as signing. To them, this means computing a message digest, or HMAC, and encrypting with the private key. Likening encryption with the private key to signing makes just as much sense as saying that sticking a document in a safe, and leaving the key lying around, is a stand-in for signing the document.
Yes, it IS encrypting, because its the same operation. The private-key encrypted ciphertext is just as illegible as the public-key encrypted ciphertext; one needs both keys to decrypt the ciphertext.
See http://fringe.davesource.com/Fringe/Crypt/RSA/Algorithm.html for reference on the RSA algorithm.
使用私钥执行原始 RSA 操作通常称为解密操作(就像使用公钥执行该操作称为加密操作一样)。
访问此操作很有用 - 例如实现框架不支持的操作。
该操作存在:它是 DecryptValue-方法,由 RSACryptoServiceProvider 的基类定义: 系统.安全.加密.RSA。 不幸的是,RSACryptoServiceProvider 不支持它(因为底层 win32-api CryptoAPI 不支持它)。 不过,如果您能找到 RSA 类的另一个 .NET 实现,您就能够做到这一点。
Performing the raw RSA operation with the private key is usually called the decryption operation (just as performing it with the public key is called the encryption operation).
It is useful to have access to this operation - for example to implement an operation that is not supported by the framework.
The operation exists: it is the DecryptValue-method, which is defined by RSACryptoServiceProvider's base-class: System.Security.Cryptography.RSA. Unfortunately, it is not supported by RSACryptoServiceProvider (since the underlying win32-api, CryptoAPI, does not support it). If you could get hold of another .NET-implementation of the RSA-class, you would be able to do it, however.
不,这不是任何公钥/私钥加密的工作原理。 只能用公钥加密,只能用私钥解密。
如果您想将私钥应用于消息,也许您正在寻找签名,而不是加密? 这是一种不同的加密方案,也可以使用 RSA 密钥。
No. That's not how any public/private key encryption works. You can only encrypt with the public key, and only decrypt with the private key.
If you want to apply the private key to a message, maybe you're looking for a signature, rather than encryption? This is a different cryptographic scheme that can also use RSA keys.
幸运的是没有。 但是,您可以使用私钥进行签名并使用公钥验证签名。
虽然当关键角色颠倒时涉及的数学是有意义的(这就是签名的工作原理),但当解密密钥众所周知且公开时,隐私加密就没有多大意义。
Fortunately no. You can however sign with the private key and verify the signature with the public key.
While the math involve makes sense when the key roles are reversed (and this is how signatures work), encrypting for privacy doesn't make much sense when the decryption key is well know and public.
你可以两者都做; 使用私有加密并使用公共解密,或者使用公共加密并使用私有解密。 您不能仅使用私钥加密然后解密,也不能仅使用公钥执行相同的操作。
莱姆斯成功了; 当解密密钥众所周知且公开时,使用私钥加密没有多大意义。
此外,您可以从私钥导出公钥,但反之则不然。
You can do both; encrypt with private and decypt with public, OR, encrypt with public and decrypt with private. You can not encrypt then decrypt with private key only, nor can you do the same with public keys alone.
Remus nailed it; encrypting with the private key doesn't make much sense when the decryption key is well know and public.
Also, you can derive the public key from the private key, but not vice versa.
这就是我理解的RSA签名。
伪代码:
首先 Alice 进行了签名:
然后 Bob Eve...(任何拥有
alice_public_key
的人)验证签名:确认:
This is what I understand RSA signature.
pseudo code:
First Alice made a signature:
Then Bob Eve... (anyone who having
alice_public_key
) verify the signature:To confirm:
您可以使用 PrivateKey 进行加密和解密。 PrivateKey实际上包含Private和PublicKey。
从理论上讲,至少您可以使用公钥加密并使用私钥解密,反之亦然。 在 VB.net 中,我看到第一种情况有效,第二种情况抛出 BadKey 错误
You can both encrypt and decrypt with PrivateKey. PrivateKey infact contains both Private and PublicKey.
Theoretically at least you can encrypt with PublicKey and decrypt with PrivateKey and vice-versa. In VB.net I see the first case works and secondcase throws BadKey error
公钥密码系统的安全性取决于以下事实:sign()/encrypt() 函数是 单向函数,因为如果没有公钥“活板门”,解密它会花费不可行的时间。
此外,通常生成的密钥的长度不相同,尽管它们可能是相同的。 有很多关于 RSA 的非对称密钥长度的论文。
The security of public key cryptosystems rests on the fact that the sign()/encrypt() function is a one-way function in that it would take an infeasible amount of time to decrypt it without the public key "trap-door".
Also, usually the generated keys are not the same length, although they could be. There is a lot of papers about asymmetric key length with RSA.