说明:
本文是用 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 = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
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);
}
}
RSAUtil:
package com.fknight.sbsmdemo.tools;
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;
/**
* RSA 是非对称的密码算法,密钥分公钥和私钥,公钥用来加密,私钥用于解密
*/
public class RSAUtil {
/**
* 生成密钥对:密钥对中包含公钥和私钥
* @return 包含 RSA 公钥与私钥的 keyPair
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static KeyPair getKeyPair() throws NoSuchAlgorithmException, UnsupportedEncodingException {
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;
}
}
测试:
AESUtilTest
package com.fknight.sbsmdemo.tools;
import javax.crypto.SecretKey;
import java.util.Base64;
/**
* 测试 AESUtil 对AES加密算法的封装
*/
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();
}
}
}
测试结果:
RSAUtilTest
package com.fknight.sbsmdemo.tools;
import java.security.*;
import java.util.Base64;
/**
* 对 RSAUtil 进行测试
*/
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();
}
}
}
测试结果:
PS:关于如何使用 JavaScript 和 Java 进行跨语言 AES 和 RSA 的实现可以参考我的另一篇博文