Book Image

Demystifying Cryptography with OpenSSL 3.0

By : Alexei Khlebnikov
Book Image

Demystifying Cryptography with OpenSSL 3.0

By: Alexei Khlebnikov

Overview of this book

Security and networking are essential features of software today. The modern internet is full of worms, Trojan horses, men-in-the-middle, and other threats. This is why maintaining security is more important than ever. OpenSSL is one of the most widely used and essential open source projects on the internet for this purpose. If you are a software developer, system administrator, network security engineer, or DevOps specialist, you’ve probably stumbled upon this toolset in the past – but how do you make the most out of it? With the help of this book, you will learn the most important features of OpenSSL, and gain insight into its full potential. This book contains step-by-step explanations of essential cryptography and network security concepts, as well as practical examples illustrating the usage of those concepts. You’ll start by learning the basics, such as how to perform symmetric encryption and calculate message digests. Next, you will discover more about cryptography: MAC and HMAC, public and private keys, and digital signatures. As you progress, you will explore best practices for using X.509 certificates, public key infrastructure, and TLS connections. By the end of this book, you’ll be able to use the most popular features of OpenSSL, allowing you to implement cryptography and TLS in your applications and network infrastructure.
Table of Contents (20 chapters)
1
Part 1: Introduction
3
Part 2: Symmetric Cryptography
8
Part 3: Asymmetric Cryptography and Certificates
12
Part 4: TLS Connections and Secure Communication
16
Part 5: Running a Mini-CA

How to encrypt and decrypt with RSA on the command line

The openssl tool provides two subcommands for encrypting with RSA – pkeyutl and the deprecated, RSA-specific rsautl subcommand. We will, of course, use pkeyutl. Documentation for that subcommand can be found on the openssl-pkeyutl man page:

man openssl-pkeyutl

As explained previously, RSA is usually used for encrypting a session key, which will then be used to encrypt useful data. Let’s generate a 256-bit session key:

$ openssl rand -out session_key.bin 32

Now, let’s use openssl pkeyutl with our public RSA key for encrypting the session key:

$ openssl pkeyutl \
    -encrypt \
    -in session_key.bin \
    -out session_key.bin.encrypted \
    -pubin \
    -inkey rsa_public_key.pem \
    -pkeyopt rsa_padding_mode:oaep

Note the -pkeyopt rsa_padding_mode:oaep switch. It instructs...