AES及RSA算法java实现
AES算法工具类:
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;
/**
* @Title:
* @Description: AES 加密方法,是对称的密码算法(加密与解密的密钥一致),这里使用最大的 256 位的密钥
* @author: wangsai
* @date 2019/8/14 15:57
*/
public class AESUtil {
/**
* 获得一个 密钥长度为 256 位的 AES 密钥,
* @return 返回经 BASE64 处理之后的密钥字符串
*/
public static String getStrKeyAES() throws NoSuchAlgorithmException, UnsupportedEncodingException {
/* KeyGenerator此类提供(对称)密钥生成器的功能:
参考网页:https://www.oschina.net/uploads/doc/javase-6-doc-api-zh_CN/javax/crypto/KeyGenerator.html*/
KeyGenerator keyGen = KeyGenerator.getInstance("AES");//返回生成指定算法的秘密密钥的 KeyGenerator 对象
/*System.currentTimeMillis()产生一个当前的毫秒
SecureRandom类提供加密的强随机数生成器 (RNG)*/
SecureRandom secureRandom = new SecureRandom(String.valueOf(System.currentTimeMillis()).getBytes("utf-8"));
/*init(int keysize, SecureRandom random)
使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小*/
keyGen.init(256, secureRandom); // 这里可以是 128、192、256、越大越安全
/*SecretKey此接口不包含方法或常量。其唯一目的是分组秘密密钥(并为其提供类型安全)(秘密(对称)密钥。)。
参考网址:http://man.lupaworld.com/content/develop/JDK_6.0_API_html_zh_CN/html/zh_CN/api/javax/crypto/SecretKey.html*/
SecretKey secretKey = keyGen.generateKey();// .generateKey()生成一个密钥。
//.getEncoded()返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null。
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
/**
* 将使用 Base64 加密后的字符串类型的 secretKey 转为 SecretKey
* @param strKey
* @return SecretKey
*/
public static SecretKey strKey2SecretKey(String strKey){
/*SecretKey此接口不包含方法或常量。其唯一目的是分组秘密密钥(并为其提供类型安全)(秘密(对称)密钥。)。
参考网址:http://man.lupaworld.com/content/develop/JDK_6.0_API_html_zh_CN/html/zh_CN/api/javax/crypto/SecretKey.html*/
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此类为加密和解密提供密码功能
参考网址:http://tool.oschina.net/uploads/apidocs/jdk-zh/javax/crypto/Cipher.html*/
Cipher cipher = Cipher.getInstance("AES");//应用程序调用 Cipher的getInstance 方法并将所请求转换的名称传递给它
cipher.init(Cipher.ENCRYPT_MODE, secretKey);//用密钥和随机源初始化此 Cipher。
return cipher.doFinal(content);//按单部分操作加密或解密数据,或者结束一个多部分操作。
}
/**
* 解密
* @param content 待解密内容
* @param secretKey 解密使用的 AES 密钥
* @return 解密后的明文 byte[]
*/
public static byte[] decryptAES(byte[] content, SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(content);
}
}
AES算法测试:
import javax.crypto.SecretKey;
import java.util.Base64;
/**
* @Title:
* @Description: 测试 AESUtil 对AES加密算法的封装
* @author: wangsai
* @date 2019/8/14 15:59
*/
public class AESUtilTest {
public static void main(String[] args){
String content = "abcdefg789+-*+="; // 待加密的字符串
System.out.println("明文数据为:" + content);
try {
// 获得经 BASE64 处理之后的 AES 密钥
String strKeyAES = AESUtil.getStrKeyAES();
System.out.println("经BASE64处理之后的密钥:" + strKeyAES);
// 将 BASE64 处理之后的 AES 密钥转为 SecretKey
SecretKey secretKey = AESUtil.strKey2SecretKey(strKeyAES);
// 加密数据
byte[] encryptAESbytes = AESUtil.encryptAES(content.getBytes("utf-8"), secretKey);
System.out.println("加密后的数据经 BASE64 处理之后为:" + Base64.getEncoder().encodeToString(encryptAESbytes));
// 解密数据
String decryptAESStr = new String(AESUtil.decryptAES(encryptAESbytes, secretKey), "utf-8");
System.out.println("解密后的数据为:" + decryptAESStr);
if (content.equals(decryptAESStr)){
System.out.println("测试通过!");
}else {
System.out.println("测试未通过!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
AES测试结果截图:
RSA算法工具类:
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* @Title:
* @Description: RSA 是非对称的密码算法,密钥分公钥和私钥,公钥用来加密,私钥用于解密
* @author: wangsai
* @date 2019/8/14 16:02
*/
public class RSAUtil {
/**
* 生成密钥对:密钥对中包含公钥和私钥
* @return 包含 RSA 公钥与私钥的 keyPair
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static KeyPair getKeyPair() throws NoSuchAlgorithmException, UnsupportedEncodingException {
/* KeyPairGenerator类用于生成公钥和私钥对。
* 参考地址:https://docs.oracle.com/javase/7/docs/api/java/security/KeyPairGenerator.html */
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); // 获得RSA密钥对的生成器实例
SecureRandom secureRandom = new SecureRandom(String.valueOf(System.currentTimeMillis()).getBytes("utf-8")); // 说的一个安全的随机数
keyPairGenerator.initialize(2048, secureRandom); // 这里可以是1024、2048 初始化一个密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 获得密钥对
return keyPair;
}
/**
* 获取公钥 (并进行Base64编码,返回一个 Base64 编码后的字符串)
* @param keyPair
* @return 返回一个 Base64 编码后的公钥字符串
*/
public static String getPublicKey(KeyPair keyPair){
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return Base64.getEncoder().encodeToString(bytes);
}
/**
* 获取私钥(并进行Base64编码,返回一个 Base64 编码后的字符串)
* @param keyPair
* @return 返回一个 Base64 编码后的私钥字符串
*/
public static String getPrivateKey(KeyPair keyPair){
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return Base64.getEncoder().encodeToString(bytes);
}
/**
* 将Base64编码后的公钥转换成 PublicKey 对象
* @param pubStr
* @return PublicKey
*/
public static PublicKey string2PublicKey(String pubStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] bytes = Base64.getDecoder().decode(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 将Base64编码后的私钥转换成 PrivateKey 对象
* @param priStr
* @return PrivateKey
*/
public static PrivateKey string2Privatekey(String priStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] bytes = Base64.getDecoder().decode(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 公钥加密
* @param content 待加密的内容 byte[]
* @param publicKey 加密所需的公钥对象 PublicKey
* @return 加密后的字节数组 byte[]
*/
public static byte[] publicEncrytype(byte[] content, PublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
/**
* 私钥解密
* @param content 待解密的内容 byte[]
* @param privateKey 解密需要的私钥对象 PrivateKey
* @return 解密后的字节数组 byte[]
*/
public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
}
RSA测试类:
import java.security.*;
import java.util.Base64;
/**
* @Title:
* @Description: 对 RSAUtil 进行测试
* @author: wangsai
* @date 2019/8/14 16:03
*/
public class RSAUtilTest {
public static void main(String[] args){
String content = "abcdefg456+-="; // 明文内容
System.out.println("原始字符串是:" + content);
try {
// 获得密钥对
KeyPair keyPair = RSAUtil.getKeyPair();
// 获得进行Base64 加密后的公钥和私钥 String
String privateKeyStr = RSAUtil.getPrivateKey(keyPair);
String publicKeyStr = RSAUtil.getPublicKey(keyPair);
System.out.println("Base64处理后的私钥:" + privateKeyStr + "\n"
+ "Base64处理后的公钥:" + publicKeyStr);
// 获得原始的公钥和私钥,并以字符串形式打印出来
PrivateKey privateKey = RSAUtil.string2Privatekey(privateKeyStr);
PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr);
// 公钥加密/私钥解密
byte[] publicEncryBytes = RSAUtil.publicEncrytype(content.getBytes(), publicKey);
System.out.println("公钥加密后的字符串(经BASE64处理):" + Base64.getEncoder().encodeToString(publicEncryBytes));
byte[] privateDecryBytes = RSAUtil.privateDecrypt(publicEncryBytes, privateKey);
System.out.println("私钥解密后的原始字符串:" + new String(privateDecryBytes));
String privateDecryStr = new String(privateDecryBytes, "utf-8");
if (content.equals(privateDecryStr)){
System.out.println("测试通过!");
}else {
System.out.println("测试未通过!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}