public static void main(String[] args) { //明文密码 String password = "123456"; //md5加密后的密码 String md5Password = Md5Utils.getMD5String(password); System.out.println(md5Password); //e10adc3949ba59abbe56e057f20f883e //随机一个8位数 加盐(salt) String salt = UUID.randomUUID().toString().substring(0, 8); System.out.println(salt); //d1a3a984 // 先加盐,再经过md5加密后的密码 String md5SaltPassword = Md5Utils.getMD5String(password + salt); System.out.println(md5SaltPassword);//fe685f5a90a1cfd4b3f2aca7eb2cc02d }
import cn.hutool.crypto.Mode; import cn.hutool.crypto.Padding; import cn.hutool.crypto.symmetric.AES; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; /** * @author gogo */ public class AESUtil { /** * 密钥 16字节 */ private static final String ENCODE_KEY = "1234567812345678"; private static final String IV_KEY = "0000000000000000"; public static void main(String[] args) { String encryptData = encryptFromString("zdm321123.", Mode.CBC, Padding.ZeroPadding); System.out.println("加密:" + encryptData); String decryptData = decryptFromString(encryptData, Mode.CBC, Padding.ZeroPadding); System.out.println("解密:" + decryptData); } public static String encryptFromString(String data, Mode mode, Padding padding) { AES aes; if (Mode.CBC == mode) { aes = new AES(mode, padding, new SecretKeySpec(ENCODE_KEY.getBytes(), "AES"), new IvParameterSpec(IV_KEY.getBytes())); } else { aes = new AES(mode, padding, new SecretKeySpec(ENCODE_KEY.getBytes(), "AES")); } return aes.encryptBase64(data, StandardCharsets.UTF_8); } public static String decryptFromString(String data, Mode mode, Padding padding) { AES aes; if (Mode.CBC == mode) { aes = new AES(mode, padding, new SecretKeySpec(ENCODE_KEY.getBytes(), "AES"), new IvParameterSpec(IV_KEY.getBytes())); } else { aes = new AES(mode, padding, new SecretKeySpec(ENCODE_KEY.getBytes(), "AES")); } byte[] decryptDataBase64 = aes.decrypt(data); return new String(decryptDataBase64, StandardCharsets.UTF_8); } }
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.RSA; import java.security.KeyPair; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; import java.util.HashMap; import java.util.Map; /** */ public class RsaUtils { /** * 类型 */ public static final String ENCRYPT_TYPE = "RSA"; /** * 获取公钥的key */ private static final String PUBLIC_KEY = "RSAPublicKey"; /** * 获取私钥的key */ private static final String PRIVATE_KEY = "RSAPrivateKey"; public static void main(String[] args) { Map stringStringMap = RsaUtils.generateKeyPair(); String RSAPublicKey = stringStringMap.get(PUBLIC_KEY); String RSAPrivateKey = stringStringMap.get(PRIVATE_KEY); System.out.println(RSAPublicKey); System.out.println("**************************"); System.out.println(RSAPrivateKey); System.out.println("##########################"); String content = "湾湾是中国的"; //公钥加密 String encrypt = RsaUtils.encrypt(content, RSAPublicKey); System.out.println(encrypt); //私钥解密 String decrypt = RsaUtils.decrypt(encrypt, RSAPrivateKey); System.out.println(decrypt); } public static Map generateKeyPair() { try { KeyPair pair = SecureUtil.generateKeyPair(ENCRYPT_TYPE); PrivateKey privateKey = pair.getPrivate(); PublicKey publicKey = pair.getPublic(); // 获取 公钥和私钥 的 编码格式(通过该 编码格式 可以反过来 生成公钥和私钥对象) byte[] pubEncBytes = publicKey.getEncoded(); byte[] priEncBytes = privateKey.getEncoded(); // 把 公钥和私钥 的 编码格式 转换为 Base64文本 方便保存 String pubEncBase64 = Base64.getEncoder().encodeToString(pubEncBytes); String priEncBase64 = Base64.getEncoder().encodeToString(priEncBytes); Map map = new HashMap(2); map.put(PUBLIC_KEY, pubEncBase64); map.put(PRIVATE_KEY, priEncBase64); return map; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 公钥加密 * * @param content 要加密的内容 * @param publicKey 公钥 */ public static String encrypt(String content, String publicKey) { try { RSA rsa = new RSA(null, publicKey); return rsa.encryptBase64(content, KeyType.PublicKey); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 私钥解密 * * @param content 要解密的内容 * @param privateKey 私钥 */ public static String decrypt(String content, String privateKey) { try { RSA rsa = new RSA(privateKey, null); return rsa.decryptStr(content, KeyType.PrivateKey); } catch (Exception e) { e.printStackTrace(); } return null; } }
com.auth0 java-jwt 3.2.0 io.jsonwebtoken jjwt 0.6.0
package com.bw.common; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import io.jsonwebtoken.*; import java.util.Date; public class JwtUtils { //密钥 public static final String SECRET = "zjKkye4PN59B2wriTjtVCo3BOYoD1B"; //过期时间 public static final long EXPIRE = 1000 * 60 * 60 * 24; /** * 根据入参进行一系列的加密,生成一个token * @param payload 入参是一个对象,一般传入用户的基本信息 * @return 返回一个token */ public static String createToken(Object payload) { String jwtToken = Jwts.builder() .setHeaderParam("typ", "JWT") .setHeaderParam("alg", "HS256")//使用hs256算法进行加密 此算法是对称性加密 .setIssuedAt(new Date()) .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))//设置token的有效截止时间 .claim("payload", JSONUtil.toJsonStr(payload))//存放载荷(用户信息) .signWith(SignatureAlgorithm.HS256, SECRET) .compact(); return jwtToken; } /** * 验证token是否有效 * @param jwtToken * @return */ public static boolean validToken(String jwtToken) { if (StrUtil.isEmpty(jwtToken)) { return false; } try { Jwts.parser().setSigningKey(SECRET).parseClaimsJws(jwtToken); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * 获取token里的载荷(用户信息) * @param jwtToken * @return */ public static String parseToken(String jwtToken) { if (StrUtil.isEmpty(jwtToken)) { return null; } try { Jws claimsJws = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(jwtToken); Claims claims = claimsJws.getBody(); return (String) claims.get("payload"); } catch (Exception e) { return null; } } }