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加密解密的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明2. CSS hack用法案例詳解3. ASP 處理JSON數(shù)據(jù)的實(shí)現(xiàn)代碼4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. asp中response.write("中文")或者js中文亂碼問(wèn)題7. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息
