是否可以使用 .net RSACryptoServiceProvider 使用私钥进行加密?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1181421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:04:23  来源:igfitidea点击:

Is it possible to encrypt with private key using .net RSACryptoServiceProvider?

.netcryptographyrsa

提问by guaike

I know that RSACryptoServiceProvider can encrypt with the public key, then it can be decrypted with the private key.

我知道 RSACryptoServiceProvider 可以用公钥加密,然后可以用私钥解密。

Is it possible to encrypt with the private key and decrypt with the public key using the RSACryptoServiceProvider ?

是否可以使用 RSACryptoServiceProvider 使用私钥加密并使用公钥解密?

采纳答案by blowdart

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.

如果您想将私钥应用于消息,也许您正在寻找签名而不是加密?这是一种不同的加密方案,也可以使用 RSA 密钥。

回答by Alex319

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 可用于加密(确保 Eve 无法读取 Alice 发送给 Bob 的消息)或用于签名(确保如果 Alice 向 Bob 发送消息,Bob 知道实际上是 Alice 发送了消息,而不是 Eve 假装成为爱丽丝)

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.

RSA 生成一对密钥——一个公钥和一个私钥。RSA 的设计使得如果您应用公钥然后应用私钥,反之亦然,您将收到相同的消息。公钥可以从私钥推导出来,但相反是不可能的。

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.

为了使用 RSA 进行加密,Alice 使用 Bob 的公钥加密消息。阅读此消息的唯一方法是使用 Bob 的私钥,只有他拥有。因此 Eve 无法阅读消息,因为他没有这把钥匙。另一方面,这不提供消息来源的身份验证。Eve 还可以获得 Bob 的公钥(因为它是公开的)并向 Bob 发送消息,假装是 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.

为了使用 RSA 进行签名,Alice 获取消息的散列,使用她自己的私钥加密散列,并将结果(这是签名)附加到消息中。Eve 当然仍然可以使用 Alice 的公钥对其进行解密。但是,Bob 可以使用 Alice 的公钥解密签名并查看它是否匹配。如果是这样,它一定是用 Alice 的私钥加密的,只有她才有,所以它一定来自 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 加密 API,所以我不确定它是否完全按照这里的描述工作。但是这个解释可能会帮助你理解你得到的一些答案。

回答by smaudet

EDIT: I should preface this answer by saying that the specific .NET RSACyrptoServiceProviderlikely 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.

编辑:我应该在这个答案的开头说特定的 .NETRSACyrptoServiceProvider可能不会支持这一点,因为货物崇拜“知识”这是不可能的,或者更实用的知识,这在实践中很少有用。

ORIGINAL:

原来的:

Everyone claiming that there is no such thing either doesn't know how RSA works, or they are stuck in the "signing" rut.

每个声称没有这样的东西的人要么不知道 RSA 的工作原理,要么陷入“签名”的困境。

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.

用私钥加密是完全可能的,而且完全合理。是的,这类似于签名,但这根本不是大多数现代图书馆所认为的签名。对他们来说,这意味着计算消息摘要或 HMAC,并使用私钥进行加密。将用私钥加密比作签名就像说将文件放在保险箱里,然后把钥匙放在身边,是签署文件的替代品一样有意义。

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.htmlfor reference on the RSA algorithm.

有关RSA 算法的参考,请参阅http://fringe.davesource.com/Fringe/Crypt/RSA/Algorithm.html

回答by Rasmus Faber

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).

使用私钥执行原始 RSA 操作通常称为解密操作(就像使用公钥执行它称为加密操作一样)。

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.

该操作存在:它是DecryptValue方法,它由 RSACryptoServiceProvider 的基类定义:System.Security.Cryptography.RSA。不幸的是,它不受 RSACryptoServiceProvider 支持(因为底层的 win32-api CryptoAPI 不支持它)。但是,如果您能掌握 RSA 类的另一个 .NET 实现,您就可以做到。

回答by Remus Rusanu

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.

虽然当密钥角色颠倒时数学涉及是有意义的(这就是签名的工作方式),但当解密密钥众所周知且公开时,为隐私加密就没有多大意义。

回答by Attila

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.

此外,您可以从私钥派生公钥,但反之则不行。

回答by JustWe

This is what I understand RSA signature.

这就是我理解的RSA签名。

pseudo code:

伪代码:

First Alice made a signature:

首先爱丽丝做了一个签名:

alice_signature = encrypt(alice_message, alice_private_key)

Then Bob Eve... (anyone who having alice_public_key) verify the signature:

然后 Bob Eve...(任何拥有 的人alice_public_key)验证签名:

decrypted_message = decrypt(alice_signature, alice_public_key)

To confirm:

确认:

if(received_message_from_alice == decrypted_message)
  the signature proved the message is from alice

回答by user1770145

You can both encrypt and decrypt with PrivateKey. PrivateKey infact contains both Private and PublicKey.

您可以使用 PrivateKey 加密和解密。PrivateKey 实际上包含 Private 和 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

理论上至少你可以用公钥加密,用私钥解密,反之亦然。在 VB.net 中,我看到第一种情况有效,而第二种情况会引发 BadKey 错误

回答by Sean A.O. Harney

The security of public key cryptosystems rests on the fact that the sign()/encrypt() function is a one-way functionin that it would take an infeasible amount of time to decrypt it without the public key "trap-door".

公钥密码系统的安全性基于这样一个事实,即 sign()/encrypt() 函数是一种单向函数,因为在没有公钥“陷阱门”的情况下解密它会花费不可行的时间。

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.

此外,通常生成的密钥长度不同,尽管它们可能是。有很多关于 RSA 非对称密钥长度的论文。