小工具知识网

当前位置: 首页 > 行业知识 > Java RSA 加密解密工具类 RSAUtil 支持长文本加密解密(密钥对生成、公钥加密、私钥加密、公钥解密、私钥解密)

行业知识

Java RSA 加密解密工具类 RSAUtil 支持长文本加密解密(密钥对生成、公钥加密、私钥加密、公钥解密、私钥解密)

2026-12-04 12:17:34 来源:互联网转载

对于加密解密的代码,我能想到的可能会使用的都封装在了如下工具类中,代码注释也尽量写详细了,如:密钥对生成、公钥加密、私钥加密、公钥解密、私钥解密…

()

文章归档:https://www.yuque.com/u27599042/coding_star/srag5g2yoxso7vwd

源代码

import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.security.*;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.security.spec.InvalidKeySpecException;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.*;/** * 和 RSA 相关的工具类。 * Padding 方式为默认的 OPENSSL_PKCS1_PADDING; * RSA 密钥对的 bit 数默认为 1024。 * 

* 代码参考: * - https://blog.csdn.net/piaoranyuji/article/details/126140261
* - https://blog.csdn.net/qy20115549/article/details/83105736
* - https://blog.csdn.net/qq_33204709/article/details/126939400
* - https://blog.csdn.net/qq_41893274/article/details/130120395
*/public class RSAUtil { /** * 加密算法名称 */ public static final String ENCRYPTION_ALGORITHM_NAME = "RSA"; /** * RSA 加密算法获取密钥对中的公钥使用的 key */ public static final String PUBLIC_KEY = "RSAPublicKey"; /** * RSA 加密算法获取密钥对中的密钥使用的 key */ public static final String PRIVATE_KEY = "RSAPrivateKey"; /** * RSA 密钥对的 bit 数(密钥对的长度)。 * 常用 1024、2048,密钥对的 bit 数,越大越安全,但是越大对服务器的消耗越大 */ private static int keySize = 1024; /** * keySize bit 数下的 RSA 密钥对所能够加密的最大明文大小。 * RSA 算法一次能加密的明文长度与密钥长度(RSA 密钥对的 bit 数)成正比, * 默认情况下,Padding 方式为 OPENSSL_PKCS1_PADDING,RSA 算法会使 * 用 11 字节的长度用于填充,所以默认情况下,RSA 所能够加密的最大明文大 * 小为 (keySize / 8 - 11) byte */ private static int maxEncryptPlainTextLen = 117; /** * keySize bit 数下的 RSA 密钥对所能够解密的最大密文大小。 * (keySize / 8) byte */ private static int maxDecryptCipherTextLen = 128; private RSAUtil() { } /** * 获取 RSA 密钥对的 bit 数 * * @return RSA 密钥对的 bit 数 */ public static int getKeySize() { return keySize; } /** * 获取 keySize bit 数下的 RSA 密钥对所能够加密的最大明文大小。 * * @return keySize bit 数下的 RSA 密钥对所能够加密的最大明文大小。 */ public static int getMaxEncryptPlainTextLen() { return maxEncryptPlainTextLen; } /** * 获取 keySize bit 数下的 RSA 密钥对所能够解密的最大密文大小。 * * @return keySize bit 数下的 RSA 密钥对所能够解密的最大密文大小。 */ public static int getMaxDecryptCipherTextLen() { return maxDecryptCipherTextLen; } /** * 为 RSA 密钥对的 bit 数赋值,同时重新计算 keySize bit 数下的 * RSA 密钥对所能够加密的最大明文大小、所能够解密的最大密文大小 * * @param keySize RSA 密钥对的 bit 数 */ public static void setKeySize(int keySize) { // 为 RSA 密钥对的 bit 数赋值 RSAUtil.keySize = keySize; // 更新 keySize bit 数下的 RSA 密钥对所能够加密的最大明文大小 maxEncryptPlainTextLen = keySize / 8 - 11; // 更新 keySize bit 数下的 RSA 密钥对所能够解密的最大密文大小 maxDecryptCipherTextLen = keySize / 8; } /** * 生成 RSA 加密算法密钥对 * * @return RSA 加密算法密钥对 */ public static Map generateRSAKeyPair() throws NoSuchAlgorithmException { // 获取 RSA 加密算法密钥对生成器 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ENCRYPTION_ALGORITHM_NAME); // 初始化 RSA 密钥对生成器,设置密钥对的 bit 数 keyPairGenerator.initialize(keySize); // 生成 RSA 密钥对 KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 获取 RSA 密钥对公钥 RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); // 获取 RSA 密钥对私钥 RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); // 以集合的形式返回 RSA 密钥对 HashMap keyPairMap = new HashMap(); keyPairMap.put(PUBLIC_KEY, rsaPublicKey); keyPairMap.put(PRIVATE_KEY, rsaPrivateKey); return keyPairMap; } /** * 生成 RSA 加密算法密钥对 * * @param keySize 默认 1024,密钥对的 bit 数,越大越安全,但是越大对服务器的消耗越大 * @return RSA 加密算法密钥对 */ public static Map generateRSAKeyPair(Integer keySize) throws NoSuchAlgorithmException { // 如果传递了 keySize,进行 keySize 的更新 if (Objects.nonNull(keySize)) { setKeySize(keySize); } return generateRSAKeyPair(); } /** * 生成 RSA 加密算法密钥对,其中密钥是以字符串形式进行返回 * * @param keySize RSA 密钥对的 bit 数 * @return RSA 加密算法密钥对 */ public static Map generateRSAKeyPairStr(Integer keySize) throws NoSuchAlgorithmException { // 生成 RSA 加密算法密钥对 Map keyPair = generateRSAKeyPair(keySize); // 获取字符串形式的公钥 String publicKeyStr = getPublicKeyStr(keyPair); // 获取字符串形式的私钥 String privateKeyStr = getPrivateKeyStr(keyPair); // 以集合的形式返回 RSA 密钥对 HashMap keyPairStrMap = new HashMap(); keyPairStrMap.put(PUBLIC_KEY, publicKeyStr); keyPairStrMap.put(PRIVATE_KEY, privateKeyStr); return keyPairStrMap; } /** * 生成 RSA 加密算法密钥对,其中密钥是以字符串形式进行返回 * * @return RSA 加密算法密钥对 */ public static Map generateRSAKeyPairStr() throws NoSuchAlgorithmException { return generateRSAKeyPairStr(null); } /** * 将字节数组形式的密钥使用 BASE64 编码为字符串形式的密钥 * * @param key 字节数组形式的密钥 * @return 字符串形式的密钥 */ public static String encryptBASE64(byte[] key) { return new String(Base64.getEncoder().encode(key)); } /** * 将字符串形式的密钥使用 BASE64 解码为字节数组形式的密钥 * * @param key 字符串形式的密钥 * @return 字节数组形式的密钥 */ public static byte[] decryptBASE64(String key) { return Base64.getDecoder().decode(key); } /** * 获取 RSA 密钥对中的公钥 * * @param keyPair RSA 密钥对 * @return RSA 密钥对中的公钥 */ public static PublicKey getPublicKey(Map keyPair) { return ((PublicKey) keyPair.get(PUBLIC_KEY)); } /** * 获取字符串形式的公钥 * * @param keyPair RSA 密钥对 * @return 字符串形式的公钥 */ public static String getPublicKeyStr(Map keyPair) { // 获取 RSA 密钥对中的公钥 PublicKey publicKey = getPublicKey(keyPair); // 将 RSA 公钥使用 BASE64 编码为字符串形式 return encryptBASE64(publicKey.getEncoded()); } /** * 根据 RSA 公钥字符串获取 RSA Java 公钥对象 * * @param publicKeyStr RSA 公钥字符串 * @return RSA Java 公钥对象 */ public static PublicKey getPublicKeyByStr(String publicKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException { // 将字符串形式的公钥使用 BASE64 解码为字节数组形式的公钥 byte[] publicKeyBytes = decryptBASE64(publicKeyStr); // 根据 publicKeyBytes 获取 X.509 证书中的密钥规范 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes); // 获取 RSA 加密算法的密钥工厂 KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM_NAME); // 根据密钥规范,让密钥工厂生成 RSA 公钥 return keyFactory.generatePublic(x509EncodedKeySpec); } /** * 通过字符串形式的密钥对获取字符串形式的公钥 * * @param keyPairStr 字符串形式的密钥对 * @return 字符串形式的公钥 */ public static String getPublicKeyStrByKeyPairStr(Map keyPairStr) { return keyPairStr.get(PUBLIC_KEY); } /** * 获取 RSA 密钥对中的私钥 * * @param keyPair RSA 密钥对 * @return RSA 密钥对中的私钥 */ public static PrivateKey getPrivateKey(Map keyPair) { return ((PrivateKey) keyPair.get(PRIVATE_KEY)); } /** * 获取字符串形式的私钥 * * @param keyPair RSA 密钥对 * @return 字符串形式的私钥 */ public static String getPrivateKeyStr(Map keyPair) { // 获取 RSA 密钥对中的私钥 PrivateKey privateKey = getPrivateKey(keyPair); // 将 RSA 私钥使用 BASE64 编码为字符串形式 return encryptBASE64(privateKey.getEncoded()); } /** * 根据 RSA 私钥字符串获取 RSA Java 私钥对象 * * @param privateKeyStr RSA 私钥字符串 * @return RSA Java 私钥对象 */ public static PrivateKey getPrivateKeyByStr(String privateKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException { // 将字符串形式的私钥使用 BASE64 解码为字节数组形式的私钥 byte[] privateKeyBytes = decryptBASE64(privateKeyStr); // 根据 privateKeyBytes 获取密钥规范 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes); // 获取 RSA 加密算法的密钥工厂 KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM_NAME); // 根据密钥规范,让密钥工厂生成 RSA 私钥 return keyFactory.generatePrivate(pkcs8EncodedKeySpec); } /** * 通过字符串形式的密钥对获取字符串形式的私钥 * * @param keyPairStr 字符串形式的密钥对 * @return 字符串形式的私钥 */ public static String getPrivateKeyStrByKeyPairStr(Map keyPairStr) { return keyPairStr.get(PRIVATE_KEY); } /** * 根据字符串形式的密钥对获取密钥对象形式的密钥对 * * @param keyPairStr 字符串形式的密钥对 * @return 密钥对象形式的密钥对 */ public static Map getKeyPairByStr(Map keyPairStr) throws NoSuchAlgorithmException, InvalidKeySpecException { HashMap keyPair = new HashMap(); // 获取字符串形式的公钥 keyPair.put(PUBLIC_KEY, getPublicKeyByStr(getPublicKeyStrByKeyPairStr(keyPairStr))); // 获取字符串形式的私钥 keyPair.put(PRIVATE_KEY, getPrivateKeyByStr(getPrivateKeyStrByKeyPairStr(keyPairStr))); return keyPair; } /** * 使用指定的密钥(公钥或私钥)进行 RSA 明文加密 * * @param keyPair 密钥对象形式的密钥对 * @param plainText 明文 * @param key 从 Map 集合中获取密钥的 key * @return 明文加密后的密文字符串 */ public static String encrypt(Map keyPair, String plainText, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException { // 字节数组输出流,用于拼接存储中间分段加密结果,最终生成明文的完整加密密文 ByteArrayOutputStream cipherText = null; try { // 获取要进行加密的明文的字节数组 byte[] plainTextBytes = plainText.getBytes(StandardCharsets.UTF_8); // 明文的字节数组长度 int plainTextBytesLen = plainTextBytes.length; // 计算明文需要分段加密的次数 int encryptCount = ((int) Math.ceil((plainTextBytesLen * 1.0) / maxEncryptPlainTextLen)); // 字节数组输出流,用于拼接存储中间分段加密结果,最终生成明文的完整加密密文 cipherText = new ByteArrayOutputStream(); // 获取 RSA 加密算法对应的加密器 Cipher rsaCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM_NAME); // 初始化加密器,指定加密器的工作模式为加密,以及加密的密钥 rsaCipher.init(Cipher.ENCRYPT_MODE, (Key) keyPair.get(key)); // 进行明文的分段加密 for (int i = 0; i

参考

https://blog.csdn.net/piaoranyuji/article/details/126140261

()

https://blog.csdn.net/qy20115549/article/details/83105736

https://blog.csdn.net/qq_33204709/article/details/126939400

https://blog.csdn.net/qq_41893274/article/details/130120395

上一篇:服务器瘫痪的时候怎么进?

下一篇:租贷服务器怎么开光影?