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