# How is private key protected in Java JKS keystore file?

• The store password is just to verify the integrity of the file?
• How does the private keys are protected?
• The way Java handles keys is obscure and frustrating! – trognanders Nov 16 '16 at 7:01

This is implemented by sun.security.provider.JavaKeyStore (source in mercurial) but is pretty simple and based on Java's Data{Output,Input}Stream conventions: 4-byte magic number, 4-byte version number, 4-byte count of entries; a variable number of entries each containing 4-byte type, length+(modified)UTF name, 8-byte timestamp, length+encoding+certcount for a privatekey, and type (in practice always X.509)+length+encoding for the/each certificate; and at the end hash of password plus preceding data for integrity check.
Yes. The JCA API, and the JKS format, allows each privatekey to be encrypted with a separate password, which can be different from the store password. keytool for example allows you to specify a key password, but defaults it to be the same as the store password. However, some programs cannot cope with differing key and store passwords; in particular, those using the default trustmanager and/or keymanager for SSL/TLS (and thus HTTPS) so in those cases the same password must be used for both store and key(s).
Each privatekey is PBE+hash using a 'proprietary' scheme in sun.security.provider.KeyProtector (source in mercurial) and formatted in the file as a PKCS#8 EncryptedPrivateKeyInfo.