C
Caroline
Hello,
I need to encrypt about 5 million records in a flat file. For a specific
reason, each record (32 bytes) must be encrypted by itself, (not all records
can be encrypted at once). Also I must use the Rinjndael encryption.
So I am running into memory problems, because I am creating a new
CryptoStream every time. I couldn't figure out how to encrypt using one
single CryptoStream, please help!
Could anyone help? This code is really simple, but it's hogging a lot of
memory. Please give me some advice.
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
int i = 0;
try
{
SqlConnection conn;
SqlCommand cmd;
SqlDataReader r;
ICryptoTransform ict;
StreamWriter swData;
FileStream fs, fsData;
System.Security.Cryptography.RijndaelManaged cryptObj = new
RijndaelManaged();
byte[] KEY_128 = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
byte[] IV_128 = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
MemoryStream ms = null;
CryptoStream cs = null;
StreamWriter sw = null;
fs = new FileStream("..\\..\\isbncrypt.txt", FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
ict = cryptObj.CreateEncryptor(KEY_128, IV_128);
string s = "";
int blockSizeEnc = 32;
byte[] buf = new byte[blockSizeEnc];
conn = new SqlConnection("server=homeserver;Integrated
Security=SSPI;database=NorthWind;");
conn.Open();
cmd = new SqlCommand("selectrecords_ssp", conn );
cmd.CommandType = CommandType.StoredProcedure;
r = cmd.ExecuteReader();
while ((r.Read()) && i < 5000000) // 5 million records
{
i++;
ms = new MemoryStream();
cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
sw = new StreamWriter(cs);
s = r.GetString(1).Substring(0,32);
sw.Write(s);
sw.Flush();
cs.FlushFinalBlock();
ms.Position = 0;
buf = new byte[blockSizeEnc];
ms.Read(buf, 0, blockSizeEnc);
fs.Write(buf, 0, blockSizeEnc);
if(conn.State != ConnectionState.Open)
conn.Open();
}
fs.Write(buf, 0, blockSizeEnc);
ms = null;
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "*** " + i);
}
I need to encrypt about 5 million records in a flat file. For a specific
reason, each record (32 bytes) must be encrypted by itself, (not all records
can be encrypted at once). Also I must use the Rinjndael encryption.
So I am running into memory problems, because I am creating a new
CryptoStream every time. I couldn't figure out how to encrypt using one
single CryptoStream, please help!
Could anyone help? This code is really simple, but it's hogging a lot of
memory. Please give me some advice.
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
int i = 0;
try
{
SqlConnection conn;
SqlCommand cmd;
SqlDataReader r;
ICryptoTransform ict;
StreamWriter swData;
FileStream fs, fsData;
System.Security.Cryptography.RijndaelManaged cryptObj = new
RijndaelManaged();
byte[] KEY_128 = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
byte[] IV_128 = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
MemoryStream ms = null;
CryptoStream cs = null;
StreamWriter sw = null;
fs = new FileStream("..\\..\\isbncrypt.txt", FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
ict = cryptObj.CreateEncryptor(KEY_128, IV_128);
string s = "";
int blockSizeEnc = 32;
byte[] buf = new byte[blockSizeEnc];
conn = new SqlConnection("server=homeserver;Integrated
Security=SSPI;database=NorthWind;");
conn.Open();
cmd = new SqlCommand("selectrecords_ssp", conn );
cmd.CommandType = CommandType.StoredProcedure;
r = cmd.ExecuteReader();
while ((r.Read()) && i < 5000000) // 5 million records
{
i++;
ms = new MemoryStream();
cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
sw = new StreamWriter(cs);
s = r.GetString(1).Substring(0,32);
sw.Write(s);
sw.Flush();
cs.FlushFinalBlock();
ms.Position = 0;
buf = new byte[blockSizeEnc];
ms.Read(buf, 0, blockSizeEnc);
fs.Write(buf, 0, blockSizeEnc);
if(conn.State != ConnectionState.Open)
conn.Open();
}
fs.Write(buf, 0, blockSizeEnc);
ms = null;
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "*** " + i);
}