简体   繁体   English

如何使用 RSACryptoServiceProvider 解密加密文本?

[英]how to decrypt an encrypted text using RSACryptoServiceProvider?

I have encrypted a text using RSACryptoServiceProvider.我已经使用 RSACryptoServiceProvider 加密了一个文本。 I exported the public and private key.我导出了公钥和私钥。 Obviously I just want to expose the public key inside the decoder application, so I have written a code as follows:显然我只是想在解码器应用程序内部公开公钥,所以我写了如下代码:

private const string PublicKey = "<RSAKeyValue><Modulus>sIzQmj4vqK0QPd7RXKigD7Oi4GKPwvIPoiUyiKJMGP0qcbUkRPioe2psE/d3c1a2NY9oj4Da2y1qetjvKKFad2QAhXuql/gPIb1WmI+f6q555GClvHWEjrJrD/ho7SLoHbWd6oY6fY609N28lWJUYO97RLVaeg2jfNAUSu5bGC8=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

private string Decrypt()
        {
            byte[] encryptedKeyAsBytes = Convert.FromBase64String(_encryptedKey);
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(PublicKey);
            // read ciphertext, decrypt it to plaintext
            byte[] plainBytes = rsa.Decrypt(encryptedKeyAsBytes, false);
            string plainText = System.Text.Encoding.ASCII.GetString(plainBytes);

            return plainText;
        }

But an exception is thrown at line "byte[] plainBytes = rsa.Decrypt(encryptedKeyAsBytes, false);"但在“byte[] plainBytes = rsa.Decrypt(encryptedKeyAsBytes, false);”行抛出异常and says "Key does not exist."并说“钥匙不存在”。 However if I expose the whole private and public key then it runns happily.但是,如果我公开整个私钥和公钥,那么它会运行得很愉快。 So how can I decrypt the data using only the public key information?那么如何仅使用公钥信息解密数据呢?

You can't - that is the point of public/private key encryption.你不能——这就是公钥/私钥加密的重点。 The public does the encryption;公众进行加密; the private does the decryption.私人进行解密。

It sounds like you need some sort of key exchange pattern.听起来您需要某种密钥交换模式。 For example;例如; if your decoder application is trying to decrypt information from another data source (Source Application), I would implement something like this:如果您的解码器应用程序试图从另一个数据源(源应用程序)解密信息,我会实现这样的事情:

  1. The Source Application generates a symmetric key, like AES.源应用程序生成一个对称密钥,如 AES。
  2. The Decoder application generates a public and private key pair.解码器应用程序生成一个公钥和私钥对。
  3. The Source Application asks the Decoder application for the public key.源应用程序向解码器应用程序询问公钥。
  4. The Source application encrypts the symmetric key using the public key, and sends it back to the Decoder application.源应用程序使用公钥加密对称密钥,并将其发送回解码器应用程序。
  5. The Decoder application uses the private key to decrypt the symmetric key.解码器应用程序使用私钥来解密对称密钥。
  6. The Decoder application gets data encrypted with the symmetric key from the Source Application.解码器应用程序从源应用程序获取使用对称密钥加密的数据。
  7. The Decoder Application uses the exchanged symmetric key to decrypt the information it received.解码器应用程序使用交换的对称密钥来解密它收到的信息。

There is just an example;这只是一个例子; but illustrates the basics of how to exchange data between two applications without any sensitive information transmitted over the wire.但说明了如何在两个应用程序之间交换数据而无需通过网络传输任何敏感信息的基础知识。 The symmetric key is not required at all;根本不需要对称密钥; but is a very common pattern because RSA starts to introduce problems when encrypting large amounts of information.但这是一种非常常见的模式,因为 RSA 在加密大量信息时开始引入问题。 RSA is better to just encrypt an symmetric encryption key instead. RSA 最好只加密对称加密密钥。

The short answer is: you can't.简短的回答是:你不能。 To decrypt messages you need the private key, that's the major principle of asymmetric cryptography.要解密消息,您需要私钥,这是非对称密码学的主要原则。

You encrypt messages using someone's public key so that only the person in possession of the corresponding private key is able to decrypt them.您使用某人的公钥加密消息,以便只有拥有相应私钥的人才能解密它们。

That's why the public key is called public - you may safely distribute it to the public so that they can encrypt messages to be read by you who is the sole owner of the corresponding private key.这就是为什么公钥被称为公开的 - 您可以安全地将它分发给公众,以便他们可以加密消息以供您作为相应私钥的唯一所有者阅读。

The problem is that you're confusing encryption and signing.问题是您混淆了加密和签名。

Encryption is where anyone may write a message, but only the private key holder may read it.加密是任何人都可以消息的地方,但只有私钥持有者可以读取它。 Signing is where anyone may read a message, but only the private key holder may write it.签名是任何人都可以阅读消息的地方,但只有私钥持有者可以编写它。

When you call Decrypt, the RSACryptoServiceProvider is looking for encryption , that is, public write private read.当你调用 Decrypt 时,RSACryptoServiceProvider 正在寻找加密,即公写私有读。 Thus it looks for the private key.因此它寻找私钥。

You want to use the SignData and VerifyData functions to sign the payload so that people can't write it.您想使用 SignData 和 VerifyData 函数对有效负载进行签名,这样人们就无法编写它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM