Java實(shí)現(xiàn)md5和base64加密解密的示例代碼
import java.io.IOException;import java.security.MessageDigest;import sun.misc.BASE64Encoder;import sun.misc.BASE64Decoder;public class MD5Util { /** * MD5加密 */ public static String md5Encryption(String str) { MessageDigest md5 = null; try {md5 = MessageDigest.getInstance('MD5'); } catch (Exception e) {System.out.println(e.toString());e.printStackTrace();return ''; } char[] charArray = str.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++)byteArray[i] = (byte) charArray[i]; byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) {int val = ((int) md5Bytes[i]) & 0xff;if (val < 16)hexValue.append('0');hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } /** * base64加密 */ public static String base64Encryption(String str) { if (str == null) return null; String encodeStr = '';try { BASE64Encoder b64Encoder = new BASE64Encoder(); encodeStr = b64Encoder.encode(str.getBytes()); } catch (Exception e) { e.printStackTrace(); } return encodeStr; } /** * base64解密 */ public static String base64Dcrypt(String str) { if (str == null) return null; String decoderStr = '';try { BASE64Decoder decoder = new BASE64Decoder(); byte[] b = decoder.decodeBuffer(str); decoderStr = new String(b); } catch (IOException e) { e.printStackTrace(); return null; }return decoderStr; }}
以上就是Java實(shí)現(xiàn)md5和base64加密解密的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java md5和base64加密解密的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. HTTP協(xié)議常用的請求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))2. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)3. XML入門的常見問題(三)4. React優(yōu)雅的封裝SvgIcon組件示例5. HTML5 Canvas繪制圖形從入門到精通6. Vue如何使用ElementUI對表單元素進(jìn)行自定義校驗(yàn)及踩坑7. 不要在HTML中濫用div8. XML在語音合成中的應(yīng)用9. CSS清除浮動方法匯總10. HTML DOM setInterval和clearInterval方法案例詳解
