1 /** 2 * SHA-1加密 3 * @param strSrc 要加密的字符串 4 * @return 加密后的字符串 5 */ 6 public static String SHAEncrypt(String strSrc) { 7 MessageDigest md = null; 8 String strDes = null; 9 byte[] bt = strSrc.getBytes(); 10 try { 11 md = MessageDigest.getInstance("SHA-1"); 12 md.update(bt); 13 strDes = bytes2Hex(md.digest()); 14 } catch (NoSuchAlgorithmException e) { 15 System.out.println("Invalid algorithm."); 16 return null; 17 } 18 return strDes; 19 } 20 21 public static String bytes2Hex(byte[] bts) { 22 String des = ""; 23 String tmp = null; 24 for (int i = 0; i < bts.length; i++) { 25 tmp = (Integer.toHexString(bts[i] & 0xFF)); 26 if (tmp.length() == 1) { 27 des += "0"; 28 } 29 des += tmp; 30 } 31 return des; 32 } 33 //演示 34 @Test 35 public void demo(){ 36 System.out.println(SHAEncrypt("1223344")); 37 }
输出结果: a3547da48ed532585429eae800ac93147552d63a