title: Python Cryptography, Value Error: Could not deserialize key data, when using EC keys
body: <p>The goal i am trying to achieve with this code is generating elliptic curve keys and storing them in a file to load at a later time.  </p>
  
  <p>I followed the instruction in the serialization and key loading section o the f elliptic curve section in the <a href="https://cryptography.io/en/latest/hazmat/primitives/asymmetric/ec/#module-cryptography.hazmat.primitives.asymmetric.ec" rel="nofollow noreferrer">documentation</a>. The only difference is the additional step of writing the bytes generated by the the    <code>private_bytes</code> function of the to a file and later loading the bytes again from the same file.<br>
  I even compared the bytes loaded from file to the initially created bytes and they are shown as being equal.  </p>
  
  <p></p>
  
  <pre><code>from cryptography.hazmat.primitives.asymmetric import padding, ec
  from cryptography.hazmat.backends import default_backend
  from cryptography.hazmat.primitives import serialization
  
  
  
  if __name__ == '__main__':
  
      private_key = ec.generate_private_key(
          ec.SECP384R1(), default_backend()
      )
      private_key_pem = private_key.private_bytes(encoding=serialization.Encoding.PEM,
                                              format=serialization.PrivateFormat.PKCS8,
                                              encryption_algorithm=serialization.NoEncryption()
                                              )
  
      print("Loading private key from bytes")
      private_key = serialization.load_pem_private_key(private_key_pem, password=None, backend=default_backend())
      print("Successfully loaded key from bytes")
      with open("ec_private_key", "wb") as sk:
          sk.write(private_key_pem)
  
      with open("ec_private_key", "rb") as sk:
          print(sk.read() == private_key_pem)
          private_key = serialization.load_pem_private_key(sk.read(), password=None, backend=default_backend())
  </code></pre>
  
  <p>This is the output I receive when running the above script:</p>
  
  <pre><code>Loading private key from bytes
  Successfully loaded key from bytes
  True
  Traceback (most recent call last):
    File "/home/michaelgraf/Desktop/train-user-client/backend/test.py", line 25, in &lt;module&gt;
      private_key = serialization.load_pem_private_key(sk.read(), password=None, backend=default_backend())
    File "/home/michaelgraf/Desktop/train-user-client/venv/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization/base.py", line 16, in load_pem_private_key
      return backend.load_pem_private_key(data, password)
    File "/home/michaelgraf/Desktop/train-user-client/venv/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1089, in load_pem_private_key
      password,
    File "/home/michaelgraf/Desktop/train-user-client/venv/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1315, in _load_key
      self._handle_key_loading_error()
    File "/home/michaelgraf/Desktop/train-user-client/venv/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1373, in _handle_key_loading_error
      raise ValueError("Could not deserialize key data.")
  ValueError: Could not deserialize key data.
  </code></pre>
  
  <p>I am using python 3.7 and cryptography 2.8. Is there anything i could do differently when storing the keys in a file or generally? The keys need to be persisted to a file in some way to be stored for later use. Thanks in advance for any suggestions. </p>