Sunday, 3 April 2022

openSSL: How to generate RSA private, public key pair

Syntax

openssl genrsa -des3 -out {FILE_NAME} {KEY_SIZE}

 

Example

openssl genrsa -des3 -out my_rsa_key_pair 2048

 Option -des3 make sure that the pem file generated in the above step will be protected by a passphrase.

 

$openssl genrsa -des3 -out my_rsa_key_pair 2048
Generating RSA private key, 2048 bit long modulus
..................................+++
..........................+++
e is 65537 (0x10001)
Enter pass phrase for my_rsa_key_pair:
Verifying - Enter pass phrase for my_rsa_key_pair:
$
$ls
my_rsa_key_pair

 


Export private key to a pem file

Export private key to privateKey.pem file by executing below commands. 

openssl pkcs8 -topk8 -inform PEM -in my_rsa_key_pair -outform PEM -out privateKey.pem -nocrypt

 

$openssl pkcs8 -topk8 -inform PEM -in my_rsa_key_pair -outform PEM -out privateKey.pem -nocrypt
Enter pass phrase for my_rsa_key_pair:
$
$ls
my_rsa_key_pair	privateKey.pem
$
$cat privateKey.pem 
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCggQ7vAqJvYhYv
v7V6GeoDS258BeANEShKWslGqN8V6R2xLMB/dFyyDt50YBFb7gScliZQmNjFDjo/
EbtaETGxxTv5958TOA4VfurEwHrzMbyChTZwAKlBKhvyyVFN2ZATLR0jMg97g6f1
PdDvCYFIbk15RMKjSXSEz7xG8K6hKvkdoFqk4MORHvvC1e438+KqwhwSgVCa5D0E
UQ0Hm6aUOjRrVwn7iuG/pktndPWMWfqcS9yGUIdOlg2F1rZQdorej0O9MCVsIEON
nGXhStwnGJMchGcraFsEsS8JROmVcZQV+8ebPM2R+hmIQi1xAAw4stWQ4EJkJq2b
SHop9ekHAgMBAAECggEASqyg4QyNvFqbTe3Ypg1Gde/zYFpvakTwRN/GQ0ywNgZ2
XQcgLGxb8C/Ayc4kZWvw01V8UAU83TmIWe2LCBV11GqaQ+s0JIHw84lP+ug+i8U5
GlSWBjgYhwvrYuLNI/Yz9OgJZWWWFHYlf5EaEw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B7FGsBs/JNuip4wR4pk=
-----END PRIVATE KEY-----

 Export public key to a pem file

Export public key to publicKey.pem file by executing below command.

openssl rsa -in my_rsa_key_pair -outform PEM -pubout -out publicKey.pem

$openssl rsa -in my_rsa_key_pair -outform PEM -pubout -out publicKey.pem
Enter pass phrase for my_rsa_key_pair:
writing RSA key
$
$ls
my_rsa_key_pair	publicKey.pem
$
$cat publicKey.pem 
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoIEO7wKib2IWL7+1ehnq
A0tufAXgDREoSlrJRqjfFekdsSzAf3Rcsg7edGARW+4EnJYmUJjYxQ46PxG7WhEx
scU7+fefEzgOFX7qxMB68zG8goU2cACpQSob8slRTdmQEy0dIzIPe4On9T3Q7wmB
SG5NeUTCo0l0hM+8RvCuoSr5HaBapODDkR77wtXuN/PiqsIcEoFQmuQ9BFENB5um
lDo0a1cJ+4rhv6ZLZ3T1jFn6nEvchlCHTpYNhda2UHaK3o9DvTAlbCBDjZxl4Urc
JxiTHIRnK2hbBLEvCUTplXGUFfvHmzzNkfoZiEItcQAMOLLVkOBCZCatm0h6KfXp
BwIDAQAB
-----END PUBLIC KEY-----



 

Previous                                                    Next                                                    Home

openSSL: Encrypt and decrypt the data using secret key algorithm

In this post, I am going to explain how to encrypt and decrypt the data using AES symmetric key encryption algorithm.

 

Step 1: Create demo.txt file with some content.

 

demo.txt 

Hello World

 

Step 2: Execute below command to encrypt the contents of file ‘demo.txt’. Encrypted content is copied to encryptedData.bin.

 


openssl enc -aes-256-cbc -in demo.txt -out encryptedData.bin

 

You need to supply a password to encrypt the data. For example, I given ‘password123’ as password.

 

$openssl enc -aes-256-cbc -in demo.txt -out encryptedData.bin
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
$
$ls
demo.txt		encryptedData.bin

 

Step 3: Execute below command to decrypt the file

openssl enc -aes-256-cbc -d -in encryptedData.bin -pass pass:password123

$openssl enc -aes-256-cbc -d -in encryptedData.bin -pass pass:password123
Hello World

 


 

 

 

 

Previous                                                    Next                                                    Home

openSSL: Get all the supported secret key algorithms

Command ‘openssl list-cipher-commands’ prints all the secret key algorithms supported by openSSL.

