赞
踩
base64加解密:commons-codes包 String username="root", password="123", accessCode="1"; //base64加密 String userBase64 = Base64.encodeBase64String(username.getBytes("UTF-8")); HashMap<String, String> p = new HashMap<>(); p.put("UserName", userBase64); p.put("password", passwordMd5); System.out.println(username.getBytes("UTF-8")); //{password=202cb962ac59075b964b07152d234b70, UserName=cm9vdA==} System.out.println(p); //base64解密 byte[] bytes = Base64.decodeBase64("cm9vdA=="); String user = new String(bytes); System.out.println(user);//root //md5加密 String passwordMd5 = DigestUtils.md5Hex(password);
MD5加密:rt.jar包
package com.xxx; import org.apache.http.util.TextUtils; import org.junit.Test; import org.springframework.boot.test.context.SpringBootTest; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @SpringBootTest public class MD5Utils { /** * 计算字符串MD5值 * * @param string * @return */ @Test public static String md5(String string) { if (TextUtils.isEmpty(string)) { return ""; } MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest(string.getBytes()); String result = ""; for (byte b : bytes) { String temp = Integer.toHexString(b & 0xff); if (temp.length() == 1) { temp = "0" + temp; } result += temp; } return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } /** * 计算文件的 MD5 值 * * @param file * @return */ @Test public static String md5(File file) { if (file == null || !file.isFile() || !file.exists()) { return ""; } FileInputStream in = null; String result = ""; byte buffer[] = new byte[8192]; int len; try { MessageDigest md5 = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer)) != -1) { md5.update(buffer, 0, len); } byte[] bytes = md5.digest(); for (byte b : bytes) { String temp = Integer.toHexString(b & 0xff); if (temp.length() == 1) { temp = "0" + temp; } result += temp; } } catch (Exception e) { e.printStackTrace(); } finally { if (null != in) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /** * 采用nio的方式,计算文件的 MD5 值 * * @param file * @return */ @Test public static String md5Nio(File file) { String result = ""; FileInputStream in = null; try { in = new FileInputStream(file); MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(byteBuffer); byte[] bytes = md5.digest(); for (byte b : bytes) { String temp = Integer.toHexString(b & 0xff); if (temp.length() == 1) { temp = "0" + temp; } result += temp; } } catch (Exception e) { e.printStackTrace(); } finally { if (null != in) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /** * 对字符串多次MD5加密 * * @param string * @param times * @return */ @Test public static String md5(String string, int times) { if (TextUtils.isEmpty(string)) { return ""; } String md5 = md5(string); for (int i = 0; i < times - 1; i++) { md5 = md5(md5); } return md5(md5); } /** * MD5加盐 * <p> * 加盐的方式也是多种多样 * <p> * string+key(盐值key)然后进行MD5加密 * <p> * 用string明文的hashcode作为盐,然后进行MD5加密 * 随机生成一串字符串作为盐,然后进行MD5加密 * * @param string * @param slat * @return */ @Test public static String md5(String string, String slat) { if (TextUtils.isEmpty(string)) { return ""; } MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest((string + slat).getBytes()); String result = ""; for (byte b : bytes) { String temp = Integer.toHexString(b & 0xff); if (temp.length() == 1) { temp = "0" + temp; } result += temp; } return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。