简体   繁体   English

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

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

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使用公钥解密?

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生成一对密钥 - 公钥和私钥。 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. RSA的设计使得如果您应用公钥然后应用私钥,反之亦然,您将收到相同的消息。 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. 为了使用RSA进行加密,Alice使用Bob的公钥加密消息。 The only way to read this message is with Bob's private key, which only he has. 阅读此消息的唯一方法是使用Bob的私钥,只有他拥有。 Thus Eve can't read the message because he does not have this key. 因此,Eve无法阅读该消息,因为他没有此密钥。 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. 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. 要使用RSA进行签名,Alice会获取消息的哈希值,使用自己的私钥加密哈希值,并将结果(这是签名)附加到消息中。 Eve can of course still decrypt this using Alice's public key. Eve当然可以使用Alice的公钥解密它。 However, Bob can decrypt the signature using Alice's public key and see if it matches. 但是,Bob可以使用Alice的公钥解密签名,看看它是否匹配。 If it does, it must have been encrypted using Alice's private key, which only she has, so it must have come from 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. 现在,我不熟悉.NET加密API,所以我不确定它是否与此处描述的完全一致。 But this explanation might help you understand some of the answers you are getting. 但是这个解释可能会帮助您理解您获得的一些答案。

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. 编辑:我应该在这个答案RSACyrptoServiceProvider说,特定的.NET RSACyrptoServiceProvider可能不会支持这个,因为货物崇拜“知道”这是不可能的,或者更实用的知识,这在实践中很少有用。

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. 对他们而言,这意味着计算消息摘要或HMAC,并使用私钥加密。 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算法的参考,请参见http://fringe.davesource.com/Fringe/Crypt/RSA/Algorithm.html

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 . 该操作存在:它是DecryptValue -method,由RSACryptoServiceProvider的基类定义: System.Security.Cryptography.RSA Unfortunately, it is not supported by RSACryptoServiceProvider (since the underlying win32-api, CryptoAPI, does not support it). 不幸的是,RSACryptoServiceProvider不支持它(因为底层的win32-api,CryptoAPI不支持它)。 If you could get hold of another .NET-implementation of the RSA-class, you would be able to do it, however. 但是,如果您可以获得RSA类的另一个.NET实现,那么您将能够执行此操作。

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. 使用public进行私有和decypt加密,OR,使用public进行加密,使用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. 此外,您可以从私钥派生公钥,但反之亦然。

You can both encrypt and decrypt with PrivateKey. 您可以使用PrivateKey进行加密和解密。 PrivateKey infact contains both Private and PublicKey. PrivateKey infact包含Private和PublicKey。

Theoretically at least you can encrypt with PublicKey and decrypt with PrivateKey and vice-versa. 从理论上讲,至少你可以用PublicKey加密并用PrivateKey解密,反之亦然。 In VB.net I see the first case works and secondcase throws BadKey error 在VB.net中,我看到第一个案例工作,第二个案例抛出BadKey错误

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

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密钥。

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". 公钥密码系统的安全性取决于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的非对称密钥长度有很多论文。

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

相关问题 .NET RSACryptoServiceProvider 使用 4096 私钥加密,如何在 Android 上对其进行解密 - .NET RSACryptoServiceProvider encrypt with 4096 private key, how to decrypt it on Android RSACryptoServiceProvider仅使用私钥解密 - RSACryptoServiceProvider decrypt using only private key 使用C#中的RSA私钥文件创建RSACryptoServiceProvider对象 - Create RSACryptoServiceProvider object using RSA private key file in C# 使用密码或其他方式保护RSACryptoServiceProvider私钥 - Protecting RSACryptoServiceProvider private key with password or otherwise RSACryptoServiceProvider(RSACryptoServiceProvider)构造函数在.NET 3.5中因某些键名而崩溃 - RSACryptoServiceProvider(RSACryptoServiceProvider) constructor crashing in .NET 3.5 for certain key names 如何在RSACryptoServiceProvider .NET类中指定公钥 - How to specify public key in RSACryptoServiceProvider .NET class 我可以从RSACryptoServiceProvider获取纯文本形式的私钥 - Can I get private key in form of plain text from RSACryptoServiceProvider .NET 6 中的 RSACryptoServiceProvider - RSACryptoServiceProvider in .NET 6 RSA加密 - 使用私钥加密 - RSA Encryption - Encrypt with private key RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key 在 .NET Core 中不起作用 - RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key does not work in .NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM