Java實現(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實現(xiàn)md5和base64加密解密的示例代碼的詳細內(nèi)容,更多關(guān)于Java md5和base64加密解密的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. java——Byte類/包裝類的使用說明2. android studio實現(xiàn)簡單的計算器(無bug)3. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作4. python Selenium 庫的使用技巧5. android 控件同時監(jiān)聽單擊和雙擊實例6. python+pywinauto+lackey實現(xiàn)PC端exe自動化的示例代碼7. vue使用exif獲取圖片經(jīng)緯度的示例代碼8. python logging.info在終端沒輸出的解決9. 詳解android adb常見用法10. Python 忽略文件名編碼的方法
