You are here

Introduction to OpenSSL and Encryption on Mac OS X

Error message

Deprecated function: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in drupal_strip_dangerous_protocols() (line 1458 of /home2/crephoto/public_html/techblog/includes/common.inc).

The openssl utility offers an interface to a vast range of encryption on Mac OS X. This article will provide a brief introduction to the use of openssl for password generation and data encryption. As always, "man openssl" is your friend.

The Nitty Gritty

A basic knowledge of openssl is useful for two reasons. First, password files utilized by the Apache web server and often named .htpasswd implement a basic password hashing with salts. By using openssl, you can manually generate passwords and add them to an Apache password file.

A second application for openssl is to encrypt sensitive information such as a text file containing personal information like bank accounts and credit cards. While I researched a number of GUI solutions for storing encrypted data, I liked the simplicity of using openssl to keep my data private. I certainly wouldn't trust Microsoft Word encryption to protect my data.

Password Generation

When generating passwords, salts are used to make attacking multiple passwords more difficult. Using a salt does nothing to protect a single password, but will protect multiple passwords (say a whole password file) by making dictionary attacks and brute force attacks much slower. Salts are a random bit of information essentially added to the password that makes the same plain text password have a different hashed value. Using the openssl command, you can generate passwords in multiple algorithms and formats. Here are two possible values for the password "foobar" in the Unix DES crypt(3) format:

% openssl passwd -crypt foobar
jFUO4DTdeEmpw

% openssl passwd -crypt foobar
cye1Y9RMJIk/2

In the above two examples, the salt is automatically generated and stored in plain text as the first two characters, i.e. "jF" for the first password and "cy" for the second. To verify a password, the operating system takes the salt and the plain text password, creates a hash, and then compares the hashed values for a match. Again, we can demonstrate this with the openssl command by telling it to use a specific salt, rather than generate a new one:

% openssl passwd -crypt -salt cy foobar
cye1Y9RMJIk/2

Notice how the generated password mathes the second example above. We can do the same thing for FreeBSD's MD5-based passwords. Here are two examples of "foobar":

% openssl passwd -1 foobar
$1$ZBvk.4b5$xL63iF/S/IMTnQgz9.80i/

% openssl passwd -1 foobar
$1$QdAorUxg$UNly3tkv6xvo69je8gpPE0

The two middle dollar signs are used to delimit the salt. Thus in the passwords above, the salts are "ZBvk.4b5" and "QdAorUxg" respectively. You'll notice that the salts are larger than in Unix DES, which improves the effectiveness of the salt. The "$1" at the beginning is a version number that allows FreeBSD to use a different algorithm in the future. Again, we can use openssl to show how the password would be verified by the OS:

% openssl passwd -1 -salt QdAorUxg foobar
$1$QdAorUxg$UNly3tkv6xvo69je8gpPE0

Hopefully this makes the topic of salts a little more clear. If you're still confused, try playing around with the openssl passwd command on your own for a bit.

Apache .htpasswd Files

The htpasswd utility is an implementation of openssl but offers limited options. For example, you cannot specify the salt. This makes it difficult, for example, if you want to verify the password contained in a file. Take the .htpasswd file below:

charles:mYaAVqGEIKlrA
betsey:HEsWINkKip3CU

Let's say that I can't remember my password, but I think it might be "foobar". Well, I could just point my browser to the web page and try it, but if it fails, we can't be sure if it's because I entered the wrong password, or some other reason. I could always use the htpasswd command to recreate the password, but using the openssl command, I can enter:

%  openssl passwd -crypt -salt mY foobar
mYaAVqGEIKlrA

Since the output matches the contents of the htpasswd file, I've got the right password.

Text File Encryption

Openssl is also by far the simplest means of encrypting text information. This is useful for hiding sensitive personal information like bank account numbers, credit cards, and social security numbers.

Given a plain text file by the name of plain.txt, you can encrypt the file with the following command:

openssl des3 -salt -in plain.txt -out encrypted

You will be prompted for a password for the encryption, and a file called "encrypted" will be created containing the encrypted contents of the file plain.txt. Once the text file is encrypted, you can delete the original plain text file, but be sure to remember the password so you can decrypt it later!

To reverse the above process and decrypt the file:

openssl des3 -d -salt -in encrypted -out plain.txt

Once you enter your password, the file will be decrypted and the plain text will be written to the specified output file. After you obtain the information you need from the plain text file, you can delete it, or edit the plain text file and repeat the encryption process as shown above.