1. Overview

In public-key cryptography (also known as asymmetric cryptography), the encryption mechanism relies upon two related keys, a public key and a private key. The public key is used to encrypt the message while only the owner of the private key can decrypt the message.

In this tutorial, we’re going to see how to read public and private keys from a PEM file.

First, we’ll study some important concepts around public-key cryptography. Then, we’ll learn how to read PEM files using pure Java.

Finally, we’ll explore the BouncyCastle library as an alternative approach.

2. Concepts

Before we start, let’s understand some key concepts.

X.509 is a standard defining the format of public-key certificates. So, this format describes a public key among other information.

DER is the most popular encoding format to store data like X.509 certificates, PKCS8 private keys in files. It’s a binary encoding and the resulting content cannot be viewed with a text editor.

PKCS8 is a standard syntax for storing private key information. The private key can be optionally encrypted using a symmetric algorithm.

Not only can RSA private keys can be handled by this standard, but also other algorithms. The PKCS8 private keys are typically exchanged through the PEM encoding format.

#java #security

How to Read PEM File to Get Public and Private Keys
22.75 GEEK