OpenSSL Hacks

OpenSSL contains a command-line tool to do nearly everything possible within the OpenSSL library. Even better, it's probably already installed on your system.
Quick and Clean Encryption

Although not OpenSSL's strength, it also can encrypt files. The flexibility of OpenSSL makes it a bit more complicated than GnuPG.

OpenSSL has very few defaults, so more options have to be used. There are also many algorithms from which to choose. Some algorithms, like DES and RC4-40, are kept only for backward compatibility and shouldn't be used anymore. Strong algorithms you should use include bf, which is the Blowfish algorithm, and -aes-128-cbc, which is the US NIST Advanced Encryption Standard (AES) with 128-bit keys running in Cipher Block Chaining (CBC) mode.

Here is an example:

$ openssl enc -aes-128-cbc < filename > filename.aes-128-cbc
enter aes-128-cbc encryption password:
Verifying - enter aes-128-cbc encryption password:

As with GnuPG, OpenSSL asks for a passphrase twice, which will not echo to the screen.

Decryption is also a bit more complicated:

$ openssl enc -d -aes-128-cbc -in filename.aes-128-cbc > filename
enter aes-128-cbc decryption password:

Note the -d in this example, which specifies decryption.

OpenSSL, unlike GnuPG, does not automatically detect the file type or even what algorithm, key length and mode were used to encrypt a file. You need to keep track of that yourself. In my example, I've put that information in the filename extension. OpenSSL won't manage the files and file extensions for you, you have to specify where you want the outgoing data written.

If you don't specify the correct algorithm, OpenSSL either may spew garbage or complain about a bad magic number. Either way, without the correct options, your data won't decrypt properly. To be fair, this is simply not something OpenSSL was designed to do, but it does work.


Before we go much further, we should discuss the importance of passphrases. In most cryptosystems, the passphrase is the secret that keeps the other secrets. It's usually the weakest point. So, creating strong passphrases is important, but it's also difficult, unless you have the right tools. Using OpenSSL, you can create a strong passphrase quickly.

A simple guide to passphrases is that longer is usually better, and eight characters is not long enough (Table 1). The goal is to make a secret that you can remember but that someone else won't know, can't guess or won't eventually stumble upon.

Table 1. Password and passphrase strengths compared with estimated time to crack. Note: time to crack is very rough. Your crackage may vary.

TypeBytesCharactersBits/CharacterTotal BitsTime to Crack
Base64 [A-Za-z0-9+/=]68648Minutes to hours
Base64 [A-Za-z0-9+/=]912672Years
Base64 [A-Za-z0-9+/=]1216696Decades
Base64 [A-Za-z0-9+/=]15206120Uncrackable?
Diceware Passphrase 8 words12.9 per word120Uncrackable?
Generating a Passphrase

OpenSSL can create very strong random passphrases like this:

$ openssl rand 15 -base64

If you run this example, your output will be different from the example, because the passphrase is randomly generated.

The first argument of 15 is the number of binary bytes to generate, and the second argument of -base64 specifies that those binary bytes should be encoded in base64 characters. For 15 bytes, the output always will be 20 characters, plus a newline character.

The base64 character set consists only of uppercase and lowercase letters A–Z, the numbers 1–9 and the three punctuation characters: plus, slash and equals. This is intentionally a limited character set, but more complexity in the character set is not necessarily better. Adding only one additional character makes up for the difference in security. For example, an eight-character fully printable ASCII password is about a strong as a nine-character base64 password.

Although not as quick as using OpenSSL rand, the Diceware passphrase generator produces strong and often easy-to-memorize passphrases. I highly recommend it.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

In relation to the password

IainB's picture

In relation to the password hash, surely the server needs to know the salt too? When I run the example I don't get the salt returned, so how can the hash be used?? Can the salt be derived from the hash somehow, and if so, doesn't knowledge of the salt make the hash much, much weaker?

I'm sure I've missed something obvious!

As far as I know, the salt

aurir_'s picture

As far as I know, the salt is stored between two dollar signs. So it's part of the hash. At least for the MD5 (openssl passwd -1).

MD5 passwd hash

BernieD's picture

I need a cc or gcc source file that I can compile on an HP running HPUX 11. I want to compile it into an existing process that currently generates and hashes 8 digit passwords using DES i.e., the old crypt(). I nust now increase that password from 8 positions to 15 for security purposes. I'd guess that a perl script would do also as long as it had the proper hooks to the MD5 hash.

I will really appreciate any help on this item.


Anonymous's picture

Great Article Anthony!

Very interesting and informational.

Can you expand a little on where you say:

" the above example multiple times. The output is the cryptographic hash of your password. The hash is randomly salted so that every time it's run, the output will be different, even if the password is the same."

Why will the output be different?



Wu Yongzheng's picture

Basically, salt is a random string appended to your password. So instead of hash(password), we do hash(password | salt). The server keeps both salt and hash value. The purpose of salt is to prevent pre-calculated table attack.


Page is rendered too wide

rhkramer's picture

The lines on this page are too wide, I have to scroll horizontally to read each line, hence I haven't read the article.

The problem seems to be Table 2, and specifically the header lines, but Table 3 may also be wide on some screens.

I'm sure someone can find a number of creative ways to solve the problem.

Randy Kramer

Page is rendered too wide

Keith Daniels's picture

In most browsers, those capable of handling cascading style sheets (CSS) that table displays with a horizontal scroll bar at the bottom and does not extend the page width.

What browser are you using and what version?

Linux Journal

I forgot to ask

Keith Daniels's picture

Have you changed the settings in your browser so you use a default or customized style sheet for it? This is common with Opera.

Linux Journal

All the new OSs and windowing systems are oriented towards content consumption instead of content production.

--Steve Daniels 2013

Width problem

Allan Morris's picture

The width problem on this page as commented by others is still there. Using Konqueror 3.5.4 on Kubuntu with stylesheet set as default. Browser as-is passes the acid style test 100%. The problem is with your site.


Allan Morris.

Width problem

Keith Daniels's picture

There were two problems here. Though I made the page quit showing so wide, the problems still exist.

Problem #1 Konqueror will not obey the CSS rule (overflow: auto;) for a table which would make a horizontal scroll bar appear only underneath the over sized table and the screen would not display wide. All other browsers I tested do this.

Problem #2 Konqueror obeyed a CSS rule for table headers (th{white-space: nowrap;}) that none of the other browsers paid any attention to. I killed the white-space rule and the page no longer over extends your screen (or at least not as much, there is still a point where the table headers will not continue to wrap). If the other browsers had obeyed this rule I would have found this problem much earlier.


All the new OSs and windowing systems are oriented towards content consumption instead of content production.

--Steve Daniels 2013