標籤:catch ges substring ati deb color case 編碼 tin
提供了基於MD5加密16位和32位的方法
1 import java.io.IOException; 2 import java.math.BigInteger; 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 import sun.misc.BASE64Decoder; 6 import sun.misc.BASE64Encoder; 7 /** 8 * <p>標題: 編碼工具類</p> 9 * <p>功能: 對資料進行編碼轉換</p> 10 * 趙力 11 */ 12 public class EncodeUtil 13 { 14 public static void main(String[] args) throws Exception 15 { 16 System.out.println(md5Encrypt16("需要進行MD5加密的字串")); 17 } 18 19 /** 20 * MD5加密16位 21 * @param encryptStr 要加密資料 22 * @return 返回16位加密結果 23 * ZhaoLi 24 */ 25 public static String md5Encrypt16(String encryptStr) 26 { 27 return md5Encrypt32(encryptStr).substring(8, 24); 28 } 29 30 /** 31 * MD5加密32位 32 * @param encryptStr 要加密資料 33 * @return 32位加密結果 34 * ZhaoLi 35 */ 36 public static String md5Encrypt32(String encryptStr) 37 { 38 MessageDigest md5; 39 try 40 { 41 md5 = MessageDigest.getInstance("MD5"); 42 byte[] md5Bytes = md5.digest(encryptStr.getBytes()); 43 StringBuffer hexValue = new StringBuffer(); 44 for (int i = 0; i < md5Bytes.length; i++) 45 { 46 int val = (md5Bytes[i]) & 0xff; 47 if (val < 16) 48 { 49 hexValue.append("0"); 50 } 51 hexValue.append(Integer.toHexString(val)); 52 } 53 return hexValue.toString().toLowerCase(); 54 } catch (Exception e) 55 { 56 throw new RuntimeException(e); 57 } 58 } 59 60 /** 61 * 結合base64實現md5加密 62 * @param msg 待加密字串 63 * @return 擷取md5後轉為base64 64 * @throws Exception 65 */ 66 public static String md5EncryptBase64(String msg) throws Exception 67 { 68 return msg == null ? null : base64Encode(md5(msg)); 69 } 70 71 /** 72 * 將byte[]轉為各種進位的字串 73 * @param bytes byte[] 74 * @param radix 可以轉換進位的範圍,從Character.MIN_RADIX到Character.MAX_RADIX,超出範圍後變為10進位 75 * @return 轉換後的字串 76 */ 77 public static String binary(byte[] bytes, int radix) 78 { 79 return new BigInteger(1, bytes).toString(radix);// 這裡的1代表正數 80 } 81 82 /** 83 * base 64 encode 84 * @param bytes 待編碼的byte[] 85 * @return 編碼後的base 64 code 86 */ 87 public static String base64Encode(byte[] bytes) 88 { 89 return new BASE64Encoder().encode(bytes); 90 } 91 92 /** 93 * base 64 decode 94 * @param base64Code 待解碼的base 64 code 95 * @return 解碼後的byte[] 96 * @throws Exception 97 */ 98 public static byte[] base64Decode(String base64Code) 99 {100 try101 {102 return base64Code == null ? null : new BASE64Decoder().decodeBuffer(base64Code);103 } catch (IOException e)104 {105 throw new RuntimeException("報錯內容", e);106 }107 }108 109 /** 110 * 擷取byte[]的md5值 111 * @param bytes byte[] 112 * @return md5 113 * @throws Exception 114 */115 public static byte[] md5(byte[] bytes)116 {117 MessageDigest md;118 try119 {120 md = MessageDigest.getInstance("MD5");121 } catch (NoSuchAlgorithmException e)122 {123 throw new RuntimeException("報錯內容", e);124 }125 md.update(bytes);126 return md.digest();127 }128 129 /** 130 * 擷取字串md5值 131 * @param msg 132 * @return md5 133 * @throws Exception 134 */135 public static byte[] md5(String msg)136 {137 return msg == null ? null : md5(msg.getBytes());138 }139 }
基於Java語言的MD5加密Base64轉換方法