c# - Read a special stream without knowing the number of bytes -


before go , use jon skeet's answer, can't. using stream great in beginning, now, have run small problem. @ point not know amount of data expect, thought i'd use method jon skeet provided unfortunately waits on packet = mpackets.take() when packets have been read.

i have no idea how fix this, stream it's supposed (in beginning) unfortunately occasion not want made for...

should try rewrite waitingstream or there different readall method work better in case?

i have tried returning 0 when there no more packets available(when wanted read , not wait) throws io end of stream exception.

i @ loss here, understand i've dug deep hole self...

so far have not been able come better solution subtracting tls header , gcm iv , mac total length (like mentioned below) works fine 1 suite i'm using not boast future cipher suites.


some context why i'm doing this:
i'm using https://github.com/bcgit/bc-csharp create tls based encryption, because of limitations cannot assign tcp stream, string tcp layer, cannot provide propper tcp stream.

i using mockpsktlsserver , client , i've created so:

waitingstream mstdin = new waitingstream(); waitingstream mstdout = new waitingstream(); cryptopsktlsclient mserver = new cryptopsktlsclient(null); securerandom securerandom = new securerandom(); tlsclientprotocol tlsprotocol = new tlsserverprotocol(stdin, stdout, securerandom); tlsprotocol.connect(mserver); 

this works fine, i've added event waiting stream fires when there's output stream. multiple handshake messages when created.

this works fine, have been made better (less.... hacked?) it's supposed do.

i know of ways work hack , i'm looking elegant way it. here code example 1 of hacks:

public override string decryptdata(string ciphertext) {     byte[] ciphertextbuff = asciiencoding.default.getbytes(ciphertext);     mstdin.write(ciphertextbuff, 0, ciphertextbuff.length);      stream tlsstream = mserverprotocol.stream;     byte[] plaintextbuffer = new byte[ciphertextbuff.length - 29];//filthy hack 29 bytes bij aes-gcm want tls packet = 5, gcm iv = 8, gcm-mac = 16 totaal = 29.     streams.readfully(tlsstream, plaintextbuffer);      string plaintext = asciiencoding.default.getstring(plaintextbuffer);     return plaintext; } 

or prepending plaintext lenght in x reserved bytes tls packet , retrieving these bytes before decrypting.

but can see code example buffer in read method must length of bytes want extract, can less stream has cannot more because wait indefinitely.

when i'm talking methods readall , readfully i mean these methods

all string tcp layer

this surely fatal flaw in attempts make work. implicit in trying data sent across tcp connection tls encrypted. produces highly random byte values, value between 0 , 255 possible. parcel of data you'd receive socket's read() call byte[], not string.

converting byte[] string requires using .net encoding class. there many varieties of it, stock ones asciiencoding, unicodeencoding, utf8encoding. , bunch of custom ones designed work specific code page, them using encoding(int codepage) constructor.

we don't know encoding used "tcp layer" need work with, utf8 likely, given produce corrupted data when asked convert byte[] encrypted content. byte values don't have corresponding unicode codepoint. encoding object punts kind of problem generating ? or substitute character. whatever value of encoding.encoderfallback happens be. odds can see them in debugger, albeit frequency unpredictable.

in other words, encrypted data inevitably corrupted these substitutions. there no workaround this, nothing can recover original byte[] since content lost substitutions. must tackled lower layers, either need encode byte[] content string can convert (base64 standard solution) or needs stop converting data string. need raw byte[] make work.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -