package com.lxc.wmb; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import sun.misc.BASE64Encoder; @SuppressWarnings("restriction") public class MD5 { /**利用MD5进行加密*/ public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{ //确定计算方法 MessageDigest md5=MessageDigest.getInstance("MD5"); BASE64Encoder base64en = new BASE64Encoder(); //加密后的字符串 String newstr=base64en.encode(md5.digest(str.getBytes("utf-8"))); return newstr; } /**判断用户密码是否正确 *newpasswd 用户输入的密码 *oldpasswd 正确密码*/ public boolean checkpassword(String newpasswd,String oldpasswd) throws NoSuchAlgorithmException, UnsupportedEncodingException{ if(EncoderByMd5(newpasswd).equals(oldpasswd)) return true; else return false; } }
package com.lxc.wmb; import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class UpdatePwd { public static void main(String[] args) { String url = "jdbc:mysql://test1256.db.58dns.org:23384/db58_fbu_payment?useUnicode=true&characterEncoding=UTF-8&connectTimeout=60000&socketTimeout=60000"; String userName = "fbupay_admin"; String password = "7046e5127a93295c"; updatePwd(url, userName, password); } private static void updatePwd(String url, String userName, String password) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(url, userName, password); String sql = "SELECT ID,PASSWORD FROM FP_USER_BASE WHERE PASSWORD IS NOT NULL"; ps = conn.prepareStatement(sql); ps.executeQuery(); rs = ps.executeQuery(); List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); while(rs.next()){ long id = rs.getLong("id"); String pwd = rs.getString("password"); Map<String,Object> map = new HashMap<String, Object>(); map.put("id", id); map.put("pwd", pwd); list.add(map); } for (Map<String, Object> map : list) { long id = (Long) map.get("id"); String pwd = (String) map.get("pwd"); String newPwd = MD5.EncoderByMd5(pwd); String updateSql = "UPDATE FP_USER_BASE SET PASSWORD = '"+newPwd+"' WHERE ID = "+id; ps.executeUpdate(updateSql); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }finally{ try { rs.close(); ps.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }