get Encrypted Password - Java Security

Java examples for Security:Password

Description

get Encrypted Password

Demo Code


import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String password = "java2s.com";
        System.out.println(getEncryptedPassword(password));
    }//w ww.  j a  v a2s . c om
    private static final Logger LOGGER = Logger
            .getLogger(CommonUtils.class);
    public static final String getEncryptedPassword(String password) {
        String sha1 = null;
        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(password.getBytes("UTF-8"));
            sha1 = new BigInteger(1, crypt.digest()).toString(16);

        } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
            LOGGER.error("Can not encrypt password", ex);
        }
        return sha1;
    }
}

Related Tutorials