Java KeyPair

Jakob Jenkov
Last update: 2018-02-10

The Java KeyPair class (java.security.KeyPair 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

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next