Java:加密与数字证书

加密算法

哈希算法

哈希算法是将任意的输入数据映射为固定长度的字符串,且其加密结果不可逆,所以常用于生成报文摘要以检测报文是否被篡改,因此哈希算法也称摘要算法

常见的哈希算法:

  • MD 系列:MD1、MD2、MD3、MD4、MD5
  • SHA 系列:SHA-1、SHA-256、SHA-512
  • HMAC 系列:HmacMD5

MD5加密示例:

import org.apache.tomcat.util.buf.HexUtils;
import org.springframework.util.StringUtils;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public String md5Digest(String message){
   
	if(!StringUtils.isEmpty(message)){
   
		try{
   
			//根据MD5加密算法获取消息摘要对象
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			//添加报文
			messageDigest.update(message.getBytes(StandardCharsets.UTF_8));
			//生成摘要
			byte[] digest = messageDigest();
			//字节数组转十六进制字符串
			return HexUtils.toHexString(digest);
		}catch(NoSuchAlgorithmException e){
   
			log.error("发生异常:",e);
		}
	}
	return null;
}

为了防止 彩虹表 攻击,一般在报文中加入盐值(salt),或使用HMAC算法

MD5加盐加密示例:

import org.apache.tomcat.util.buf.HexUtils;
import org.springframework.util.StringUtils;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public String md5DigestWithSalt(String message, String salt){
   
	if(!StringUtils.isEmpty(message)){
   
		try{
   
			//加盐
			message = message + salt;
			//根据MD5加密算法获取消息摘要对象
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			//添加报文
			messageDigest.update(message.getBytes(StandardCharsets.UTF_8));
			//生成摘要
			byte[] digest = messageDigest();
			//字节数组转为十六进制字符串
			return HexUtils.toHexString(digest);
		}catch(NoSuchAlgorithmException e){
   
			log.error("发生异常:",e);
		}
	}
	return null;
}

HmacMD5加密示例:

import org.apache.tomcat.util.buf.HexUtils;
import org.springframework.util.StringUtils;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

//获取密钥
public String getKey(){
   
	try{
   
		//根据算法获取密钥构造器
		KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
		//获取密钥
		SecretKey secretKey = keyGenerator.generateKey();
		//密钥转字节数组
		byte[] secretKeyBytes = secretKey.getEncoded();
		//字节数组转十六进制字符串
		return HexUtils.toHexString(secretKeyBytes);
	}catch(NoSuchAlgorithmException e){
   
		log.error("发生异常:",e);
	}
	return null;
}

//加密
public String digest(String message, String key){
   
	if(!StringUtils.isEmpty(message) && !StringUtils.isEmpty(key)){
   
		try{
   
			//获取key
			SecretKey secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacMD5");
			//根据算法获取mac对象
			Mac mac = Mac.getInstance("HmacMD5");
			//初始化密钥
			mac.init(secretKey);
			//更新报文
			mac.update(message.getBytes(StandardCharsets.UTF_8));
			//生成摘要
			byte[] digestBytes = mac.doFinal();
			//字节数组转为十六进制字符串
			return HexUtils.toHexString(digestBytes);
		}catch(NoSuchAlgorithmException | InvalidKeyException e){
   
			log.error("发生异常:",e);
		}
	}
	return null;
}

对称加密算法

对称加密算法是指加密和解密使用同一个密钥,常用对称加密算法:

  • DES
  • AES
  • IDEA

AES加密示例:

import org.apache.tomcat.util.buf.HexUtils;
import org.springframework.util.StringUtils;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值