Java实现AES加密
生成秘钥简单粗暴
这边AES秘钥默认为128位,获得无政策权限后可为192或256,因此对应字符为16位,直接生成16位的秘钥
import java.io.UnsupportedEncodingException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.Base64; import java.util.HashMap; import java.util.Map; import java.util.Random; /** * @ClassName: SecretKeyUtils * 各秘钥生成 * @author zhupig3028 * @date 2022年5月10日 */ public class SecretKeyUtils { /** AES秘钥 */ private static final String AES_ALGORITHM = "AES"; /** 3DES秘钥 */ private static final String DES_ALGORITHM = "DESede"; /** 秘钥生成来源 */ public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; /** * 生成AES密钥对象 * @throws NoSuchAlgorithmException */ public static String generateAESKey() throws NoSuchAlgorithmException, UnsupportedEncodingException { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0; i < 16; i++) { sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); } return sb.toString(); } /** * 生成3DES密钥对象 */ public static String generate3DESKey() { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0; i < 24; i++) { sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); } return sb.toString(); } /** * 随机生成RSA秘钥 * @throws NoSuchAlgorithmException */ public static Map<String,String > genKeyPair() throws NoSuchAlgorithmException { HashMap<String, String> stringStringHashMap = new HashMap<>(); // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); // 初始化密钥对生成器,密钥大小为96-1024位 keyPairGen.initialize(1024,new SecureRandom()); // 生成一个密钥对,保存在keyPair中 KeyPair keyPair = keyPairGen.generateKeyPair(); // 得到私钥 RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到公钥 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); String publicKeyString = new String(Base64.getEncoder().encodeToString(publicKey.getEncoded())); // 得到私钥字符串 String privateKeyString = new String(Base64.getEncoder().encodeToString((privateKey.getEncoded()))); // 将公钥和私钥保存到Map //0表示公钥 stringStringHashMap.put("0",publicKeyString); //1表示私钥 stringStringHashMap.put("1",privateKeyString); return stringStringHashMap; } }
Java实现AES加解密
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; /** * @ClassName: AESUtils * @Description: AES 对称算法加密/解密工具类 * @author zhupig3028 * @date 2022年5月10日 */ public class AESUtils { /** * 加解密统一编码方式 */ private final static String ENCODING = "utf-8"; /** * 加解密方式 */ private final static String ALGORITHM = "AES"; /** *加密模式及填充方式 */ private final static String PATTERN = "AES/ECB/pkcs5padding"; /** * AES加密 * @param plainText * @param key * @return * @throws Exception */ public static String encrypt(String plainText, String key) throws Exception { if (key == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位 if (key.length() != 16) { System.out.print("Key长度不是16位"); return null; } SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM); // AES加密采用pkcs5padding填充 Cipher cipher = Cipher.getInstance(PATTERN); //用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, secretKey); //执行加密操作 byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING)); return Base64.getEncoder().encodeToString(encryptData); } /** * AES解密 * @param plainText * @param key * @return * @throws Exception */ public static String decrypt(String plainText, String key) throws Exception { SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM); // 获取 AES 密码器 Cipher cipher = Cipher.getInstance(PATTERN); // 初始化密码器(解密模型) cipher.init(Cipher.DECRYPT_MODE, secretKey); // 解密数据, 返回明文 byte[] encryptData = cipher.doFinal(Base64.getDecoder().decode(plainText)); return new String(encryptData,ENCODING); } }