FlushFinalBlock method was called twice on a CryptoStream ERROR MESSAGE

  • Thread starter Thread starter Caroline
  • Start date Start date
Yes. I will post it later. There is some extra processing involved, so I
have to change it a bit so u can see it. Thanks for all your help.
 
Your welcome Caronline. You just need to verify padding used. I don't know
what padding is used on compact fx. So laptop needs to use the same as you
know. Then you need to verify what the encrypted size of 32 bytes makes
using that padding - and this is your fixed record size. Also need to
verify that all combination of 32 bytes using that padding produce the same
fixed size encrypted array len, as (I ~think) some paddings can produce
different results.

--
William Stacey [MVP]

Caroline said:
Thanks. Will try your code.

William Stacey said:
Using the class I posted in last post, I created 1 million in 7.3 secs on
my laptop and could find and decrypt record 500,000 in 1.5 milliseconds.

string path = @"\isbncrypt.txt";
byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6 };
byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6 };
File.Delete(path);
MyRecords mr = new MyRecords(path, key, iv);
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] rec = new byte[32];
Stopwatch sw = new Stopwatch();
sw.Start();
for ( int i = 0; i < 1000000; i++ )
{
rec[0] = (byte)i;
mr.Add(rec);
}
sw.Stop();
Console.WriteLine("Time to create records:" +
sw.Elapsed.TotalSeconds.ToString());

sw.Start();
rec = mr.FindIndex(50000);
sw.Stop();
Console.WriteLine("Rec {0}:{1}", 500000,
BitConverter.ToString(rec));
Console.WriteLine("Time to find:" +
sw.Elapsed.TotalSeconds.ToString());

Time to create records:7.3629251
Rec
500000:20-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
ms to find:1.5317
ticks to find:15317


--
William Stacey [MVP]

Caroline said:
Jon is getting the point.

I will access each encrypted record via a binary search on the pocket
pc.
I have tried this and have no problems with it, it's quite fast.

But the problem I am having is creating the encrypted strings using
CryptoStream on my laptop (or desktop). Last night I tried using the
close clause, it's faster, but still took about 18 hours to run. It
goes fast in the beginning, but than after the initial 8 mb it runs
slower and slower. The generated file is 183 MB total. I am going to try
using a memory profiling tool.





I think you're missing the aim - if I need to get to the last
record, I
have to decrypt everything up until that point with your scheme.

Most likely I am missing it. I am not getting getting the full story
from
Caroline so I try to read between the lines here. On one hand, she
says see
needs to seek, but then talks like she will enumerate all records
anyway.
Caroline, how will you access the records 1-1,000,000 or some random
order?

I haven't seen where it looks like she's going to be enumerating all
the records, other than in the sample code - which I just took to be
sample code which demonstrated the problem.

Note that this is going to be read on the compact framework by the way,
so even if reading 2 million records only took 8.5 seconds on your
laptop, reading that many on a mobile device could take a *lot* longer.
 
William,

Thanks for the sample code. Groovy! It only took a few minutes for me,
including my processing.
I only have 1 question:
I noticed you did not use the StreamWriter, so that probably was the reason
mine was so slow. With StreamWriter, I was getting 32 bytes output for a 32
bytes input. Without it, I am getting 48 bytes output (because of padding?).
But if I use 31 bytes input, I get 32 bytes output, which is fine. However I
was just wondering how come I don't get 32 bytes output with 32 bytes input?

Thank you very much!

William Stacey said:
Using the class I posted in last post, I created 1 million in 7.3 secs on
my laptop and could find and decrypt record 500,000 in 1.5 milliseconds.

string path = @"\isbncrypt.txt";
byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6 };
byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6 };
File.Delete(path);
MyRecords mr = new MyRecords(path, key, iv);
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] rec = new byte[32];
Stopwatch sw = new Stopwatch();
sw.Start();
for ( int i = 0; i < 1000000; i++ )
{
rec[0] = (byte)i;
mr.Add(rec);
}
sw.Stop();
Console.WriteLine("Time to create records:" +
sw.Elapsed.TotalSeconds.ToString());

sw.Start();
rec = mr.FindIndex(50000);
sw.Stop();
Console.WriteLine("Rec {0}:{1}", 500000,
BitConverter.ToString(rec));
Console.WriteLine("Time to find:" +
sw.Elapsed.TotalSeconds.ToString());

Time to create records:7.3629251
Rec
500000:20-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
ms to find:1.5317
ticks to find:15317
 
Please show post the code your using. Most likely an encoding thing. I
would just encoding yourself and keep things in bytes using the Encoding
class.

--
William Stacey [MVP]

Caroline said:
William,

Thanks for the sample code. Groovy! It only took a few minutes for me,
including my processing.
I only have 1 question:
I noticed you did not use the StreamWriter, so that probably was the
reason mine was so slow. With StreamWriter, I was getting 32 bytes output
for a 32 bytes input. Without it, I am getting 48 bytes output (because of
padding?). But if I use 31 bytes input, I get 32 bytes output, which is
fine. However I was just wondering how come I don't get 32 bytes output
with 32 bytes input?

Thank you very much!

