DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Password Encryption and Decryption Using jBCrypt

Looking to create a great password for your sensitive files? Read on to learn how to generate a random password and and how to hash that password.

Dhiraj Ray user avatar by
Dhiraj Ray
·
May. 20, 17 · Tutorial
Like (1)
Save
Tweet
Share
45.67K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will take a look into how to generate random passwords that have alphanumeric and special characters and encrypt it using the one-way hash algorithm, jBCrypt.

Generating a Random Password

I'll use the Passay library to generate random passwords having alphanumeric and special characters. The following code block is a sample code to generate a random password using the Passay library. It also allows you to configure the character length of the resultant password.

public String generateRandomPassword() {

List rules = Arrays.asList(new CharacterRule(EnglishCharacterData.UpperCase, 1),
new CharacterRule(EnglishCharacterData.LowerCase, 1), new CharacterRule(EnglishCharacterData.Digit, 1),new CharacterRule(EnglishCharacterData.Special, 1));

PasswordGenerator generator = new PasswordGenerator();
String password = generator.generatePassword(8, rules);
return password;
}

You can find more reference example of using Passay here - Random Password Generator

Once any random alphanumeric password is generated, we'll use jBCrypt to encode it.

Password Hashing Using jBCrypt

jBcrypt is a one-way password hashing algorithm based on the Blowfish cipher that uses an adaptive hash algorithm to store passwords. BCrypt internally generates a random salt while encoding passwords and hence it provides a different encoded result for the same string. But one common thing is that every time it generates a String of length 60.

The following code is the implementation to encode a string using jBCrypt:

private String hashPassword(String plainTextPassword){
    return BCrypt.hashpw(plainTextPassword, BCrypt.gensalt());
} 

Once the password is hashed we can save it to DB and whenever there is a need to match the plain text password with this hashed password saved into the DB, we can do the following:

private void checkPass(String plainPassword, String hashedPassword) {
if (BCrypt.checkpw(plainPassword, hashedPassword))
System.out.println("The password matches.");
else
System.out.println("The password does not match.");
}

Storing plain-text password in DB is always vulnerable to security. Hence, we can use the above implementations to save a hashed password to the database instead of saving a plain text password.

Plain text

Published at DZone with permission of Dhiraj Ray. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • Comparing Map.of() and New HashMap() in Java
  • 10 Best Ways to Level Up as a Developer
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: