说明:
本文是用 Java1.8 官方的工具类进行的封装,两种加密算法的原理参考:
AES:https://blog.csdn.net/gulang03/article/details/81175854
RSA:https://blog.csdn.net/gulang03/article/details/81176133
实现类:
AESUtil:
package com.fknight.sbsmdemo.tools;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
/**
* AES 加密方法,是对称的密码算法(加密与解密的密钥一致),这里使用最大的 256 位的密钥
*/
public class AESUtil {
/**
* 获得一个 密钥长度为 256 位的 AES 密钥,
* @return 返回经 BASE64 处理之后的密钥字符串
*/
public static String getStrKeyAES() throws NoSuchAlgorithmException, UnsupportedEncodingException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom(String.valueOf(System.currentTimeMillis()).getBytes("utf-8"));
keyGen.init(256, secureRandom); // 这里可以是 128、192、256、越大越安全
SecretKey secretKey = keyGen.generateKey();
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
/**
* 将使用 Base64 加密后的字符串类型的 secretKey 转为 SecretKey
* @param strKey
* @return SecretKey
*/
public static SecretKey strKey2SecretKey(String strKey){
byte[] bytes = Base64.getDecoder().decode(strKey);
SecretKeySpec secretKey = new SecretKeySpec(bytes, "AES");
return secretKey;
}
/**
* 加密
* @param content 待加密内容
* @param secretKey 加密使用的 AES 密钥
* @return 加密后的密文 byte[]
*/
public static byte[] encryptAES(byte[] content, SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher &#