如何加密大型XML文件

时间:2014-06-12 06:50:00

标签: c# xml encryption

我在One Pc中生成一个XMl文件(从数据库导出表),并将该文件发送到另一个Pc,而不是来自该xml文件的用户Importdata, 出于安全原因,我需要加密此文件, 一般我都在使用这个功能,

 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> 

我需要导出4lacs RSN号码,如上例标签将重复4lacs时间, 能告诉我哪种类型的加密对这种性能更好吗

1 个答案:

答案 0 :(得分:1)

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

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

相关问题