How do I validate user's password with PasswordEncryptor?

Category: org.jasypt, viewed: 2490 time(s).

Every application that you'll create may requires an authentication process. This authentication process will at least contains a process of checking user's login name and their password. To make the system reliable the password we usually stored the password in an encrypted form.

The BasicPasswordEncryptor which implements the PasswordEncryptor interface provide a BasicPasswordEncryptor.encryptPassword(String password) method for encrypting user's password. To check if the user's password is correct we use the BasicPasswordEncryptor.checkPassword(String plainText, String encryptedPassword) method.

package org.kodejava.example.jasypt;

import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.BasicPasswordEncryptor;

public class PasswordEncryptorDemo {
    public static void main(String[] args) {
        //
        // Creates an instance of BasicPasswordEncryptor.
        //
        PasswordEncryptor encryptor = new BasicPasswordEncryptor();

        //
        // Encrypted version of user password.
        //
        String encrypted = encryptor.encryptPassword("secret");
        System.out.println("encrypted = " + encrypted);
        
        //
        // Compare user's plain text password with the encrypted one to check
        // if they are match.
        //
        if (encryptor.checkPassword("secret", encrypted)) {
            System.out.println("Welcome to Jasypt");
        } else {
            System.out.println("Invalid secret word, access denied!");
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats