Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a Secure Random Password in Java with Minimum Special Character Requirements

How do I create a random password that meets the system's length and character set requirements in Java?

I have to create a random password that is 10-14 characters long and has at least one uppercase, one lowercase, and one special character. Unfortunately, some special characters are too special and cannot be used, so I cannot use just printed ASCII.

Many of the examples on this site generate a random password or session key without enough entropy in the characters or without realistic requirements in a business setting like the ones given above, so I'm asking more pointed question to get a better answer.

My character set, every special character on a standard US keyboard except for a space:

A-Z
a-z
0-9
~`!@#$%^&*()-_=+[{]}\|;:'",<.>/?
like image 637
summer Avatar asked Jul 07 '15 05:07

summer


People also ask

How to generate random passwords in Java?

Here is a simple algorithm that I am using to generate random passwords in Java. For online password generator check the password generator. The code is based on a dictionary of characters, numbers and specials characters. Code. The code is easy to be changed in order to extend the dictionary or to changed how the random characters are chosen.

What are the conditions for generating random password?

Following example generates a random password adhering to following conditions − It should contain at least one capital case letter. It should contain at least one lower-case letter. It should contain at least one number. Length should be 8 characters. It should contain one of the following special characters: @, $, #, !.

How to generate a secure random alphanumeric string in Java?

We will be using ASCII Table to get Special Characters by Decimal Value in java. Take a look at below mapping between Decimal Value and Characters. Create public method CrunchifyRandomPasswordGenerator () in which we will get ASCII decimal values in 3 different loops.

How to generate a cryptographically strong alphanumeric password in Java?

This post will discuss how to generate a cryptographically strong random alphanumeric password of the desired length in Java. 1. Using SecureRandom.nextInt(…) + StringBuilder. A simple solution is to randomly choose characters from the defined ASCII range and construct a string of the desired length out of it.


1 Answers

I suggest using apache commons RandomStringUtils. Use something what is already done.

String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?";
String pwd = RandomStringUtils.random( 15, characters );
System.out.println( pwd );

If you are using maven

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

otherwise download jar

UPDATE Version with secure random. So matter of required characters left and can be solved as in comment, generate required parts separately and normal ones. Then join them randomly.

char[] possibleCharacters = (new String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?")).toCharArray();
String randomStr = RandomStringUtils.random( randomStrLength, 0, possibleCharacters.length-1, false, false, possibleCharacters, new SecureRandom() );
System.out.println( randomStr );
like image 71
Robert Wadowski Avatar answered Sep 22 '22 13:09

Robert Wadowski