MD5加密实战

    技术2022-08-01  88

     

    MTD5加密是为了防止在数据库操作时,MBA(数据库管理员)直接获取用户的密码导致信息的泄露而采用的一种加密的方法;下面以登录系统为例进行说明:

    Main方法:

    package com.jd.test; import java.util.Scanner; import java.util.UUID; import com.jd.tool.MD5Tool; import com.jd.tool.db.DBLink; public class Main { public static void main(String[] args) { System.out.println("*********************************"); System.out.println("*\t\t\t\t*"); System.out.println("*\t欢迎使用学生信息管理系统\t\t*"); System.out.println("*\t\t\t\t*"); System.out.println("*********************************"); while (true) { menu(); } } static void menu() { System.out.println("1、注册"); System.out.println("2、登录"); System.out.println("3、退出");//地址传递 System.out.println("请输入操作,以Enter键结束:"); Scanner scanner = new Scanner(System.in); int option = scanner.nextInt(); switch (option) { case 1:{ String userName=scanner.next(); System.out.println("请输入想要设定的密码"); String password=scanner.next(); System.out.println("请再次输入设定的密码"); String repassword=scanner.next(); String sql="select id from user_info where user_Name =? "; if(new DBLink().exist(sql, userName)) { System.out.println("用户名被占用,重新输入"); return; } if(!password.equals(repassword)) { System.out.println("两次的密码不一致,操作终止"); return; } String id=UUID.randomUUID().toString();//获取一个UUID sql="insert into user_info (id,user_name,password) values('"+id+"','"+userName+"','"+password+"')"; if(new DBLink ().exist(sql)) { System.out.println("注册成功"); return; } System.out.println("注册失败"); break; } case 2:{ System.out.println("请输入用户名"); String userName=scanner.next(); System.out.println("请输入密码"); String password=scanner.next(); password=MD5Tool.encrypt(password); String sql="select id from user_info where user_name=? and password=?"; if(new DBLink().exist(sql,userName,password)) { System.out.println("登陆成功!"); return; } System.out.println("用户名或者密码错误,登陆失败!"); break; } case 3:{ System.out.println("已经退出系统"); System.exit(0); break; } default: System.out.println("I'm Sorry,there is not the "+option+" option,please try again."); } } }

     工具目录下(tool):

    MD5Tool.java:

    package com.jd.tool; import java.math.BigInteger; import java.security.MessageDigest; public class MD5Tool { public static String encrypt(String password) { byte[] bytes = null; try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.update(password.getBytes());//加密 bytes = messageDigest.digest();//获得加密结果 } catch (Exception e) { e.printStackTrace(); } String result = new BigInteger(1, bytes).toString(16);// 将加密后的数据转换为16进制数字 // 生成数字未满32位,则前面补0 for (int i = 0; i < 32 - result.length(); i++) { result = "0" + result; } return result; } }

    PropertiesTool.java:

    package com.jd.tool; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesTool { private static Properties properties = new Properties(); static { InputStream inputStream = PropertiesTool.class.getClassLoader().getResourceAsStream("db.properties");//将db.properties变为javaIO流对�? try { properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static String getValue(String key) { return properties.getProperty(key); } }

    tool.db目录下:

    DBLink.java 工具:

    package com.jd.tool.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; import com.jd.tool.PropertiesTool; /** * 数据库管理工具类 * * @author xjk */ public class DBLink { private Logger logger = Logger.getLogger(DBLink.class); /** * 获取数据库连接 * * @author xjk */ private Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 String userName = PropertiesTool.getValue("db.username"); String password = PropertiesTool.getValue("db.password"); String url = PropertiesTool.getValue("db.url"); return DriverManager.getConnection(url, userName, password);//获取连接 } catch (Exception e) { logger.debug(e.getMessage(), e); } return null; } /** * 判断SQL语句是否能查出数据 * * @author xjk */ public boolean exist(String sql) { Connection connection = null; Statement statement =null; ResultSet resultSet=null; try { connection = getConnection();//获取连接 statement = connection.createStatement(); resultSet= statement.executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中 return resultSet.next(); } catch (Exception e) { logger.debug(e.getMessage(), e); }finally { close(resultSet,statement,connection); } return false; } /** * 判断SQL语句是否能查出数据 * * @author xjk */ public boolean exist(String sql, Object ...params) { Connection connection = null; PreparedStatement preparedStatement =null; ResultSet resultSet=null; try { connection = getConnection();//获取连接 preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { preparedStatement.setObject(i+1, params[i]); } resultSet= preparedStatement.executeQuery();//执行sql,将查询的数据存到ResultSet类型的变量中 return resultSet.next(); } catch (Exception e) { logger.debug(e.getMessage(), e); }finally { close(resultSet,preparedStatement,connection); } return false; } /** * 查询数据 * * @author xjk */ public void select(String sql,IRowMapper rowMapper) {//接口无法创建对象,所以rowMapper参数一定指向IRowMapper接口实现类对象 Connection connection = null; Statement statement =null; ResultSet resultSet=null; try { connection = getConnection();//获取连接 statement = connection.createStatement(); resultSet= statement.executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中 rowMapper.rowMapper(resultSet);//因为rowMapper参数指向IRowMapper接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法 多态 } catch (Exception e) { logger.debug(e.getMessage(), e); }finally {//释放资源 close(resultSet,statement,connection); } } /** * 查询数据 * * @author xjk */ public void select(String sql,IRowMapper rowMapper,Object ...params) { Connection connection = null; PreparedStatement preparedStatement =null; ResultSet resultSet=null; try { connection = getConnection();//获取连接 preparedStatement= connection.prepareStatement(sql);//含有?号占位符的sql for (int i = 0; i < params.length; i++) { preparedStatement.setObject(i+1, params[i]);//为?号赋值 } resultSet= preparedStatement.executeQuery();//执行sql rowMapper.rowMapper(resultSet);//因为rowMapper参数指向IRowMapper接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法 多态 } catch (Exception e) { logger.debug(e.getMessage(), e); }finally {//释放资源 close(resultSet,preparedStatement,connection); } } /** * 修改(insert、update和delete)数据 * * @author xjk */ public boolean update(String sql) { Connection connection = null; Statement statement = null; try { connection = getConnection();//获取数据库连接对象,一个对象表示一次数据库连接 statement = connection.createStatement();//获取Statement对象 int result = statement.executeUpdate(sql);//执行sql语句,返回受影响的行数,仅限于数据insert、update和delete return result>0;//处理结果 } catch (Exception e) { logger.debug(e.getMessage(), e); }finally {//即便有异常也会执行代码 close(statement,connection); } return false; } /** * 修改(insert、update和delete)数据 * * @author xjk */ public boolean update(String sql,Object ...params) { Connection connection= null; PreparedStatement preparedStatement= null; try { connection= getConnection(); preparedStatement= connection.prepareStatement(sql);//含有?号占位符的sql for (int i = 0; i < params.length; i++) { preparedStatement.setObject(i+1, params[i]);//为?号赋值 } return preparedStatement.executeUpdate()>0; } catch (Exception e) { logger.debug(e.getMessage(), e); }finally { close(preparedStatement,connection); } return false; } /** * 释放资源 * * @author xjk */ private void close(Statement statement,Connection connection) { try { if(statement!=null) {//有可能由于异常导致statement没有赋值,比如url出错 statement.close(); } } catch (SQLException e) { logger.debug(e.getMessage(), e); } try { if(connection!=null) { connection.close(); } } catch (SQLException e) { logger.debug(e.getMessage(), e); } } /** * 释放资源 * * @author xjk */ private void close(ResultSet resultSet,Statement statement,Connection connection) {//重载 try { if(resultSet!=null) { resultSet.close(); } } catch (SQLException e) { logger.debug(e.getMessage(), e); } close(statement,connection); } }

    IRowMapper.java:

    package com.jd.tool.db; import java.sql.ResultSet; public interface IRowMapper { void rowMapper(ResultSet rs); }

     

    Processed: 0.012, SQL: 9