c# - Is it possible to deserialize an encrypted file via DataContractSerializer? -


i'm serializing object via datacontractserializer without problems.

but if try serialize object encrypted file exception when deserializing.

here code:

        public static bool serializedatacontract<t>(stream filestream, t o, bool bcrypt = false)     {         datacontractserializer serializer = new datacontractserializer(typeof(t));         if(bcrypt)         {             tripledescryptoserviceprovider crypt = new tripledescryptoserviceprovider();             crypt.iv = crypt_init_vector;             crypt.key = crypt_key;             crypt.padding = paddingmode.zeros;              using(cryptostream cryptostream = new cryptostream(filestream, crypt.createencryptor(), cryptostreammode.write))             {                 serializer.writeobject(cryptostream, o);                 cryptostream.close();             }         }         else             serializer.writeobject(filestream, o);         return true;     }     public static bool deserializedatacontract<t>(stream filestream, out t o, bool bcrypt = false)     {         o = default(t);          try         {             datacontractserializer serializer = new datacontractserializer(typeof(t));             if(bcrypt)             {                 tripledescryptoserviceprovider crypt = new tripledescryptoserviceprovider();                 crypt.iv = crypt_init_vector;                 crypt.key = crypt_key;                 crypt.padding = paddingmode.zeros;                  using(cryptostream cryptostream = new cryptostream(filestream, crypt.createdecryptor(), cryptostreammode.read))                 {                     //tracexml(cryptostream);                      o = (t)serializer.readobject(cryptostream);                     cryptostream.close();                 }             }             else             {                 o = (t)serializer.readobject(filestream);             }         }         catch(exception ex)         {             return false;         }          return true;     } 

if call 2 functions bcrypt=false, works expected. if call functions bcrypt=true, exception when deserializing.

exception (tranlated german english): serializationexception: data @ root level invalid.

if trace data read after decryption, data seems ok me, looks serialization without encryption.

do know, bug in code?

or not possible use encryption datacontractserializer?

the problem encrypted data padded zeroes length of original data isn't obvious.

here's 1 way remove them deserializing works:

using(var cryptostream =        new cryptostream(filestream, crypt.createdecryptor(), cryptostreammode.read)) {                    using(var reader = new streamreader(cryptostream))     {         var s = reader.readtoend().trimend(new char[]{'\0'});          using(var stream = new memorystream(encoding.ascii.getbytes(s)))         {             o = (t)serializer.readobject(stream);         }     } } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -