c# - Decode base64 filestream with FromBase64Transform -


the example msdn seems read whole file memory. don't want that. file should processed block block. therefore tried rewrite example:

using (frombase64transform mytransform = new frombase64transform (frombase64transformmode.ignorewhitespaces)) {     byte[] transformbuffer = new byte[mytransform.outputblocksize];      using (filestream inputfile = file.openread("/path/to/file/47311.b64")) {         using(filestream outputfile = file.openwrite("/path/to/file/47311.jpg")){             int bytesread;             byte[] inputbuffer = new byte[4096];             while ((bytesread = inputfile.read (inputbuffer, 0, 4096)) > 0) {                 int byteswritten = mytransform.transformblock (inputbuffer, 0, 4, transformbuffer, 0);                 outputfile.write (transformbuffer, 0, byteswritten);             }              mytransform.clear ();         }     } } 

but image can't opened. i'm doing wrong?

i believe line bug:

int byteswritten = mytransform.transformblock (inputbuffer, 0, 4, transformbuffer, 0); 

you're transforming 4 bytes, regardless of how many bytes you've read. suspect want:

int byteswritten = mytransform.transformblock (inputbuffer, 0, bytesread, transformbuffer, 0); 

you may need change size of transformbuffer though - if you're reading 4k of base64 data per iteration, need 3k plaintext data per iteration.

a simpler option, however, create cryptostream using transform, reading input stream, , using stream.copyto write filestream.


Comments