vb.net - Serialization Cryptography Error -


i having trouble using encryption serialization when deserializing object.

this error:

failed deserialize. reason: end of stream encountered before parsing completed

here code:

imports system.io imports system.security.cryptography imports system.runtime.serialization imports system.runtime.serialization.formatters.binary imports system.text  module testmodencryption  public sub saveencryptedobjecttofile(filename string, item object)     dim fs filestream     dim encryptor cryptostream      dim formatter new binaryformatter      dim password string = "mypassword"     dim salt string = "initialvector123"      dim aes aesmanaged = new aesmanaged     aes.padding = paddingmode.none     aes.mode = ciphermode.cbc      dim hashalgorithm string = "sha1" 'can sha1 or md5     dim passworditerations integer = 2     dim initialvector string = "initialvector123" 'this should string of 16 ascii characters.     dim keysize integer = 256 'can 128, 192, or 256.      dim initialvectorbytes byte() = encoding.ascii.getbytes(initialvector)     dim saltvaluebytes byte() = encoding.ascii.getbytes(salt)     dim derivedpassword new rfc2898derivebytes(password, saltvaluebytes, passworditerations)     dim keybytes byte() = derivedpassword.getbytes(cint(keysize / 8))      dim encrypttransf icryptotransform = aes.createencryptor(keybytes, initialvectorbytes)      fs = new filestream(filename, filemode.create)     encryptor = new cryptostream(fs, encrypttransf, cryptostreammode.write)      try         formatter.serialize(encryptor, item)     catch e serializationexception         console.writeline("failed serialize. reason: " & e.message)         throw             fs.close()     end try end sub  public function openencryptedobjectfromfile(filename string) object     dim fs new filestream(filename, filemode.open)     dim decryptor cryptostream      dim itemtoreturn new object      dim password string = "mypassword"     dim salt string = "initialvector123"      dim aes aesmanaged = new aesmanaged     aes.padding = paddingmode.none     aes.mode = ciphermode.cbc      dim hashalgorithm string = "sha1" 'can sha1 or md5     dim passworditerations integer = 2     dim initialvector string = "initialvector123" 'this should string of 16 ascii characters.     dim keysize integer = 256 'can 128, 192, or 256.      dim initialvectorbytes byte() = encoding.ascii.getbytes(initialvector)     dim saltvaluebytes byte() = encoding.ascii.getbytes(salt)     dim derivedpassword new rfc2898derivebytes(password, saltvaluebytes, passworditerations)     dim keybytes byte() = derivedpassword.getbytes(cint(keysize / 8))      dim decrypttrans icryptotransform = aes.createdecryptor(keybytes, initialvectorbytes)      try         dim formatter new binaryformatter          decryptor = new cryptostream(fs, decrypttrans, cryptostreammode.read)         itemtoreturn = directcast(formatter.deserialize(decryptor), object)         return itemtoreturn     catch e serializationexception         msgbox("failed deserialize. reason: " & e.message)         return nothing         'throw             fs.close()     end try end function  end module 

crypto complex. first crypto working, crypto. start piece of text: "i wandered lonely armadillo." use code encrypt , decrypt text, forgetting serialization. when working correctly then, , then, use working crypto code encrypt/decrypt serialized object.

have serialized/deserialized object without encryption?

on brief glance, need set padding pkcs#7 (aka pkcs#5). paddingmode.none may causing problem. without padding final block may not being processed correctly. need use same padding both encryption , decryption.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -