windows runtime - C# DataReader issue when used with .AsInputStream() converted stream -
i need various reads on stream decryption process. (for winrt project) one, i'd readbyte() first byte of stream. there-after i'd read few bytes array, , read rest of bytes buffer.
i open datareader on passed iinputstream object. in turn created system.io.stream object using .asinputstream() method. when @ datareader object during debugging see unconsumedbufferlength 0, , unable readbyte() or readbytes() lest want "the operation attempted access data outside valid range" exception.
why datareader object seem empty? i've had issues asinputstream() method not returning actual iinputstream before. how can open datareader object on system.io.stream object.
code datareader assigned:
private stream decryptstream(iinputstream streamtodecrypt, byte[] key) { try { var datareader = new datareader(streamtodecrypt); int ivlength = datareader.readbyte(); //throws exception (unconsumedbufferlength = 0 remember) byte[] iv = new byte[ivlength]; datareader.readbytes(iv); //throws exception (unconsumedbufferlength = 0 remember) ibuffer todecryptbuffer = new windows.storage.streams.buffer(datareader.unconsumedbufferlength); todecryptbuffer = datareader.readbuffer(datareader.unconsumedbufferlength); //works, because todecryptbuffer of length 0. still useless.
code calls above method:
stream plainstream = decryptstream(streamtodecrypt.asinputstream(),key);
update: code created stream stream first created, system.io.compression.ziparchive object. passed interim decryption funciton "streamtodecrypt"
ziparchiveentry metaentry = archive.entries.where(x => x.fullname == "myfullnamehere").firstordefault(); stream returnstream = metaentry.open();
you need call datareader.loadasync(size) load file buffer before reading it.
change decrypt method this:
private async task<stream> decryptstream(iinputstream streamtodecrypt, byte[] key, int uncompressedsize) { try { var datareader = new datareader(streamtodecrypt); await datareader.loadasync(uncompressedsize); int ivlength = datareader.readbyte(); //throws exception (unconsumedbufferlength = 0 remember) byte[] iv = new byte[ivlength]; datareader.readbytes(iv); //throws exception (unconsumedbufferlength = 0 remember)
then when call it, pass in uncompressed size ziparchiveentry:
await decryptstream(streamtodecrypt.asinputstream(),key, streamtodecrypt.length);
Comments
Post a Comment