Q:

Encryption of Password in Java or MySQL? 

I am trying to encrypt the password using MD5 in user registration and matching the same during logging in.. I am using Mysql as my database.. Mysql too offers an option to convert password into MD5(ie MD5(password)).. I want to know which is the better way to encrypt it.. is it using java code or mysql query..
Thanks in advance. 

A:

Don't store passwords.  If it's ever sitting on a disk, it can be
  stolen.  Instead, store password hashes.  Use the right hashing
  algorithm, like SHA256, BCrypt or Salt the hash. 

Here are some useful links you must see:

How to best store user information and user login and password
Best practices for storing database passwords
Password Management Best Practices (soup to nuts, not just storage or generation)
Salting Your Password: Best Practices? 
Is it ever ok to store password in plain text in a php variable or php constant? 
Fundamental difference between Hashing and Encryption algorithms
http://www.jasypt.org/howtoencryptuserpasswords.html
http://www.codinghorror.com/blog/2007/09/youre-probably-storing-passwords-incorrectly.html
Java - Hash algorithms - Fastest implementations
Fast MD5 Implementation in Java
https://security.stackexchange.com/questions/12009/definitely-safest-password-storage-scheme

Hope this helps. 

A:

Since logically your application and database can exist in two separate physical boxes, it is more safe an option if you encrypt your password in application layer and then move it to database. 
From application design point of view you should consider encryption, hashing your password at a very early level so that from layer to layer your password doesnt move as plain string. 
Offtopic:
MD5 is prone to collision attack, you would consider using salt like appending email or dynamically generated values to prevent Rainbow Table. 
definitely-safest-password-storage-scheme

A:

Here is the algorithm I use to crypt with MD5.It returns your crypted output. 
   public class CryptWithMD5 {
   private static MessageDigest md;

   public static String cryptWithMD5(String pass){
    try {
        md = MessageDigest.getInstance("MD5");
        byte[] passBytes = pass.getBytes();
        md.reset();
        byte[] digested = md.digest(passBytes);
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<digested.length;i++){
            sb.append(Integer.toHexString(0xff & digested[i]));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(CryptWithMD5.class.getName()).log(Level. SEVERE, null, ex);
    }
        return null;

   }
}

You cannot decrypt MD5, but you can compare outputs since if you put the same string in this method it will return the same crypted output. If you want to decrypt you need to use the SHA.You will never use decription for a users password. For that always use MD5.That exception is pretty redundant. It will never throw it. When you add the password to the database, you add what this method returns. To login you compare them.