侧边栏壁纸
  • 累计撰写 46 篇文章
  • 累计创建 38 个标签
  • 累计收到 2 条评论
隐藏侧边栏

java实现BASE,MD5,SHA,HMAC加密

哒卟刘先生
2022-01-10 / 0 评论 / 0 点赞 / 113 阅读 / 2,622 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-01-10,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

java实现密码加密的四种方式,记录一下方便以后查阅

1,BASE加密

public class BaseUtil {
  /** 
   * 解密 
   */ 
  public static byte[] decryptBASE(String key) throws Exception { 
  	return (new BASEDecoder()).decodeBuffer(key); 
  } 
  /** 
   * 加密 
   */
  public static String encryptBASE(byte[] key) throws Exception { 
    return (new BASEEncoder()).encodeBuffer(key); 
  } 
  public static void main(String[] args) {
   String str="123456";
    try {
    String result= BASE.encryptBASE(str.getBytes());	
    System.out.println("加密结果:"+result);
     byt[] result2= BASE.decryptBASE(result);
     String str=new String(result2);
      System.out.println("解密结果:"+str);
  } catch (Exception e) {
    e.printStackTrace();
  }
  }
}

MD5加密

public static String getMD5(String str) {
    try {
        // 生成一个MD5加密计算摘要
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 计算md5函数
        md.update((str+"liuergou").getBytes());
        // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
        // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
        return new BigInteger(1, md.digest()).toString(16);
    } catch (Exception e) {
        throw new SpeedException("MD5加密出现错误");
    }
}

SHA加密

public class SHAUtil {
	public static byte[] encryptSHA(byte[] data, String shaN) throws Exception {

        MessageDigest sha = MessageDigest.getInstance(shaN);
        sha.update(data);
        return sha.digest();

    }
	
	public static String getSHAString(String str) {
		byte[] outputData = new byte[0];
        try {
        	  outputData =  encryptSHA((str+"liuergou").getBytes(), "SHA-256");
              return new BigInteger(1, outputData).toString(16);
        } catch (Exception e) {
           e.printStackTrace();
           return null;
        }
    }
	
	public static void main(String[] args) {
		String str = "123456";
		String string = getSHAString(str);
		System.out.println(string);
		
		
	}
}

HMAC加密

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/** 
 * 基础加密组件 
 */ 
public abstract class HMAC { 
  public static final String KEY_MAC = "HmacMD"; 
  /** 
   * 初始化HMAC密钥 
   * 
   * @return 
   * @throws Exception 
   */ 
  public static String initMacKey() throws Exception { 
    KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC); 
    SecretKey secretKey = keyGenerator.generateKey(); 
    //base加密
    return BASE.encryptBASE(secretKey.getEncoded()); 
  } 
  /** 
   * HMAC加密 :主要方法
   * 
   * @param data 
   * @param key 
   * @return 
   * @throws Exception 
   */ 
  public static String encryptHMAC(byte[] data, String key) throws Exception { 
    SecretKey secretKey = new SecretKeySpec(BASE.decryptBASE(key), KEY_MAC); 
    Mac mac = Mac.getInstance(secretKey.getAlgorithm()); 
    mac.init(secretKey); 
    return new String(mac.doFinal(data)); 
  } 
 
  public static String getHMAC(String str) throws Exception{
	  String key=initMacKey();
	  
	  byte[] inputData = str.getBytes(); 
      /*对数据进行加密*/
    return HMAC.encryptHMAC(inputData, key);
  }
  
  
  public static void main(String args[])
  {
    try {
       String inputStr = "liuergou";
       System.out.println(getHMAC(inputStr));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
0

评论区