Core Java

Secure Password Storage – Don’ts, dos and a Java example

The importance of storing passwords securely
As software developers, one of our most important responsibilities is the protection of our users’ personal information. Without technical knowledge of our applications, users have no choice but to trust that we’re fulfilling this responsibility. Sadly, when it comes to passwords, the software development community has a spotty track record.
While it’s impossible to build a 100% secure system, there are fortunately some simple steps we can take to make our users’ passwords safe enough to send would-be hackers in search of easier prey.
If you don’t want all the background, feel free to skip to the Java SE example below.
The Don’ts
First, let’s quickly discuss some of the things you shouldn’t do when building an application that requires authentication:
  • Don’t store authentication data unless you really have to. This may seem like a cop-out, but before you start building a database of user credentials, consider letting someone else handle it. If you’re building a public application, consider using OAuth providers such as Google or Facebook. If you’re building an internal enterprise application, consider using any internal authentication services that may already exist, like a corporate LDAP or Kerberos service. Whether it’s a public or internal application, your users will appreciate not needing to remember another user ID and password, and it’s one less database out there for hackers to attack.
  • If you must store authentication data, for Gosling’s sake don’t store the passwords in clear text. This should be obvious, but it bears mentioning. Let’s at least make the hackers break a sweat.
  • Don’t use two-way encryption unless you really need to retrieve the clear-text password. You only need to know their clear-text password if you are using their credentials to interact with an external system on their behalf. Even then, you’re better off having the user authenticate with that system directly. To be clear, you do not need to use the user’s original clear-text password to perform authentication in your application. I’ll go into more detail on this later, but when performing authentication, you will be applying an encryption algorithm to the password the user entered and comparing it to the encrypted password you’ve stored.
  • Don’t use outdated hashing algorithms like MD5. Honestly, hashing a password with MD5 is virtually useless. Here’s an MD5-hashed password: 569a70c2ccd0ac41c9d1637afe8cd932. Go to http://www.md5hacker.com/ and you can decrypt it in seconds.
  • Don’t come up with your own encryption scheme. There are a handful of brilliant encryption experts in the world that are capable of outwitting hackers and devising a new encryption algorithm. I am not one of them, and most likely, neither are you. If a hacker gets access to your user database, they can probably get your code too. Unless you’ve invented the next great successor to PBKDF2 or bcrypt, they will be cackling maniacally as they quickly crack all your users’ passwords and publish them on the darknet.
The Dos
Okay, enough lecturing on what not to do. Here are the things you need to focus on:
  • Choose a one-way encryption algorithm. As I mentioned above, once you’ve encrypted and stored a user’s password, you never need to know the real value again. When a user attempts to authenticate, you’ll just apply the same algorithm to the password they entered, and compare that to the encrypted password that you stored.
  • Make the encryption as slow as your application can tolerate. Any modern password encryption algorithm should allow you to provide parameters that increase the time needed to encrypt a password (i.e. in PBKDF2, specifying the number of iterations). Why is slow good? Your users won’t notice if it takes an extra 100ms to encrypt their password, but a hacker trying a brute-force attack will notice the difference as they run the algorithm billions of times.
  • Pick a well-known algorithm. The National Institute of Standards and Technology (NIST) recommends PBKDF2 for passwords. bcrypt is a popular and established alternative, and scrypt is a relatively new algorithm that has been well-received. All these are popular for a reason: they’re good.
PBKDF2
Before I give show you some concrete code, let’s talk a little about why PBKDF2 is a good choice for encrypting passwords:
  • Recommended by the NIST. Section 5.3 of Special Publication 800-132 recommends PBKDF2 for encrypting passwords. Security officials will love that.
  • Adjustable key stretching to defeat brute force attacks. The basic idea of key stretching is that after you apply your hashing algorithm to the password, you then continue to apply the same algorithm to the result many times (the iteration count). If hackers are trying to crack your passwords, this greatly increases the time it takes to try the billions of possible passwords. As mentioned previously, the slower, the better. PBKDF2 lets you specify the number of iterations to apply, allowing you to make it as slow as you like.
  • A required salt to defeat rainbow table attacks and prevent collisions with other users. A salt is a randomly generated sequence of bits that is unique to each user and is added to the user’s password as part of the hashing. This prevents rainbow table attacks by making a precomputed list of results unfeasible. And since each user gets their own salt, even if two users have the same password, the encrypted values will be different. There is a lot of conflicting information out there on whether the salts should be stored someplace separate from the encrypted passwords. Since the key stretching in PBKDF2 already protects us from brute-force attacks, I feel it is unnecessary to try to hide the salt. Section 3.1 of NIST SP 800-132 also defines salt as a “non-secret binary value,” so that’s what I go with.
  • Part of Java SE 6. No additional libraries necessary. This is particularly attractive to those working in environments with restrictive open-source policies.
