title: Using Protocol Buffer to Serialize Bytes Python3
body: <p>I am trying to serialize a bytes object - which is an initialization vector for my program's encryption. But, the Google Protocol Buffer only accepts strings. It seems like the error starts with casting bytes to string. Am I using the correct method to do this? Thank you for any help or guidance!</p>
  
  <p>Or also, can I make the Initialization Vector a string object for AES-CBC mode encryption?</p>
  
  <p><strong>Code</strong></p>
  
  <ul>
  <li>Cast the bytes to a string
  
  <ul>
  <li><code>string_iv = str(bytes_iv, 'utf-8')</code></li>
  </ul></li>
  <li>Serialize the string using SerializeToString():
  
  <ul>
  <li><code>serialized_iv = IV.SerializeToString()</code></li>
  </ul></li>
  <li>Use ParseToString() to recover the string:
  
  <ul>
  <li><code>IV.ParseFromString( serialized_iv )</code></li>
  </ul></li>
  <li>And finally, UTF-8 encode the string back to bytes: 
  
  <ul>
  <li><code>bytes_iv = bytes(IV.string_iv, encoding= 'utf-8')</code></li>
  </ul></li>
  </ul>
  
  <p><strong>Error</strong></p>
  
  <p>string_iv = str(bytes_iv, 'utf-8')
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9b in position 3: invalid start byte</p>