md5上的Java jdbc和将日期字符串解析为mysql

md5上的Java jdbc和将日期字符串解析为mysql,java,mysql,Java,Mysql,我正在编写一个程序,可以用java在网页上注册,详细信息存储在mysql数据库中。有人可以帮助我如何编写使用md5加密密码的代码,以及如何解析日期字符串并将此日期存储在数据库中。要从字符串实例获取md5,请使用类。假设您的密码存储在passwordString变量中 MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] passwordMD5Digest = messageDigest.digest(pass

我正在编写一个程序,可以用java在网页上注册,详细信息存储在mysql数据库中。有人可以帮助我如何编写使用md5加密密码的代码,以及如何解析日期字符串并将此日期存储在数据库中。

要从
字符串
实例获取md5,请使用类。假设您的密码存储在
passwordString
变量中

MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] passwordMD5Digest = messageDigest.digest(passwordString.getBytes("UTF-8"));
要分析到日期的字符串,请使用类

Date
是类


更新

下面是您的代码,按照我描述的方式进行了更新:

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();
}  
publicstaticvoidmain(字符串[]args)抛出NoSuchAlgorithmException、UnsupportedEncodingException{
System.out.println(密码);
}
publicstaticstringcrypt(stringstr)抛出nosuchagorithmexception,UnsupportedEncodingException{
如果(str==null | | str.length()==0){
抛出新的IllegalArgumentException(“要加密的字符串不能为null或零长度”);
}
MessageDigest md=MessageDigest.getInstance(“MD5”);
byte[]hash=md.digest(str.getBytes(“UTF-8”);
返回到hextstring(散列);
}
/**
*将字节数组转换为十六进制字符串
*/
公共静态字符串到十六进制字符串(字节[]块){
StringBuffer buf=新的StringBuffer();
字符[]十六进制字符={
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
‘A’、‘B’、‘C’、‘D’、‘E’、‘F’};
int len=block.length;
int高=0;
int低=0;
对于(int i=0;i>4);
低=(块[i]&0x0f);
buf.append(六角字符[高]);
buf.append(hexChars[low]);
}
返回buf.toString();
}  

到目前为止,您获得了哪些代码?你到底在挣扎什么?先做些调查!!!欢迎光临!请展示你的努力,到目前为止你做了什么,你在哪里遇到了困难。顺便说一下,这将是一个很大的帮助。张贴完整的代码。对于MD5-请参阅此SO post,对于解析日期字符串,请参阅此处的SimpleDataFormat的Javadoc private static final long serialVersionUID=1L;公共静态字符串密码(String str){if(str==null | | str.length()==0){抛出新的IllegalArgumentException(“要加密的字符串不能为null或零长度”);}StringBuffer hexString=new StringBuffer();try{MessageDigest md=MessageDigest.getInstance(“MD5”);md.update(str.getBytes());byte[]哈希=md.digest();for(int i=0;idebug您的代码,您将看到if条件工作不正常。我提供的代码工作正常,您将获得md5哈希的byte[]数组。@FelixOtienoOdhiambo检查更新以获取我的答案。
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();
}