How to make a key pair of ecdsa in a format that can be read by Java

Introduction

In some cases, such as JWT, you want to sign / verify using the public / private key on the server. I think that the JWT library side provides a method to create a key pair, but if you use it, a key pair will be generated every time you start the application. If you can't disable the last issued JWT each time you boot, you'll be using a fixed key pair.

These days, ECDSA seems to be used more often than RSA because it is smaller in size and has the same level of cryptographic strength.

Creating a key

Create in pem format.

openssl ecparam -genkey -name secp256k1 -out key-pair.pem
openssl pkcs8 -topk8 -inform pem -in key-pair.pem -outform pem -nocrypt -out private.pem
openssl ec -in key-pair.pem -pubout -outform pem -out public.pem

private key

private key is

import java.nio.file.Files;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

String privatePem = new String(Files.readAllBytes(path))
                    .replaceAll("\\r\\n", "")
                    .replaceAll("\\n", "")
                    .replaceAll("-----BEGIN PRIVATE KEY-----", "")
                    .replaceAll("-----END PRIVATE KEY-----", "");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privatePem));
PrivateKey privateKey = KeyFactory.getInstance("EC").generatePrivate(keySpec);

public key

String publicPem = new String(Files.readAllBytes(jwtSettings.getPublicKeyAsPath()))
                    .replaceAll("\\r\\n", "")
                    .replaceAll("\\n", "")
                    .replaceAll("-----BEGIN PUBLIC KEY-----", "")
                    .replaceAll("-----END PUBLIC KEY-----", "");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicPem));
PublicKey publicKey =  KeyFactory.getInstance(jwtSettings.getAlgorithm()).generatePublic(keySpec);

Reference link

Recommended Posts

How to make a key pair of ecdsa in a format that can be read by Java
Write a class that can be ordered in Java
I tried to make a client of RESAS-API in Java
How to make a Java container
How to make a Java array
Summary of ORM "uroboroSQL" that can be used in enterprise Java
How to implement a job that uses Java API in JobScheduler
Object-oriented design that can be used when you want to return a response in form format
How to make a Java calendar Summary
How to make a Discord bot (Java)
How to test a private method in Java and partially mock that method
How to get the absolute path of a directory running in Java
How to override in a model unit test so that Faker can be used to generate random values
How to display a web page in Java
How to make a follow function in Rails
Log out a CSV file that can be read in Excel using logback
How to make a unique combination of data in the rails intermediate table
Try to save the data that can be read by JavaFX as PNG
How to set tabs and spaces to be visible by using the tab key to insert spaces in Java files in Eclipse
How to create a Java environment in just 3 seconds
[Java] Integer information of characters in a text file acquired by the read () method
[Java] How to get the key and value stored in Map by iterative processing
How to create a data URI (base64) in Java
[Java] How to get a request by HTTP communication
I tried to make a login function in Java
[ruby] How to assign a value to a hash by referring to the value and key of another hash
[Java] How to cut out a character string character by character
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
Summary of how to implement default arguments in Java
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
Considering the adoption of Java language in the Reiwa era ~ How to choose a safe SDK
How to batch initialize arrays in Java that I didn't know when I was a beginner
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
A concise summary of Java 8 date / time APIs that are likely to be used frequently
I tried a puzzle that can only be solved by the bottom 10% of bad engineers
List of devices that can be previewed in Swift UI
Create a jar file that can be executed in Gradle
How to store a string from ArrayList to String in Java (Personal)
Introduction to Rakefile that can be done in about 10 minutes
Find a Switch statement that can be converted to a Switch expression
How to select a specified date by code in FSCalendar
Java (super beginner edition) that can be understood in 180 seconds
Things to be aware of when writing code in Java
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
How to derive the last day of the month in Java
Java --How to make JTable
[Swift5] How to create a .gitignore file and the code that should be written by default
Reference memo / In-memory LDAP server that can be embedded in Java
How to make a jar file with no dependencies in Maven
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to automatically operate a screen created in Java on Windows
How to get the id of PRIMAY KEY auto_incremented in MyBatis
Cast an array of Strings to a List of Integers in Java
How to rename a model with foreign key constraints in Rails
[Java] I tried to make a maze by the digging method ♪
How to output a list of strings in JSF as comma-separated strings
How to identify the path that is easy to make a mistake
Mecab installation that can be done almost by typing a command
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~