Base64是一種任意二進制到文本字符串的編碼方法,經常使用於在URL、Cookie、網頁中傳輸少許二進制數據。java
http://hw1287789687.iteye.com/blog/1910853 apache
commons-codec.jar 的下載地址:app
http://commons.apache.org/proper/commons-codec/download_codec.cgi ui
http://blog.csdn.net/jingdian14/article/details/7800524 編碼
依賴1加密
方法一:
spa
public static String MD5(String str) { try { byte[] bytesOfMessage = str.getBytes("UTF-8"); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] thedigest = md.digest(bytesOfMessage); StringBuffer sb = new StringBuffer(); for (int i = 0; i < thedigest.length; ++i) { sb.append(Integer.toHexString((thedigest[i] & 0xFF) | 0x100).substring(1,3)); } return sb.toString(); } catch(Exception e) { return null; } }
方法二:.net
public static String md5(String str){ StringBuilder mess = new StringBuilder(); try { MessageDigest md = MessageDigest.getInstance("MD5");//獲取加密器,方式是Md5 byte[] bytes = str.getBytes(); byte[] digest = md.digest(bytes); for (byte b : digest){ int d = b & 0xff;//把每一個字節轉成16進制數 String hexString = Integer.toHexString(d); if (hexString.length() == 1) {//字節的高4位爲0 hexString = "0" + hexString; } mess.append(hexString);//把每一個字節對應的2位十六進制數當成字符串拼接一塊兒 } } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mess + ""; }