C
Caroline
Hello,
I need to encrypt millions of records (32 bytes each single record), and
would like to reuse the CryptoStream object without recreating it every time
inside the loop. Can anybody point to why I am getting this error on the
second loop iteration:
FlushFinalBlock() method was called twice on a CryptoStream. This method can
only be called once.
Here is the sample code:
**
using System.Security.Cryptography;
using System.IO;
**
private void button2_Click(object sender, System.EventArgs e)
{
int i = 0;
try
{
ICryptoTransform ict;
FileStream fs;
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 = "";
byte[] buf = new byte[32];
ms = new MemoryStream();
cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
sw = new StreamWriter(cs);
while ((i < 3)) // millions of records
{
i++;
s = "12345678901234567890123456789012";
sw.Write(s);
sw.Flush();
cs.FlushFinalBlock(); // ERROR ON SECOND ITERATION: ???
//FlushFinalBlock() method was called twice on a CryptoStream.
This method can only be called once.
ms.Position = 0;
ms.Read(buf, 0, 32);
fs.Write(buf, 0, 32);
}
fs.Write(buf, 0, 32);
ms = null;
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "*** " + i);
}
}
I need to encrypt millions of records (32 bytes each single record), and
would like to reuse the CryptoStream object without recreating it every time
inside the loop. Can anybody point to why I am getting this error on the
second loop iteration:
FlushFinalBlock() method was called twice on a CryptoStream. This method can
only be called once.
Here is the sample code:
**
using System.Security.Cryptography;
using System.IO;
**
private void button2_Click(object sender, System.EventArgs e)
{
int i = 0;
try
{
ICryptoTransform ict;
FileStream fs;
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 = "";
byte[] buf = new byte[32];
ms = new MemoryStream();
cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
sw = new StreamWriter(cs);
while ((i < 3)) // millions of records
{
i++;
s = "12345678901234567890123456789012";
sw.Write(s);
sw.Flush();
cs.FlushFinalBlock(); // ERROR ON SECOND ITERATION: ???
//FlushFinalBlock() method was called twice on a CryptoStream.
This method can only be called once.
ms.Position = 0;
ms.Read(buf, 0, 32);
fs.Write(buf, 0, 32);
}
fs.Write(buf, 0, 32);
ms = null;
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "*** " + i);
}
}