SHA1加密解密 public static String jmSha1Key() throws Exception { String jm_key = Sha1Util.encryptToSHA(MD5.md5("Y@WKyzcB9LP7%SEy")); return jm_key; }
public class Sha1Util { public static String getSecurityAppKey() { return ""; } // 使用方法按照getSecurityAppKey方法使用,将要加密的字串写到encryptToSHA中即可! public static String encryptToSHA(String info) { byte[] digesta = null; try { MessageDigest alga = MessageDigest.getInstance("SHA-1"); alga.update(info.getBytes()); digesta = alga.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } String rs = byte2hex(digesta); return rs; } public static String byte2hex(byte[] b) { String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (Integer.toHexString(b[n] & 0XFF)); if (stmp.length() == 1) { hs = hs + "0" + stmp; } else { hs = hs + stmp; } } return hs; } }
MD5工具
public class MD5 { /** * 对外提供getMD5(String)方法 * * @author randyjia */ public static String getMD5(String val) throws NoSuchAlgorithmException { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(val.getBytes()); byte[] m = md5.digest();//加密 return getString(m); } private static String getString(byte[] b) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < b.length; i++) { sb.append(b[i]); } return sb.toString(); } public static String md5(String string) { byte[] hash; try { hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Huh, MD5 should be supported?", e); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Huh, UTF-8 should be supported?", e); } StringBuilder hex = new StringBuilder(hash.length * 2); for (byte b : hash) { if ((b & 0xFF) < 0x10) hex.append("0"); hex.append(Integer.toHexString(b & 0xFF)); } return hex.toString(); } public static String md5MM(String args) { Calendar c = Calendar.getInstance();// int mYear = c.get(Calendar.YEAR); // 获取当前年份 int mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份 int mDay = c.get(Calendar.DAY_OF_MONTH);// 获取当日期 int mWay = c.get(Calendar.DAY_OF_WEEK);// 获取当前日期的星期 int mHour = c.get(Calendar.HOUR_OF_DAY);//时 int mMinute = c.get(Calendar.MINUTE);//分 // int ms = c.get(Calendar.) return mYear + "" + mMonth + "" + mDay + "" + mHour + "" + mMinute + "" + mWay + ""; } }