如何在 C# 中使用 RSA 加密文件(大数据)

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

问题描述

我是加密新手.我需要实现非对称加密算法,我认为它使用私钥/公钥.我开始使用 RSACryptoServiceProvider 的示例.可以加密小数据.但是当在相对较大的数据2行"上使用它时,我得到异常 CryptographicException Bad Length"!

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 its 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 its 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 its 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 作为对称加密算法,它已经过时并且开始显示其年龄.对 AesCryptoServiceProvicerRijndaelManaged 类使用 AES 对称加密.

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)