如何使用RSA加密在C#中的文件(海量数据) [英] how to use RSA to encrypt files (huge data) in C#

查看:187
本文介绍了如何使用RSA加密在C#中的文件(海量数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来加密。我需要实现非对称加密算法,我认为它使用公钥/私钥。我开始使用的RSACryptoServiceProvider的样本。这是确定的小数据进行加密。但使用它的相对较大的数据2线的时候,我得到的异常CryptographicException坏长度!

I'm new to encryption. I need to implement asymmetric encryption algorithm, which i think it uses private/public key. I started using a sample of RSACryptoServiceProvider. it was ok with small data to encrypt. But when using it on relatively larger data "2 lines", i get the exception CryptographicException "Bad Length"!

//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{

    //Import the RSA Key information. This only needs
    //toinclude the public key information.
    //RSA.ImportParameters(RSAKeyInfo);
    byte[] keyValue = Convert.FromBase64String(publicKey);
    RSA.ImportCspBlob(keyValue);

    //Encrypt the passed byte array and specify OAEP padding.  
    //OAEP padding is only available on Microsoft Windows XP or
    //later.  
    encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}

这时我发现加密通过CryptoStream的大数据(或文件)的一些样品,并且只能使用对称算法如DES或3DES,其具备的功能CreateEncryptor返回ICryptoTransform的作为输入的CryptoStream的构造函数中的一个! !

Then I found some samples of encrypting large data (or files) by using CryptoStream, and only use symmetric algorithms like DES or 3DES, which have the function CreateEncryptor to return ICryptoTransform as one of the input to the constructor of CryptoStream!!!

CryptoStream cStream = new CryptoStream(fStream,
                new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
                CryptoStreamMode.Write);

什么是加密使用RSA文件的方式?

What is the way to encrypt files using RSA?

推荐答案

在其他的答案非对称加密是专为加密小于它的关键尺寸数据提及。

As mentioned in other answers asymmetric encryption is only designed for encrypting data smaller than it's key size.

需要两个系统之间传送大量加密的数据时,我已实施的一种选择是有一个RSA密钥对的公开密钥数据时需要是已知的发送者和接收者然后既要发送的接收机产生一个新的RSA密钥对,加密与共同公钥密钥对的公开密钥和将加密的公共密钥给发件人。发送者使用其私有密钥(其中接收器并不需要知道,就像发送者不需要知道该接收机产生的私有密钥)​​解密接收公共密钥,产生一个对称加密密钥,数据用该对称密钥加密然后加密使用从接收器接收的公共密钥的对称密钥。两个加密的对称密钥,然后将加密的数据发送给它使用它的生成的私钥来解密对称密钥的接收机,然后对数据进行解密。

One option that I have implemented when needing to transfer large amounts of encrypted data between two systems is to have an RSA keypair whose public key is known to both the sender and the receiver then when data needs to be sent the receiver generates a new RSA keypair, encrypts the public key of that keypair with the common public key and sends the encrypted public key to the sender. The sender decrypts the receivers public key using it's private key (which the receiver does not need to know, just as the sender does not need to know the receivers generated private key), generates a symmetric encryption key, encrypts the data with the symmetric key and then encrypts the symmetric key using the public key received from the receiver. Both the encrypted symmetric key and the encrypted data are then sent to the receiver which uses it's generated private key to decrypt the symmetric key and then decrypts the data.

您可以使用RSACRyptoServiceProvider.ToXMLString和RSACryptoServiceProvider.FromXMLString方法来存储公用公钥为XML字符串在接收器应用程序的文字。

You can use the RSACRyptoServiceProvider.ToXMLString and RSACryptoServiceProvider.FromXMLString methods to store the common public key as an XML string literal in the receiver application.

不要忘了,当你生成对称加密密钥使用RNGCryptoServiceProvider()来生成密钥,因为它是生成(伪)随机数的更安全的方法。

Don't forget, when you generate the symmetric encryption key to use RNGCryptoServiceProvider() to generate the key as it is a much more secure method of generating (pseudo) random numbers.

另外,我强烈建议不要使用3DES作为对称加密算法,它是旧的,开始显示出它的年龄。使用AES对称加密与任何AesCryptoServiceProvicer或RijndaelManaged的类。

Also, I strongly recommend against using 3DES as your symmetric encryption algorithm, it is old and starting to show it's age. Use AES symmetric encryption with either AesCryptoServiceProvicer or RijndaelManaged classes.

这篇关于如何使用RSA加密在C#中的文件(海量数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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