Monday, November 19, 2012

How to store the encrypted Password into Database?


How to encrypt the Password ?
How to encode the password?


    public static String encryptPassword(String password) {
         
           String encryptPassword = password;//getIt1().getValue().toString();
           byte[] unencodedPassword = encryptPassword.getBytes();
           MessageDigest md = null;
           try {
                md = MessageDigest.getInstance("MD5");
           } catch (Exception e) {
                   e.printStackTrace();
               }
           md.reset();
           md.update(unencodedPassword);
           byte[] encodedPassword = md.digest();
           StringBuffer buf = new StringBuffer();
           for (int i = 0; i < encodedPassword.length; i++) {
               if (((int) encodedPassword[i] & 0xff) < 0x10) {
               buf.append("0");
           }
                buf.append(Long.toString((int) encodedPassword[i] & 0xff, 16));
           }
            String passw=buf.toString();   
        
           return passw;
       }