Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何加密大型XML文件_C#_Xml_Encryption - Fatal编程技术网

C# 如何加密大型XML文件

C# 如何加密大型XML文件,c#,xml,encryption,C#,Xml,Encryption,我在一台电脑上生成一个XMl文件(从数据库导出表),并将该文件发送到另一台电脑,然后从该XMl文件中导入用户数据, 出于安全原因,我需要加密此文件, 通常我使用这个函数 public static string Encrypt(string strText, string strEncrKey) { //Initialization Vector IV also must be 8 character long. byte[] IV = { 0x12,

我在一台电脑上生成一个XMl文件(从数据库导出表),并将该文件发送到另一台电脑,然后从该XMl文件中导入用户数据, 出于安全原因,我需要加密此文件, 通常我使用这个函数

 public static string Encrypt(string strText, string strEncrKey)
    {
        //Initialization Vector IV also must be 8 character long.
        byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        try
        {
            // Declare a UTF8Encoding object so we may use the GetByte
            // method to transform the plainText into a Byte array.
            byte[] bykey = System.Text.Encoding.UTF8.GetBytes(strEncrKey);
            byte[] InputByteArray = System.Text.Encoding.UTF8.GetBytes(strText);
            System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); // Create a new DES service provider
            // All cryptographic functions need a stream to output the
            // encrypted information. Here we declare a memory stream
            // for this purpose.
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(bykey, IV), System.Security.Cryptography.CryptoStreamMode.Write);
            // Write the encrypted information to the stream. Flush the information
            // when done to ensure everything is out of the buffer.
            cs.Write(InputByteArray, 0, InputByteArray.Length);
            cs.FlushFinalBlock();
            //Return Byte array into Base64 String Format
            return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception ex)
        {
            //Return ex.Message

            clsLogs.LogError(ex.Message + "|" + ex.TargetSite.ToString() + "|" + ex.StackTrace);
            return clsGlobleFunction.errorstring;
        }
    }
它的工作非常完美,但当文件大小很大时会产生问题, 例如,我的Xml文件显示了以下数据

<NewDataSet>
  <Table>
    <Batch_M_id>-1</Batch_M_id>
    <RSN>000061483</RSN>
    <Parent_RSN />
    <Pkg_Location>1</Pkg_Location>
    <CompanyId>1</CompanyId>
  </Table>
 <Table>
   <Batch_M_id>-1</Batch_M_id>
   <RSN>000062321</RSN>
   <Parent_RSN />
   <Pkg_Location>1</Pkg_Location>
   <CompanyId>1</CompanyId>
</Table>
</NewDataSet> 

-1
000061483
1.
1.
-1
000062321
1.
1.
我需要导出4lacs RSN编号,根据上面的示例标记将重复4lacs时间,
您能告诉我哪种类型的加密更适合此性能吗?

一般来说,XML是臃肿的。是故意的。设计考虑是,膨胀可以作为可读性的折衷,因为膨胀可以很容易地打包。因此,如果您想在某处传输XML文件,请打包它。NET有Zip类,任何其他算法也可能很好。一旦你的文件是它当前大小的一小部分,任何其他操作都会容易得多

如果文件大小有问题,请不要对生成的字节进行编码。你有一个字节流。将其写入文件。不要先将其转换为文本