Java KeyPair

Jakob Jenkov
Last update: 2018-02-10

The Java KeyPair class ( represents an asymmetric key pair. In other words, a public key + private key pair. A KeyPair instance is typically used when performing asymmetric cryptography, like encrypting or signing data.

Obtaining a KeyPair Instance

You will normally obtain a KeyPair instance from a Java keystore or a Java KeyPairGenerator.

Accessing KeyPair Public Key

You can access the PublicKey of a KeyPair by calling its getPublic() method. Her is an example of obtaining the PublicKey from a KeyPair instance:

PublicKey publicKey = keyPair.getPublic();

Accessing the KeyPair Private Key

You can access the PrivateKey of a KeyPair by calling the getPrivate() method. Here is an example of obtaining the PrivateKey from a KeyPair instance:

PrivateKey privateKey = keyPair.getPrivate();

Jakob Jenkov

Featured Videos

Core Software Performance Optimization Principles


Java Persistence
Close TOC

All Trails

Trail TOC

Page TOC