Create a strong encrypted password in Java : In almost every application we need to create a strong encrypted password. There are many encryption algorithms to create password. It is good approach to have a strong salt and on the basis of that salt create a encrypted password. Here I will show you how you can create a very strong encrypted password using the SHA algorithm with the help of salt.
Example of password encryption : Here is the sample example to create a encrypted password using SHA-384 Algorithm.
PasswordEncoder.java
public class PasswordEncoder{
public static String getSalt(){
String uuid = UUID.randomUUID().toString();
return salt;
}
public static String getEncryptedPassword(String salt, String password){
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-384");
md.update(salt.getBytes());
byte[] bytes = md.digest(password.getBytes());
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++){
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
return generatedPassword;
}
public static void main(String args[]){
System.out.println("password is : " + PasswordEncoder.getEncryptedPassword(getSalt(), "user1234"));
}
}
The above code showing that how the PasswordEncoder class encrypting the password using the SHA-384 algorithm. First of all getSalt() method is creating the salt which is randomly generated using the UUID class. Look at the second method getEncryptedPassword() which is then taking the salt and raw password as a parameter and generate the strong encrypted password. The output of the program is as follows:
password is :57cccf4c31c28bf4d287ae64ed28d00aec68b11749b01de1f5ec61b3007cd004e1871296934d2c1a310c8b3915f0eca9
This is very long password generated by the algorithm which is very strong and hard to break.
0 Comment(s)