[英]How to Encrypt Large XML file
我正在一個PC中生成一個XMl文件(從數據庫導出表),然后將該文件發送到另一個PC,並且比該用戶從該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>
我需要導出4lacs RSN編號,按照上面的示例表格標記將重復4lacs時間,請您告訴我哪種加密對此性能更好
一般來說,XML是is腫的。 通過設計。 設計上的考慮是,膨脹可以作為可讀性的折衷,因為膨脹可以很容易地打包。 因此,如果您想將XML文件傳輸到某處,請將其打包。 .NET具有Zip類,其他任何算法也可能做得很好。 一旦文件僅是當前大小的一小部分,任何其他操作都將變得更加容易。
如果文件大小有問題,請不要對結果字節進行編碼。 您有一個字節流。 將其寫入文件。 不要先將其轉換為文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.