Finally, a concrete example
Okay, here’s some code to encrypt passwords using PBKDF2. Only Java SE 6 is required.
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class PasswordEncryptionService {

 public boolean authenticate(String attemptedPassword, byte[] encryptedPassword, byte[] salt)
   throws NoSuchAlgorithmException, InvalidKeySpecException {
  // Encrypt the clear-text password using the same salt that was used to
  // encrypt the original password
  byte[] encryptedAttemptedPassword = getEncryptedPassword(attemptedPassword, salt);

  // Authentication succeeds if encrypted password that the user entered
  // is equal to the stored hash
  return Arrays.equals(encryptedPassword, encryptedAttemptedPassword);
 }

 public byte[] getEncryptedPassword(String password, byte[] salt)
   throws NoSuchAlgorithmException, InvalidKeySpecException {
  // PBKDF2 with SHA-1 as the hashing algorithm. Note that the NIST
  // specifically names SHA-1 as an acceptable hashing algorithm for PBKDF2
  String algorithm = "PBKDF2WithHmacSHA1";
  // SHA-1 generates 160 bit hashes, so that's what makes sense here
  int derivedKeyLength = 160;
  // Pick an iteration count that works for you. The NIST recommends at
  // least 1,000 iterations:
  // http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
  // iOS 4.x reportedly uses 10,000:
  // http://blog.crackpassword.com/2010/09/smartphone-forensics-cracking-blackberry-backup-passwords/
  int iterations = 20000;

  KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength);

  SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);

  return f.generateSecret(spec).getEncoded();
 }

 public byte[] generateSalt() throws NoSuchAlgorithmException {
  // VERY important to use SecureRandom instead of just Random
  SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

  // Generate a 8 byte (64 bit) salt as recommended by RSA PKCS5
  byte[] salt = new byte[8];
  random.nextBytes(salt);

  return salt;
 }
}
The flow goes something like this:
  1. When adding a new user, call generateSalt(), then getEncryptedPassword(), and store both the encrypted password and the salt. Do not store the clear-text password. Don’t worry about keeping the salt in a separate table or location from the encrypted password; as discussed above, the salt is non-secret.
  2. When authenticating a user, retrieve the previously encrypted password and salt from the database, then send those and the clear-text password they entered to authenticate(). If it returns true, authentication succeeded.
  3. When a user changes their password, it’s safe to reuse their old salt; you can just call getEncryptedPassword() with the old salt.
Easy enough, right? If you’re building or maintaining an application that violates any of the “don’ts” above, then please do your users a favor and use something like PBKDF2 or bcrypt. Help them, Obi-Wan Developer, you’re their only hope.

Jerry Orr

Jerry Orr is a software developer who currently spends most of his time on Java web applications. He has worked in a variety of domains, including commercial software, higher education, state government, and federal government.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

52 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Meanbunny
Meanbunny
10 years ago

When authenticating the user, how do we retrieve the salt if we aren’t storing it in the table? I am very interested by your article and have learned a great deal. Just trying to implement now.

Meanbunny
Meanbunny
10 years ago
Reply to  Meanbunny

nvm…just read it right this time

New2Dev
New2Dev
10 years ago

What happens if a user forgets their password. Isn’t this a time when it is necessary to access the password in clear text?

Or is it best to suggest them to create a new password/send them a new password to a registered email address?

Jerry Orr
10 years ago
Reply to  New2Dev

> What happens if a user forgets their password. Isn’t this a time when it is necessary to access the password in clear text?

Absolutely not! It should never be possible to retrieve someone’s clear-text password, because if *you* can do it, then someone who has stolen your database can, too. In fact, if a website ever sends you your clear-text password, you know they aren’t doing a good job protecting it.

If a user forgets his or her password, usually the best thing to do is send a password reset link to their email address.

sunil kumar verma
sunil kumar verma
10 years ago

