title: StreamCorruptedException during serialised object decryption (and other byte fields)
body: <p>my software raised StreamCorruptedException during a deciphering:
  My cipher is AES/CBC/PKCS5Padding and my key is obtained with PBKey Derivation method, so I need to create a salt to generate AES128 key.</p>
  
  <p>My goal is to obtain a file formed in this way:</p>
  
  <p><a href="https://i.stack.imgur.com/LvyHE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LvyHE.png" alt=""></a></p>
  
  <p>(I will delete exception management code to improve readability)
  My cipher code:</p>
  
  <pre><code>char[] password = passwordString.toCharArray();
  
      SecureRandom random = new SecureRandom();
      byte salt[] = new byte[SALT_BYTES]; 
      random.nextBytes(salt);
  
      SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
  
      KeySpec keySpec = new PBEKeySpec(password, salt, ITERATION, AES_KEY_BITS);
  
      SecretKey tmp = factory.generateSecret(keySpec);
  
      SecretKey secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
  
      Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
  
      cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  
      FileOutputStream fout = null;
      ObjectOutputStream objOut = null;
  
  
          fout = new FileOutputStream(PRIVATE_RING_FILENAME);
  
          fout.write(salt);
  
          byte[] ivN = cipher.getIV();
          fout.write(ivN);
  
          CipherOutputStream cos = new CipherOutputStream(fout, cipher);
          objOut = new ObjectOutputStream(cos);
  
          PrivateKeyRing prvKeyRing = new PrivateKeyRing();
          SealedObject sealedObject = new SealedObject(prvKeyRing, cipher);
          objOut.writeObject(sealedObject);
  
          fout.close();
          objOut.close();
          cos.close();
  </code></pre>
  
  <p>and it works without problem. </p>
  
  <p>My deciphering code:</p>
  
  <pre><code>char[] password = passwordString.toCharArray();
  
      File file = new File(PRIVATE_RING_FILENAME);
      FileInputStream fin = new FileInputStream(file);
  
  
      Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
  
  
      byte[] salt = new byte[SALT_BYTES];
  
      fin.read(salt);
  
  
      SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
  
  
      KeySpec keySpec = new PBEKeySpec(password, salt, ITERATION, AES_KEY_BITS);
  
      SecretKey = factory.generateSecret(keySpec);
  
      SecretKey secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
  
  
          byte[] ivN = new byte[AES_BYTES];
          fin.read(ivN, 0, AES_BYTES);
  
          cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(ivN));
  
      CipherInputStream cis = new CipherInputStream(fin, cipher);
      ObjectInputStream objIn;
      PrivateKeyRing prvKeyRing = null;
      SealedObject sealedObject = null;
      objIn = new ObjectInputStream(cis);
  
      sealedObject = (SealedObject) objIn.readObject();
      prvKeyRing = (PrivateKeyRing) sealedObject.getObject(cipher);
  
          objIn.close();
          fin.close();
          cis.close();
  </code></pre>
  
  <p>But StreamCorruptedException: invalid stream header: 73720019 occurs when system execute:</p>
  
  <pre><code>objIn = new ObjectInputStream(cis);
  </code></pre>
  
  <p>If I try to write the object without ciphering all works.
  What do you think about? 
  I read about some problem when you try to write multiple serialised object but I think this is not the case.</p>