简体   繁体   English

使用C#和RSA加密/解密文件

[英]Encrypting/Decrypting a file using C# and RSA

I am trying to encrypt and then decrypt an XML file using RSA and C# and while I'm really close, there's a problem. 我正在尝试使用RSA和C#加密然后解密XML文件,而实际上我确实很接近,但是有一个问题。 Once it's decrypted, almost all of the file is there but there's a hiccup toward the end. 解密后,几乎所有文件都在那里,但是最后会有一点麻烦。 It's either a gap toward the end of the file or more data is appended to the very end of the file. 这要么是文件末尾的空白,要么是更多数据附加到文件末尾。

Here is my encrypt method: 这是我的加密方法:

    public static bool Encrypt(ProcessingHolder ph)
    {
        FileInfo inFile = ph.encryptedFI;
        FileInfo outFile = ph.unEncryptedFI;

        X509Certificate2 daCert = new X509Certificate2(keyFP, daCertPassword);
        RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)daCert.PrivateKey;

        bool done = false;
        FileStream fs = null;
        FileStream fso = null;

        try
        {
            //opens the file to encrypt into a filestream object
            fs = inFile.OpenRead();

            //240 is what the iOS side is using
            //algorithm that calculates max bytes ((KeySize - 384) / 8) + 37 
            //(returns 245)
            int chunkSize = 245;

            fso = outFile.OpenWrite();
            byte[] buffer = new byte[chunkSize];
            int totalRead = 0;


            while (totalRead < fs.Length)
            {
                int readBytes = fs.Read(buffer,0, chunkSize);

                totalRead += readBytes;

                //check to see if the final chunk of data is less than 245 so as not to write empty buffer
                if (readBytes < chunkSize) buffer = new byte[readBytes];
                //byte[] encr = new byte[readBytes];

                //actual encryption
                //encr = RSA.Encrypt(buffer, false);

                byte[] encr = RSA.Encrypt(buffer, false);
                fso.Write(encr, 0, encr.Length);
            }
            fso.Flush();
            fso.Close();
            fs.Close();
            done = true;
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Decrypt failed with message " + ex.Message);
            done = false;
        }
        finally
        {
            if (fs != null) fs.Close();
            if (fso != null) fso.Close();
        }
        return done;
    }
}

and here is my decrypt method: 这是我的解密方法:

    public static bool Decrypt(ProcessingHolder ph)
    {
        FileInfo inFile = ph.encryptedFI;
        FileInfo outFile = ph.unEncryptedFI;

        X509Certificate2 daCert = new X509Certificate2(keyFP, daCertPassword);
        RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)daCert.PrivateKey;

        bool done = false;
        FileStream fs = null;
        FileStream fso = null;

        try
        {
            fs = inFile.OpenRead();
            int chunkSize = 256;

            fso = outFile.OpenWrite();
            byte[] buffer = new byte[chunkSize];
            int totalRead = 0;

            while (totalRead < fs.Length)
            {
                int readBytes = fs.Read(buffer, 0, chunkSize);
                totalRead += readBytes;

                //check to see if the final chunk of data is less than 245 so as not to write empty buffer
                //if (readBytes < chunkSize) buffer = new byte[readBytes];

                byte[] decr = RSA.Decrypt(buffer, false);
                fso.Write(decr, 0, decr.Length);
            }
            fso.Flush();
            fso.Close();
            fs.Close();
            done = true;
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Decrypt failed with message " + ex.Message);
            done = false;
        }
        finally
        {
            if (fs != null) fs.Close();
            if (fso != null) fso.Close();
        }
        return done;
    }

banging my head against the wall here, thanks in advance 在这里,我的头撞在墙上,谢谢

What happens during encrypting if the file is not a multiple of the length of the chunk size? 如果文件不是块大小长度的倍数,在加密过程中会发生什么? Ie. 就是 a file 500 bytes long would read two sets of 245, but they have 10 bytes left over? 一个500字节长的文件将读取两组245,但是它们还有10字节剩余? This might be loosing the last few bytes at the end or adding extra values? 这可能是丢失最后的最后几个字节还是添加了额外的值?

Maybe you need to add a header to the file with the size in bytes of the decrypted file so that the decrypter knows where to stop and a way to pad out the final block during encryption 也许您需要向文件添加标头,其大小为解密文件的字节数,以便解密器知道在加密期间在哪里停止以及填充最终块的方法

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

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