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

本文关键字:解密 文件 加密 RSA 使用 | 更新日期: 2023-09-27 18:36:30

我正在尝试使用 RSA 和 C# 加密然后解密 XML 文件,虽然我真的很接近,但有一个问题。 一旦解密,几乎所有的文件都在那里,但最后有一个打嗝。 它要么是文件末尾的间隙,要么是将更多数据附加到文件末尾。

这是我的加密方法:

    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;
    }
}

这是我的解密方法:

    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;
    }

在这里用头撞墙,提前感谢

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

如果文件不是块大小长度的倍数,在加密过程中会发生什么情况?即。一个 500 字节长的文件将读取两组 245 字节,但它们还剩下 10 个字节?这可能是丢失最后的最后几个字节或添加额外的值?

也许您需要向文件添加一个标头,其大小以解密文件的字节为单位,以便解密器知道在哪里停止以及在加密过程中填充最终块的方法