$openssl list-cipher-commands
aes-128-cbc
aes-128-ecb
aes-192-cbc
aes-192-ecb
aes-256-cbc
aes-256-ecb
base64
bf
bf-cbc
bf-cfb
bf-ecb
bf-ofb
camellia-128-cbc
camellia-128-ecb
camellia-192-cbc
camellia-192-ecb
camellia-256-cbc
camellia-256-ecb
cast
cast-cbc
cast5-cbc
cast5-cfb
cast5-ecb
cast5-ofb
chacha
des
des-cbc
des-cfb
des-ecb
des-ede
des-ede-cbc
des-ede-cfb
des-ede-ofb
des-ede3
des-ede3-cbc
des-ede3-cfb
des-ede3-ofb
des-ofb
des3
desx
rc2
rc2-40-cbc
rc2-64-cbc
rc2-cbc
rc2-cfb
rc2-ecb
rc2-ofb
rc4
rc4-40

 

 


 

Previous                                                    Next                                                    Home

openSSL: Get all the standard commands

‘openssl list-standard-commands’ print all the standard commands available in openSSL.

$openssl list-standard-commands
asn1parse
ca
certhash
ciphers
crl
crl2pkcs7
dgst
dh
dhparam
dsa
dsaparam
ec
ecparam
enc
errstr
gendh
gendsa
genpkey
genrsa
nseq
ocsp
passwd
pkcs12
pkcs7
pkcs8
pkey
pkeyparam
pkeyutl
prime
rand
req
rsa
rsautl
s_client
s_server
s_time
sess_id
smime
speed
spkac
ts
verify
version
x509

 


 

 

Previous                                                    Next                                                    Home

How to get the openSSL version?

Execute the command ‘openssl version’ to get the openSSL version. 

$openssl version
LibreSSL 2.8.3

 

By adding the option -a to the above command you can get some more additional details.

$openssl version -a
LibreSSL 2.8.3
built on: date not available
platform: information not available
options:  bn(64,64) rc4(16x,int) des(idx,cisc,16,int) blowfish(idx) 
compiler: information not available
OPENSSLDIR: "/private/etc/ssl"

 


 

 

Previous                                                    Next                                                    Home

openSSL tutorial

OpenSSL is a popular open source cryptography library. In this tutorial series, I am going to explain different openssl commands that will help you in real time.

 

      How to get the openSSL version?
      Get all the standard commands
      Get all the supported secret key algorithms
      Encrypt and decrypt the data using secret key algorithm
      How to generate RSA private, public key pair
      Export private key to a pem file
      Export public key to a pem file
      Export private and public keys to a pem file
      Generate ssl certificate
      Viewing the certificate as plain text
      How to associate an existing private key to a self-signed certificate?

 

Reference

https://www.openssl.org/

 

Previous                                                    Next                                                    Home

Spring: oepnapi: Document allowable values of a request header

Using schema attribute of @Parameter annotation, we can specify the allowable values of a header.

 

Example

@Parameter(name = "infoLevel", in = ParameterIn.HEADER, description = "info level", required = true, schema = @Schema(type = "string", allowableValues = {
		"ONLY_ID", "ONLYE_NAME_AND_ID", "ALL" }))
@GetMapping("/about-me")
public ResponseEntity<Map<String, Object>> aboutMe() {
	......
	......
}

Above snippet generate below documentation for the header infoLevel.


 
 

Find the below working application.

 

Step 1: Create new maven project ‘openapi-document-header-allowable-values’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>openapi-document-header-allowable-values</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.4</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-ui</artifactId>
			<version>1.6.6</version>
		</dependency>


	</dependencies>

</project>

 

Step 3: Define UserController class.

 

UserController.java

package com.sample.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;

@RestController
@RequestMapping(value = "/api/v1/users")
@CrossOrigin("*")
public class UserController {

	@Parameter(name = "authorization1", in = ParameterIn.HEADER, description = "Bearer token", required = true, examples = {
			@ExampleObject(name = "example1", value = "Bearer 1233rada"),
			@ExampleObject(name = "example2", value = "Bearer ASDSFA") })
	@Parameter(name = "infoLevel", in = ParameterIn.HEADER, description = "info level", required = true, schema = @Schema(type = "string", allowableValues = {
			"ONLY_ID", "ONLYE_NAME_AND_ID", "ALL" }))
	@GetMapping("/about-me")
	public ResponseEntity<Map<String, Object>> aboutMe() {
		Map<String, Object> myDetails = new HashMap<>();
		myDetails.put("id", 1);
		myDetails.put("name", "Krishna");

		return ResponseEntity.ok(myDetails);

	}

}

 

Step 4: Define main application class.

 

App.java

 

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {

		SpringApplication.run(App.class, args);

	}
}

 

Total project structure looks like below.

 


 

Run App.java and open the url ‘http://localhost:8080/swagger-ui/index.html’ to see the documentation.

 

You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/openapi/openapi-document-header-allowable-values

  

Previous                                                    Next                                                    Home