William Stacey said:
Using the class I posted in last post, I created 1 million in 7.3 secs on
my laptop and could find and decrypt record 500,000 in 1.5 milliseconds.

string path = @"\isbncrypt.txt";
byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6 };
byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6 };
File.Delete(path);
MyRecords mr = new MyRecords(path, key, iv);
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] rec = new byte[32];
Stopwatch sw = new Stopwatch();
sw.Start();
for ( int i = 0; i < 1000000; i++ )
{
rec[0] = (byte)i;
mr.Add(rec);
}
sw.Stop();
Console.WriteLine("Time to create records:" +
sw.Elapsed.TotalSeconds.ToString());

sw.Start();
rec = mr.FindIndex(50000);
sw.Stop();
Console.WriteLine("Rec {0}:{1}", 500000,
BitConverter.ToString(rec));
Console.WriteLine("Time to find:" +
sw.Elapsed.TotalSeconds.ToString());

Time to create records:7.3629251
Rec
500000:20-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
ms to find:1.5317
ticks to find:15317


--
William Stacey [MVP]

Caroline said:
Jon is getting the point.

I will access each encrypted record via a binary search on the pocket
pc.
I have tried this and have no problems with it, it's quite fast.

But the problem I am having is creating the encrypted strings using
CryptoStream on my laptop (or desktop). Last night I tried using the
close clause, it's faster, but still took about 18 hours to run. It
goes fast in the beginning, but than after the initial 8 mb it runs
slower and slower. The generated file is 183 MB total. I am going to try
using a memory profiling tool.





I think you're missing the aim - if I need to get to the last
record, I
have to decrypt everything up until that point with your scheme.

Most likely I am missing it. I am not getting getting the full story
from
Caroline so I try to read between the lines here. On one hand, she
says see
needs to seek, but then talks like she will enumerate all records
anyway.
Caroline, how will you access the records 1-1,000,000 or some random
order?

I haven't seen where it looks like she's going to be enumerating all
the records, other than in the sample code - which I just took to be
sample code which demonstrated the problem.

Note that this is going to be read on the compact framework by the way,
so even if reading 2 million records only took 8.5 seconds on your
laptop, reading that many on a mobile device could take a *lot* longer.
 
It's ok. No big deal. Thank you very much for your help, both you and Jon!

William Stacey said:
Please show post the code your using. Most likely an encoding thing. I
would just encoding yourself and keep things in bytes using the Encoding
class.

--
William Stacey [MVP]

Caroline said:
William,

Thanks for the sample code. Groovy! It only took a few minutes for me,
including my processing.
I only have 1 question:
I noticed you did not use the StreamWriter, so that probably was the
reason mine was so slow. With StreamWriter, I was getting 32 bytes output
for a 32 bytes input. Without it, I am getting 48 bytes output (because
of padding?). But if I use 31 bytes input, I get 32 bytes output, which
is fine. However I was just wondering how come I don't get 32 bytes
output with 32 bytes input?

Thank you very much!

William Stacey said:
Using the class I posted in last post, I created 1 million in 7.3 secs
on my laptop and could find and decrypt record 500,000 in 1.5
milliseconds.

string path = @"\isbncrypt.txt";
byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6 };
byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6 };
File.Delete(path);
MyRecords mr = new MyRecords(path, key, iv);
RNGCryptoServiceProvider rng = new
RNGCryptoServiceProvider();
byte[] rec = new byte[32];
Stopwatch sw = new Stopwatch();
sw.Start();
for ( int i = 0; i < 1000000; i++ )
{
rec[0] = (byte)i;
mr.Add(rec);
}
sw.Stop();
Console.WriteLine("Time to create records:" +
sw.Elapsed.TotalSeconds.ToString());

sw.Start();
rec = mr.FindIndex(50000);
sw.Stop();
Console.WriteLine("Rec {0}:{1}", 500000,
BitConverter.ToString(rec));
Console.WriteLine("Time to find:" +
sw.Elapsed.TotalSeconds.ToString());

Time to create records:7.3629251
Rec
500000:20-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
ms to find:1.5317
ticks to find:15317


--
William Stacey [MVP]

Jon is getting the point.

I will access each encrypted record via a binary search on the pocket
pc.
I have tried this and have no problems with it, it's quite fast.

But the problem I am having is creating the encrypted strings using
CryptoStream on my laptop (or desktop). Last night I tried using the
close clause, it's faster, but still took about 18 hours to run. It
goes fast in the beginning, but than after the initial 8 mb it runs
slower and slower. The generated file is 183 MB total. I am going to
try using a memory profiling tool.





I think you're missing the aim - if I need to get to the last
record, I
have to decrypt everything up until that point with your scheme.

Most likely I am missing it. I am not getting getting the full story
from
Caroline so I try to read between the lines here. On one hand, she
says see
needs to seek, but then talks like she will enumerate all records
anyway.
Caroline, how will you access the records 1-1,000,000 or some random
order?

I haven't seen where it looks like she's going to be enumerating all
the records, other than in the sample code - which I just took to be
sample code which demonstrated the problem.

Note that this is going to be read on the compact framework by the
way,
so even if reading 2 million records only took 8.5 seconds on your
laptop, reading that many on a mobile device could take a *lot*
longer.
 
Back
Top