title: How to parse encrypted data in function dataRecieved in twisted?
body: <p>In my project I inherited twisted protocol <code>from twisted.internet.protocol import Protocol</code>, As we know, I should do something packet manipulation in the function <code>dataRecieved</code>, However, the <a href="http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.protocol.Protocol.html#dataReceived" rel="nofollow">doc</a> said: </p>
  
  <blockquote>
    <p>data: a string of indeterminate length. Please keep in mind that you will probably need to buffer some data, as partial (or multiple) protocol messages may be received! I recommend that unit tests for protocols call through to this method with differing chunk sizes, down to one byte at a time.</p>
  </blockquote>
  
  <p>So my data format is that:
  <code>
  packet len | payload
  2 bytes    | variable bytes
  </code></p>
  
  <p>But things become complected if I want to encrypt my data, How should I do then?</p>
  
  <p>Should I encrypt both packet length and payload? then how to judge if the packet is end?</p>
  
  <p>Or should I encrypt only the payload, then alter the packet length ? what if the encrypted payload length is larger than the max value of 2bytes?</p>
  
  <p>Plus: If I use raw socket instead of twisted, can I omit the 2 bytes packet len prefix?</p>
  
  <p>Thanks!</p>