如何使用的RSACryptoServiceProvider解密加密的文本? [英] how to decrypt an encrypted text using RSACryptoServiceProvider?

查看:185
本文介绍了如何使用的RSACryptoServiceProvider解密加密的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用加密的RSACryptoServiceProvider文本。我导出的公钥和私钥。很显然,我只是想揭露解码器应用程序内的公共密钥,所以我已经写了代码如下:

 私人常量字符串公钥=下; RSAKeyValue>&下;模量> sIzQmj4vqK0QPd7RXKigD7Oi4GKPwvIPoiUyiKJMGP0qcbUkRPioe2psE / d3c1a2NY9oj4Da2y1qetjvKKFad2QAhXuql / gPIb1WmI + f6q555GClvHWEjrJrD / ho7SLoHbWd6oY6fY609N28lWJUYO97RLVaeg2jfNAUSu5bGC8 = LT /模量>&下;指数> AQAB&下; /指数>&下; / RSAKeyValue>中; 

私人字符串解密()
{
字节[] = encryptedKeyAsBytes Convert.FromBase64String(_encryptedKey);
的RSACryptoServiceProvider RSA =新的RSACryptoServiceProvider();
rsa.FromXmlString(公钥);
//读取密文,它解密成明文
字节[] = plainBytes rsa.Decrypt(encryptedKeyAsBytes,FALSE);
字符串明文= System.Text.Encoding.ASCII.GetString(plainBytes);

返回明文;
}



但是,一个例外是在一线的byte [] plainBytes = rsa.Decrypt抛出(encryptedKeyAsBytes,FALSE);
说键不存在。但是如果我揭露了整个私钥和公钥然后愉快地runns。那么,如何才能解密仅使用公开密钥信息的数据。


解决方案

您不能 - ?这是公共点/私有密钥加密。公众并加密;民营确实解密。



这听起来像你需要某种形式的密钥交换模式。例如;如果你的解码器应用程序试图解密从另一个数据源(源应用程序)的信息,我将实施这样的:




  1. 源应用程序生成一个对称密钥,如AES。

  2. 解码器应用程序生成一个公共和私有密钥对。

  3. 源应用程序要求的解码器应用程序公共密钥。

  4. 源应用程序加密使用公钥对称密钥,并将其发送回给解码器的应用程序。

  5. 解码器应用程序使用私钥解密对称密钥。

  6. 解码器应用程序获取与来自源应用程序的对称密钥加密的数据。

  7. 解码器应用程序使用所交换的对称密钥来解密接收到的信息



  8. 有仅仅是一个例子。但说明了如何在不通过线路传输的敏感信息交换两个应用之间的数据的基础知识。对称密钥并不在所有必需的;但是是一个很常见的模式,因为RSA加密开始大量信息时带来问题。 RSA是最好只使用加密的对称加密密钥来代替。


    I have encrypted a text using 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);" 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.
    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 is better to just encrypt an symmetric encryption key instead.

    这篇关于如何使用的RSACryptoServiceProvider解密加密的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