加密算法
private static final byte[] DES_KEY = { 21, 1, -110, 82, -32, -85, -128, -65 }; @SuppressWarnings("restriction")
//加密算法 public static String encryptBasedDes(String data) { String encryptedData = null; try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(DES_KEY); // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(deskey); // 加密对象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key, sr); // 加密,并把字节数组编码成字符串 encryptedData = new sun.misc.BASE64Encoder().encode(cipher.doFinal(data.getBytes())); } catch (Exception e) { // log.error("加密错误,错误信息:", e); throw new RuntimeException("加密错误,错误信息:", e); } return encryptedData; }
//解密算法 @SuppressWarnings("restriction") public static String decryptBasedDes(String cryptData) { String decryptedData = null; try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(DES_KEY); // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(deskey); // 解密对象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key, sr); // 把字符串进行解码,解码为为字节数组,并解密 decryptedData = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cryptData))); } catch (Exception e) { throw new RuntimeException("解密错误,错误信息:", e); } return decryptedData;
}
//注册功能 @RequestMapping(value = "/register") public ModelAndView register(Management management) { ModelAndView mv = new ModelAndView(); Management management1 =managementRepository.findByManagementPhone(management.getManagementPhone()); List<Management> management2 = managementRepository.findAll(); int lastpage=management2.size()/8; if (management1==null){ management.setManagementPass(encryptBasedDes(management.getManagementPass())); managementRepository.save(management); } else { management.setManagementID(management1.getManagementID()); management.setManagementPass(encryptBasedDes(management.getManagementPass())); managementRepository.save(management); } return new ModelAndView("redirect:/management/show?page="+lastpage); }
//登录 @RequestMapping(value = "/loginManagement") public ModelAndView login(Management management, HttpSession sessoin) { ModelAndView mv = new ModelAndView(); //判断所输入账号密码是否匹配 //如果匹配成功 management.setManagementPass(encryptBasedDes(management.getManagementPass())); if (managementService.verifyManagement(management)) { //通过名字和密码找到该Management,并存进session Management management1=managementRepository.findByManagementPhoneAndAndManagementPass(management.getManagementPhone(), management.getManagementPass()).get(0); sessoin.setAttribute("management", management1); //测试从session获取该Management判定权限 Management test=(Management)sessoin.getAttribute("management"); switch (management1.getManagementRole()){ case "1": mv.setViewName("");break; case "2": case "3": mv.setViewName("");break; } mv.setViewName("/success.btl"); return mv; } //如果匹配不成功 else { mv.setViewName("/login.btl"); return mv; }