I have stored salt in salt column and encrypted password in password column at the time of signup by using PBKDF2
How to apply authenticate() function to authenticate user at login time.

Tillmann
Tillmann
9 years ago

Well written, very helpful, thank you!

Sansuns
Sansuns
9 years ago

Does the encryption / decryption affected by the OS when encrypted originally in Windows and later on retrieved from a Linux system to compare against a clear text password even when the salt are the same?

Jerry Orr
9 years ago
Reply to  Sansuns

The underlying algorithm (PBKDF2 or bcrypt) is platform-independent. A password hashed with the same salt, algorithm, and algorithm settings (iterations, key length, etc) will be identical on any OS.

Peter Lynch
Peter Lynch
9 years ago

I use the above getEncryptedPassword method in two separate programs, both of which query the same database. One program is a stand-alone Java program, the other is a web application deployed to Apache Geronimo. The problem I have is that, using the same plaintext password and salt, I get two different encrypted passwords for the two different programs, and thus I can log on using one program but not the other one. Do you know what could be going on?

Jerry Orr
9 years ago
Reply to  Peter Lynch

The algorithm is standard and platform/installation independent. If you are using the same iteration count, salt, and plain-text password, you will get the exact same hash back. Unless there’s a bug in the JRE’s PBKDF2 implementation (very unlikely), I suspect that either the iteration count, salt, or password is different.

Peter Lynch
Peter Lynch
9 years ago
Reply to  Jerry Orr

I (sort of) got to the bottom of what was going wrong. I had changed the method return type from byte[] to String, and converted your final line to “return new String(f.generateSecret(spec).getEncoded());”. Once I changed back to just using a byte[], both programs now work as expected.

Jerry Orr
9 years ago
Reply to  Peter Lynch

new String(byte[]) uses the platform’s default charset. I suspect the two runtimes were using different default charsets, thus generating different Strings.

Marco
Marco
9 years ago

Hey Jerry, great tutorial, thanks!

Just a question: is there a reason to declare these methods non static and thus instantiate the PasswordEncryptionService class?

Is it safe to declare the class methods to be static?

Jerry Orr
9 years ago
Reply to  Marco

It is technically safe to make them static, as this particular class has no state, but that’s generally an anti-pattern. It’s way too big of a topic to cover in a comment, but it makes it difficult to do things like dependency injection, extension, unit testing, etc. If, say, you decided to give this class a SecretKeyFactory pool injected by Spring, you’d end up making the methods non-static again.

Dustin Currie
Dustin Currie
8 years ago

I’ve never really understood this when it comes to command line apps. How do you handle passwords if the application is the user–some scheduled command line app that logs into a site and downloads data for example. It needs the passwords in plain text to login to the site.

Jerry Orr
8 years ago
Reply to  Dustin Currie

Storing passwords that you can use on a user’s behalf (what you’re describing) is actually a very different scenario than checking that an active user entered the correct password (the topic of this article). Storing a user’s password for later use is a more difficult and dangerous scenario. You of course want to encrypt the password, but if you need to decrypt it in some batch process, you have no choice but to keep the required information to decrypt the password (eg a private certificate or passphrase) on the same system. So anyone who has full access to your system… Read more »

DNM
DNM
7 years ago
Reply to  Jerry Orr

I have a password for a server, that I receive encrypted, but need to use it as clear txt in every single req coming to my application. My question is – is it insecure to save it in clear txt, as static member of a class?
because decrypting every time is expensive, especially when we know it will be same , unless changed manually or the application is restarted.

Jerry Orr
7 years ago
Reply to  DNM

You definitely don’t want to store it in clear text. There are a variety of ways for memory to be leaked or compromised, such as forcing a memory heap dump. You want to keep it in clear text for as short a period as possible. Some developers go as far as to only write it to a char[] and then null out each character when done, instead of waiting for garbage collection to get rid of a String. Have you measured how “expensive” decrypting it is? If decrypting it takes 1ms, and your average request is 500ms +/- 100ms, is… Read more »

Srikanth
Srikanth
8 years ago

This is an incredibly well written article. Thanks a bunch for this very helpful stuff.

Srikanth

Jerry Orr
8 years ago
Reply to  Srikanth

Thanks, I’m glad you found it helpful!

Medha
Medha
8 years ago

