![]() ![]() I just have to pass the appropriate Read or Write enumeration member to the CryptoStream's constructor. It doesn't really matter which stream I attach it to. When I read, I'm attaching it to the input (reading) stream, so I use the read method on the CryptoStream. Byte fileBytes File.ReadAllBytes ('TestData\example.pdf') var content Convert.ToBase64String (fileBytes) Share Improve this answer Follow edited Feb 7 at 1:15 Michael Freidgeim 25. In the Encode method,I am attaching it to the output (writing) stream, so when I instance the CryptoStream, I use its Write() method. Use File.ReadAllBytes to load the PDF file, and then encode the byte array as normal using Convert.ToBase64String (bytes). I am also playing around with where I attach the CryptoStream. System.IO.File.WriteAllText(outFileName, 64String(bytes)) Public void MemoryEncode(string inFileName, string outFileName)īyte bytes = System.IO.File.ReadAllBytes(inFileName) will use way less memory on a large file than this version. the output should be identical, but the crytostream version you can compare the output of my Encode method above to the output of this one this version of Encode pulls everything into memory at once While ((bytesRead = cryptStream.Read(buffer, 0, buffer.Length)) > 0) Using ( cryptStream = new (inFile, transform, .Read)) Using (System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName), Public void Decode(string inFileName, string outFileName) While ((bytesRead = inFile.Read(buffer, 0, buffer.Length)) > 0) I'm going to use a 4k buffer, tune this as needed Using ( cryptStream = new (outFile, transform, .Write)) OutFile = System.IO.File.Create(outFileName)) Using(System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName), Public void Encode(string inFileName, string outFileName) I included a (maybe poorly named) implementation of Andrew's suggestion so that you can compare the output. net includes ToBase64 and FromBase64 transformations as well, so we won't be encrypting, just encoding. Normally, the transformation is encryption/decryption, but. You plug a class called CryptoTransform into the CryptoStream (which in turn is attached to your file/memory/network stream) and it performs data transformations on the data while it's being read from or written to the stream. This is kind of an adapter that plugs into another stream. Here's how it works: You fire up a class called a CryptoStream. I have tested the example code below, and I get identical output using either my method or Andrew's method above. Net includes an encoder that can do the chunking, but it's in kind of a weird place. There is a way that you can do this in chunks so that you don't have to burn a ton of memory all at once. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |