-3

I'm writing a program that one can register on a web page in java and the details be stored in a mysql database. Can someone help me on how to write a code that encrypts the passwords using md5 and also how to parse a date string and store this date in the database.

VMai
  • 10,156
  • 9
  • 25
  • 34
  • What code have you got so far? What exactly are you struggling with? – Andrew Stubbs Jul 23 '14 at 10:05
  • 1
    Do some research first!!! – Harish Talanki Jul 23 '14 at 10:06
  • 1
    Welcome at SO! Please show your efforts, what you've done so far and where you got stuck. By the way the [Function and Operator Reference](http://dev.mysql.com/doc/refman/5.6/en/func-op-summary-ref.html) will be a great help. – VMai Jul 23 '14 at 10:09
  • Post full code. For MD5 - refer this SO post http://stackoverflow.com/questions/415953/generate-md5-hash-in-java and for parsing date string refer Javadoc of SimpleDateFormat here http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Pat Jul 23 '14 at 10:19

1 Answers1

0

To get MD5 from a String instance use MessageDigest class. Assume your password is stored in passwordString variable.

MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] passwordMD5Digest = messageDigest.digest(passwordString.getBytes("UTF-8"));

To parse string to date use SimpleDateFormat class.

final String datePattern = "dd/MM/yyyy"; // format is day/month/year
String dateString2Parse = "25/12/2004"; // date to parse

SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
Date date = sdf.parse(dateString2Parse);

Date is java.util.Date class.


UPDATE

Below, there's your code, updated to implement the way I described:

public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    System.out.println(crypt("password"));
}

public static String crypt(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    if (str == null || str.length() == 0) {
        throw new IllegalArgumentException("String to encrypt cannot be null or zero length");
    }
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] hash = md.digest(str.getBytes("UTF-8"));
    return toHexString(hash);
}

/**
 * Converts a byte array to hex string
 */
public static String toHexString(byte[] block) {
    StringBuffer buf = new StringBuffer();
    char[] hexChars = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F'};
    int len = block.length;
    int high = 0;
    int low = 0;
    for (int i = 0; i < len; i++) {
        high = ((block[i] & 0xf0) >> 4);
        low = (block[i] & 0x0f);
        buf.append(hexChars[high]);
        buf.append(hexChars[low]);
    }
    return buf.toString();
}  
  • private static final long serialVersionUID = 1L; public static String crypt(String str){ if(str==null||str.length()==0){ throw new IllegalArgumentException("String to encrypt cannot be null or zero length"); } StringBuffer hexString=new StringBuffer(); try{ MessageDigest md=MessageDigest.getInstance("MD5"); md.update(str.getBytes()); byte[] hash=md.digest(); for(int i=0;i – Felix Otieno Odhiambo Jul 23 '14 at 10:32
  • debug your code, and you will see that your if condition does not work properly. The code I provided works properly and you get byte[] array of md5 hash. –  Jul 23 '14 at 10:39
  • @FelixOtienoOdhiambo check the update for my answer. –  Jul 23 '14 at 11:13