The code is not working for me..Please suggest /** * */ package com.plant.DAO; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Arrays; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; /** * @author Medha * */ public class PassEncryptDecrypt { public static boolean authenticate(String attemptedPassword, byte[] encryptedPassword, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { // Encrypt the clear-text password using the same salt that was used to // encrypt the original password System.out.println(“We are in Password Encryption Decryption class”); System.out.println(“Attempted Password is “+attemptedPassword); System.out.println(“Encrypted password is “+encryptedPassword); System.out.println(“Salt pattern is “+salt); byte[] encPasswordGenerated = getEncryptedPassword(attemptedPassword, salt); System.out.println(“comparing the attempted and password generated by authentication”);… Read more »

Medha
Medha
8 years ago
Reply to  Medha

Please help it is very urgent.
Thanks ,
Medha

Jerry Orr
8 years ago
Reply to  Medha

The code you pasted looks correct to me, so I suspect something else is going on outside of this class. Perhaps there’s an issue storing or retrieving the password from the database? What is the database type? What are the column types for the password and salt in the PasswdDetails table?

Also, it can sometimes be easier to understand what’s going on if you can actually see a human-friendly decoded version of the byte arrays. I usually print them out in hexadecimal, like this:

javax.xml.bind.DatatypeConverter.printHexBinary(bytes)

Medha
Medha
8 years ago

I don’t get the option to Sign In and see my comments
Thanks,
Medha

Medha
Medha
8 years ago

Thanks Jerry for your quick response..I will paste the full code after some time

Medha
Medha
8 years ago

Here is my table description..Salt is stored as encpattern here desc PasswdDetails Name Null Type ———- ——– ————- USERNAME NOT NULL VARCHAR2(20) PASSWORD VARCHAR2(100) ENCPATTERN VARCHAR2(100) My LoginServlet: package com.plant.servlets; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.plant.DAO.FetchPassDAO; import com.plant.DAO.LoginDAO; import com.plant.model.User; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest request, HttpServletResponse… Read more »

Jerry Orr
8 years ago
Reply to  Medha

Wow, that’s a lot of unformatted code; difficult to follow. A couple suggestions which may or may not help: 1. You are doing “select * from PasswdDetails where username=?”, and then getting the columns by index. This can be dicey, as the column order can be unpredictable with “select *”. I suggest either selecting explicit columns (“select username, password, encpattern…”) or retrieving columns from the ResultSet by name (“rs.getString(“password”)”, etc) 2. Chartsets can get tricky, particularly when you’re dealing with byte arrays. I suggest either storing it in the database as a binary type (instead of varchar) or just encoding… Read more »

Medha
Medha
8 years ago
Reply to  Jerry Orr

Thanks Jerry for your valuable suggestions.
Kindly suggest datatype to store byte[] in oracle database.

Jerry Orr
8 years ago
Reply to  Medha

I don’t use Oracle, but this SO answer recommends the RAW datatype: http://stackoverflow.com/questions/10133006/how-to-store-a-java-byte-array-in-an-oracle-database

Medha Pundir`
Medha Pundir`
8 years ago
Reply to  Jerry Orr

Yes I have also checked the same…. thank you
and also please brief me about your comment
“Wow, that’s a lot of unformatted code” ..how should I format my code in what sense?

Thanks,
Medha

alex
alex
8 years ago

Wouldn’t it be better to retrieve the attempted password as a char[] like JPasswordField does, since generally speaking a String is less secure and accident prone? For non-Swing apps, would using the Scanner class to read input into a char[] be the best option?

Jerry Orr
8 years ago
Reply to  alex

“a String is less secure and accident prone”

How so?

“would using the Scanner class to read input into a char[] be the best option?”

I’m not sure what that buys you; Scanner is usually used for parsing text. In the case of a web application, you’d be getting the password parameter using something like request.getParameter(“password”), which gives you a String. Even if you’re using some framework like Spring MVC, it’ll be giving you a String anyway. I don’t know what would be gained by running that through a Scanner, which doesn’t even return a char[].

Jerry Orr
8 years ago
Reply to  Alex

Note that just using a char[] instead of a String doesn’t make anything more secure. You need to explicitly clear out the char[] after you’re done with it, or the password will be waiting for garbage collection just like a String. And if the password was _ever_ stored as a String (as is the case when using HttpServletRequest), you’ve gained nothing.

Alex
Alex
8 years ago

Here is my code, you don’t even need to use toCharArray() for PBEKeySpec since it takes a char[]. /** * Code used and modified from: http://www.javacodegeeks.com/2012/05/secure-password-storage-donts-dos-and.html */ public class PasswordEncryptionService { public boolean authenticate(char[] attemptedPassword, byte[] encryptedPassword, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { //Encrypt clear-text password using the same salt that wa used to encrypt original password byte[] encryptedAttemptedPassword = getEncryptedPassword(attemptedPassword, salt); //Authentication succeeds if value returned is True return Arrays.equals(encryptedPassword, encryptedAttemptedPassword); } public byte[] getEncryptedPassword(char[] password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { //Use PBKDF2 with SHA-1 for hashing algorithm String algorithm = “PBKDF2WithHmacSHA1”; //160 bit hash int derivedKeyLength =… Read more »

puneet
puneet
8 years ago

This is really a helpful article. However just wanted to understand if it is ok to have a SALT at application level rather than user level?

Jerry Orr
8 years ago
Reply to  puneet

Short version: application-level salt is a moderate improvement over no salt, but user-level is much better. You absolutely should have a user-level salt. Long version: An application-level salt is better than no salt at all, as it prevents someone from running your database through a precomputed rainbow table. However, a per-user salt makes it **much** more difficult to crack your users’ passwords, because a per-user salt makes the work done brute-forcing UserA’s password completely worthless when brute-forcing UserB’s password. If they have the same salt, then the results of brute-forcing UserA’s password can be stored and compared against UserB’s hash.… Read more »

Boniface
Boniface
8 years ago

Hi, Say I have an app with a login screen where user enters their credentials. According to your post, I should retrieve the saved salt and password hash from my servers then authenticate() them with the password the user entered? I’m I correct? This way, the user’s password never leaves his or her phone? Is it safe to make the password authenticating on the client_side(phone) rather than on my servers?

Jerry Orr
8 years ago
Reply to  Boniface

No, you definitely want to hash and authenticate on the server. If you authenticate on the client, how does the server know you aren’t just faking it? And even if you hash on the client and send the hash to the server, you effectively made the hash your password.

The client should send the plain-text password to the server (over TLS/https, of course), the server hashes the password, and compares the result to the stored hash. If they match, authentication succeeds.

Amar
Amar
7 years ago

Hi Jerry,
You recommended user level salt, what would be the salt for each user. I guess it would be different for every user. How do we generate/create/determine a salt for a new user ?

Also, once we have a salt and a hashed password, can we store them both in one table as one record ?

Jerry Orr
7 years ago
Reply to  Amar

Yes, each user should have their own salt, and the PasswordEncryptionService above has a generateSalt() method to do so. Here were my thoughts on where to store the salt: > There is a lot of conflicting information out there on whether the salts should be stored someplace separate from the encrypted passwords. Since the key stretching in PBKDF2 already protects us from brute-force attacks, I feel it is unnecessary to try to hide the salt. Section 3.1 of NIST SP 800-132 also defines salt as a “non-secret binary value,” so that’s what I go with. So yes, I consider it… Read more »

Amar
Amar
7 years ago
Reply to  Jerry Orr

Thanks Jerry for clarifying this. I am guessing the iteration number would be a static value stored in the application. We are in the estimate phase of this work. We will replace an existing hasing system with the new way and we have been told that we must use PBKDF2. In order to do that we have to support both old and new way for a wile for backward compatibility. It’s going to be bit complex until we have the new design in place and all the existing users are migrated (forcefully or naturally) to use the new way. We… Read more »

Jerry Orr
7 years ago
Reply to  Amar

Yes, you could store the iteration count as a static value, or even keep it in a column in the user table if you want the flexibility to increase it later on.

As for how to transition from one hash system to a better one, I described a phased approach I’ve used in a comment here: http://blog.jerryorr.com/2015/04/5-simple-rules-for-securely-storing.html?showComment=1428497414894#c8566036775552258292

Jerry Orr
7 years ago
Reply to  Jerry Orr

And if you have other questions in the future, my Twitter handle is @JerryOnJava

justme2h
justme2h
7 years ago

Great tutorial, thanks!

A little improvement: do not use Arrays.equals as it allows time attacks. Of course, this is not a big issue, but why the risk…

See “Why does the hashing code on this page compare the hashes in “length-constant” time?”
https://crackstation.net/hashing-security.htm

Back to top button