C# RSA加密大文件

C# RSA加密大文件,c#,base64,rsa,rsacryptoserviceprovider,C#,Base64,Rsa,Rsacryptoserviceprovider,问题是,我使用rsa加密了一个文件,使用了以下代码: for (int a = 0; a <= iterations; a++) { byte[] plain; int rsaLen = rsa.KeySize / 8 - 11; int bytesLen = plain.Length; int block = bytesLen - rsaLen * a;

问题是,我使用rsa加密了一个文件,使用了以下代码:

        for (int a = 0; a <= iterations; a++)
        {
            byte[] plain;
            int rsaLen = rsa.KeySize / 8 - 11;
            int bytesLen = plain.Length;
            int block = bytesLen - rsaLen * a;

            //The last block in the text may not be a full block
            if (block > rsaLen )
                plain = new byte[maxRsaLength];
            else
                plainblock = new byte[block];

            Buffer.BlockCopy(plaintext, rsaLen * a, plain, 0, plain.Length);

            //purfoming the encryption
            ciphertext.Append(Convert.ToBase64String(rsa.Encrypt(plain, false)));
        }
for(int a=0;a rsaLen)
普通=新字节[maxRsaLength];
其他的
plainblock=新字节[块];
Buffer.BlockCopy(纯文本,rsaLen*a,纯文本,0,纯文本.Length);
//加密
Append(Convert.ToBase64String(rsa.Encrypt(plain,false));
}
问题是,当我尝试解密时,我必须将我放入base 64的密文转换为base 64块,但随后我从RSAServiceProvider的解密方法中得到了一个错误的长度异常。我一直遵循这个网站上的例子: 无济于事。我没有得到任何加密错误,只是解密。我甚至不能确定我是否做得对。下面是我的解密循环:

   public string Decrypt(string ciphertext, string key = null)
    {
        //checking for ciphertext. Exception raise if null
        if (String.IsNullOrEmpty(ciphertext)) throw new ArgumentNullException(ciphertext, "There is no ciphertext to decrypt.");

        //String holding the decrypted value
        string plaintext = String.Empty;

        //chanck is the user has provided a key. If not the use the one automatically generated
        string keyToUse = String.IsNullOrEmpty(key) ? privatekey : key;

        //set the key
        rsa.FromXmlString(keyToUse);

        //Determine the blocksizes for the iterations
        int blockSize = ((rsa.KeySize / 8) % 3 != 0) ? (((rsa.KeySize / 8) / 3) * 4) + 4 : ((rsa.KeySize / 8) / 3) * 4;

        int iterations = ciphertext.Length / blockSize;
        byte[] allPlaintextAsBytes = new byte[0];


        try
        {
            for (int i = 0; i < iterations; i++)
            {
                //to decrypt this we have to take the cipher text from a base 64 string an array.
                byte[] cipherTextAsBytes = Convert.FromBase64String(ciphertext.Substring(blockSize * i, blockSize));

                byte[] partialPlaintextAsBytes = rsa.Decrypt(cipherTextAsBytes, false);
            }
        }....(Catch Exceptions down here)
公共字符串解密(字符串密文,字符串密钥=null)
{
//正在检查密文。如果为空,将引发异常
如果(String.IsNullOrEmpty(ciphertext))抛出新的ArgumentNullException(ciphertext,“没有要解密的密文”);
//包含解密值的字符串
string纯文本=string.Empty;
//chanck是指用户提供的密钥。如果没有,则使用自动生成的密钥
string keyToUse=string.IsNullOrEmpty(key)?privatekey:key;
//设定关键点
rsa.FromXmlString(keyToUse);
//确定迭代的块大小
int blockSize=((rsa.KeySize/8)%3!=0)?((rsa.KeySize/8)/3)*4)+4:((rsa.KeySize/8)/3)*4;
int迭代次数=密文长度/块大小;
字节[]allPlaintextAsBytes=新字节[0];
尝试
{
对于(int i=0;i
我知道这不是最好的分割文件到RSA它。是的,通常你加密密钥到一个流密码像AES与RSA和加密文件与AES。这是一个项目,我正在这样做,我必须这样做


提前感谢您的帮助。

现实世界中的RSA实现会进行填充,这会影响可加密的明文的最大长度


特别是,PKCS#1填充(最常见的一种)只支持k-11字节长的明文,其中k是密钥长度。OAEP填充只接受k-2*哈希长度-2长的明文(OAEP允许您更改正在使用的哈希算法).

确保分配该项目的人指出,这对其他可能不知道的学生来说是愚蠢的。使用嵌套对称加密不仅仅是一种安全约定:使用RSA直接加密是非常不安全的。我不希望一代学生认为prac提斯在任何方面都可以。@MyseriousDan我有点不同意。当然,这样做是愚蠢的。但是,“直接”RSA加密是安全的。这有点愚蠢吗?是的。使用512密钥或128密钥加密是愚蠢的,但它可以帮助人们开始了解这些数字是如何工作的。他要求我这样做的原因是为了让我能够做到这一点。通过做这个项目,我已经了解到,到目前为止,块密码无法加密大于模的块。我在我得到这个项目之前我不知道这一点。我可以有把握地说我不是专家,但那些专家不应该仅仅因为这是一种不寻常的做事方式就打倒这个问题,甚至不应该回答它。问题是关于我写的代码不起作用不是概念。哦,我不是故意不回答这个问题。我不知道“我不知道C#crypto API的细节,所以在这方面帮不了什么忙。@DuncanJones我不知道你说的直接RSA加密是什么意思,但做显而易见的事情(即,将明文提高到模块化能力)肯定不安全。第一个iPhone解锁是由苹果工程师实现的。”(或者他们签约的基带用户)不知道安全填充方案和Bleichenbacher攻击正是这类事情。@MyseriousDan我也不确定“direct”是什么意思-你在第一次评论中提到了它。无论如何,如果我们假设“normal”RSA加密涉及使用标准方案(如OAEP)填充明文,这样它是安全的。