visual studio - What is the opposite to this encryption+compression code in C#? -


i have written code encrypts , compresses data, have hard time writing code in reverse.

i various errors no matter how assemble code.

my question basic:

in order should different bits of code used?

here sample code encrypting , compressing:

memorystream ms = new memorystream(); cryptostream crypts = new cryptostream(ms, des.createencryptor(), cryptostreammode.write)); deflatestream defs = new deflatestream(crypts, compressionmode.compress) binarywriter bw = new binarywriter(defs))  //bw.write("write string example");  bw.close(); 

now, in decryption routine, should cryptostream used after deflatestream trace encryption routine backwards? or should deflatestream used after cryptostream?

like following example:

memorystream ms = new memorystream(); deflatestream defs = new deflatestream(ms, compressionmode.compress) cryptostream crypts = new cryptostream(defs, des.createencryptor(), cryptostreammode.write)); binarywriter bw = new binarywriter(crypts))  //bw.write("write string example");  bw.close(); 

the errors getting vary unknown block type. stream might corrupted. bad data.

edit:

here encryption , decryption routines. error unknown block type. stream might corrupted.

encryption routine:

using (memorystream ms = new memorystream())             {                 using (cryptostream crypts = new cryptostream(ms, des.createencryptor(), cryptostreammode.write))                 {                     using (deflatestream defs = new deflatestream(crypts, compressionmode.compress))                     {                         using (binarywriter bw = new binarywriter(defs))                         {                             datetime dt = new datetime().now;                             bw.write(dt.ticks);                         }                     }                 }             } 

decryption routine (assume ms populated encrypted data):

using (memorystream ms = new memorystream())             {                 using (deflatestream defs = new deflatestream(ms, compressionmode.decompress))                 {                     using (cryptostream crypts = new cryptostream(defs, des.createdecryptor(), cryptostreammode.read))                     {                         using (binaryreader br = new binaryreader(crypts))                         {                             datetime dt = new datetime((long)br.readint64());                         }                     }                 }             } 

i error failed construct huffman tree using length array. stream might corrupted.

what might error?

the data compressed, encrypted, first thing need decrypt it, can decompress it. so, yes - reversed... isn't obvious data flow reversed, order chain streams remains same.

it helpful if wrap stream usages in using blocks ensure flushed , closed correctly.

this code should work:

using (var des = new aescryptoserviceprovider()) {     byte[] result;      using (var ms = new memorystream())     {         using (var encryptor = des.createencryptor())         using (var crypts = new cryptostream(ms, encryptor, cryptostreammode.write))         using (var defs = new deflatestream(crypts, compressionmode.compress))         using (var bw = new binarywriter(defs))         {             bw.write("hello, world.");         }          result = ms.toarray();     }                  using (var ms = new memorystream(result))                 using (var decryptor = des.createdecryptor())     using (var crypts2 = new cryptostream(ms, decryptor, cryptostreammode.read))     using (var defs = new deflatestream(crypts2, compressionmode.decompress))     using (var br = new binaryreader(defs))     {         var x = br.readstring();     } } 

see this fiddle demo.


Comments

Popular posts from this blog

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

matlab - error with cyclic autocorrelation function -

php - Using grpc in Laravel, "Class 'Grpc\ChannelCredentials' not found." -