Friday, 9 August 2013

How to encrypt password in java

This is one of the method for encrypt the password using MD5 and using this method you can encrypt but you can not decrypt the password .

 import java.security.MessageDigest;  
 public class PasswordEncryptDemo {  
      public static String Password(String plainText) throws Exception  
      {  
           MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5");  
           mdAlgorithm.update(plainText.getBytes());  
           byte[] digest = mdAlgorithm.digest();  
           StringBuffer hexString = new StringBuffer();  
           for (int i = 0; i < digest.length; i++)  
           {  
             plainText = Integer.toHexString(0xFF & digest[i]);  
             if (plainText.length() < 2)   
             {  
               plainText = "0" + plainText;  
             }  
             hexString.append(plainText);  
           }  
           return hexString.toString();  
      }  
      public static void main(String args[])throws Exception  
      {  
           String planinText="poolof123";  
           System.out.println("The plain text is:"+planinText);  
           System.out.println("The encrypted text is:"+PasswordEncryptDemo.Password(planinText));  
      }  
 }  

output:
 The plain text is:poolof123  
 The encrypted text is:15e3c36a6a68803819c5d25a053b5535  

No comments:

Post a Comment