当前位置: 首页 > article >正文

RSA非对称加密解,支持分段加密解密、(公钥加密,私钥解密)、(私钥加密,公钥解密)

运行Main方法测试

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 * RSA加解密工具类
 */
public class RSAUtil {
    /**
     * 加密算法
     */
    private static final String ALGORITHM = "RSA";
    
    /**
     * 填充方式
     */
    private static final String PADDING_FORMAT = "RSA/ECB/PKCS1Padding";
    
    /**
     * 明文块最大值
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;
    
    /**
     * 密文块最大值
     */
    private static final int MAX_DECRYPT_BLOCK = 128;
    
    /**
     * 字符编码
     */
    public static final String CHARSET_UTF_8 = "UTF-8";
    
    /**
     * 生成密钥对
     *
     * @return 包含公钥和私钥的Map
     * @throws Exception 如果生成密钥对失败
     */
    private static Map<String, String> generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
        keyPairGenerator.initialize(1024);
        
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        
        Map<String, String> keyMap = new HashMap<>(2);
        keyMap.put("RSAPublicKey", Base64.encodeBase64String(publicKey.getEncoded()));
        keyMap.put("RSAPrivateKey", Base64.encodeBase64String(privateKey.getEncoded()));
        
        return keyMap;
    }
    
    /**
     * 使用公钥加密
     *
     * @param text      明文
     * @param publicKey 公钥
     * @param charset   字符编码
     * @return 加密后的字符串(base64)
     * @throws Exception 如果加密失败
     */
    public static String encrypt(String text, String publicKey, String charset) throws Exception {
        byte[] data = text.getBytes(charset);
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        Key key = keyFactory.generatePublic(x509KeySpec);
        
        return getCipherText(data, key, Cipher.ENCRYPT_MODE, MAX_ENCRYPT_BLOCK, charset);
    }
    
    /**
     * 使用私钥解密
     *
     * @param ciphertext 密文(base64)
     * @param privateKey 私钥
     * @param charset    字符编码
     * @return 解密后的字符串
     * @throws Exception 如果解密失败
     */
    public static String decrypt(String ciphertext, String privateKey, String charset) throws Exception {
        byte[] data = Base64.decodeBase64(ciphertext);
        byte[] keyBytes = Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        Key key = keyFactory.generatePrivate(pkcs8KeySpec);
        
        return getCipherText(data, key, Cipher.DECRYPT_MODE, MAX_DECRYPT_BLOCK, charset);
    }
    
    /**
     * 使用私钥加密
     *
     * @param text       明文
     * @param privateKey 私钥
     * @param charset    字符编码
     * @return 加密后的字符串(base64)
     * @throws Exception 如果加密失败
     */
    public static String encryptWithPrivateKey(String text, String privateKey, String charset) throws Exception {
        byte[] data = text.getBytes(charset);
        byte[] keyBytes = Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        Key key = keyFactory.generatePrivate(pkcs8KeySpec);
        
        return getCipherText(data, key, Cipher.ENCRYPT_MODE, MAX_ENCRYPT_BLOCK, charset);
    }
    
    /**
     * 使用公钥解密
     *
     * @param ciphertext 密文(base64)
     * @param publicKey  公钥
     * @param charset    字符编码
     * @return 解密后的字符串
     * @throws Exception 如果解密失败
     */
    public static String decryptWithPublicKey(String ciphertext, String publicKey, String charset) throws Exception {
        byte[] data = Base64.decodeBase64(ciphertext);
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        Key key = keyFactory.generatePublic(x509KeySpec);
        
        return getCipherText(data, key, Cipher.DECRYPT_MODE, MAX_DECRYPT_BLOCK, charset);
    }
    
    /**
     * 获取加密或解密后的文本
     *
     * @param data     待处理的数据
     * @param key      密钥
     * @param mode     模式(加密或解密)
     * @param maxBlock 每次处理的最大数据块大小
     * @param charset  字符编码
     * @return 处理后的文本
     * @throws Exception 如果处理失败
     */
    private static String getCipherText(byte[] data, Key key, int mode, int maxBlock, String charset) throws Exception {
        Cipher cipher = Cipher.getInstance(PADDING_FORMAT);
        cipher.init(mode, key);
        int length = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        
        // 对数据分段处理
        while (length - offSet > 0) {
            int len = Math.min(length - offSet, maxBlock);
            cache = cipher.doFinal(data, offSet, len);
            out.write(cache, 0, cache.length);
            
            i++;
            offSet = i * maxBlock;
        }
        
        byte[] cipherBytes = out.toByteArray();
        out.close();
        
        if (mode == Cipher.ENCRYPT_MODE) {
            return Base64.encodeBase64String(cipherBytes); // 返回加密后的base64字符串
        } else {
            return new String(cipherBytes, charset); // 返回解密后的字符串
        }
    }
    
    public static void main(String[] args) {
        try {
            // 创建密钥对
            Map<String, String> keys = generateKeyPair();
            System.out.println("==================================================生成Public Key======================================================");
            String pubKey = keys.get("RSAPublicKey");
            System.out.println(pubKey);
            
            System.out.println();
            
            System.out.println("==================================================生成Private Key======================================================");
            String priKey = keys.get("RSAPrivateKey");
            System.out.println(priKey);
            
            System.out.println();
            
            // 测试加密文本
            String text = "123";
            System.out.println("==================================================公钥加密,私钥解密======================================================");
            String ciphertext = encrypt(text, pubKey, CHARSET_UTF_8);
            System.out.println("明文:" + text);
            System.out.println("加密后文本:" + ciphertext);
            System.out.println("解密后文本:" + decrypt(ciphertext, priKey, CHARSET_UTF_8));
            
            System.out.println();
            
            System.out.println("==================================================私钥加密,公钥解密======================================================");
            ciphertext = encryptWithPrivateKey(text, priKey, CHARSET_UTF_8);
            System.out.println("明文:" + text);
            System.out.println("加密后文本:" + ciphertext);
            System.out.println("解密后文本:" + decryptWithPublicKey(ciphertext, pubKey, CHARSET_UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


http://www.kler.cn/a/411081.html

相关文章:

  • spf算法、三类LSA、区间防环路机制/规则、虚连接
  • 【AIGC】如何准确引导ChatGPT,实现精细化GPTs指令生成
  • Delphi ADO组件中的 ADOTable、ADOQurey 无SQL语句实现增、删、改、查
  • 【数据结构与算法】合并链表、链表分割、链表回文结构
  • oracle会话追踪
  • [M最短路] lc743. 网络延迟时间(spfa最短路+单源最短路)
  • Bean的生命周期详解保姆级教程,结合spring boot和spring.xml两种方式讲解,5/7/10大小阶段详细分析
  • 基于Matlab卷积神经网络的肺癌检测系统(良性、恶性及正常病例分类的综合分析)
  • 【Vue3+Pinia】Vue新一代状态管理器Pinia
  • 三、计算机视觉_08YOLO目标检测
  • [仓颉Cangjie刷题模板] 优先队列(含小顶堆实现)
  • 开展网络安全成熟度评估:业务分析师的工具和技术
  • postgres-howto 学习笔记
  • 微信小程序学习指南从入门到精通
  • 【2024】前端学习笔记19-ref和reactive使用
  • SpringCloud之Eureka:服务注册与发现全面教程!
  • TCP/IP学习笔记
  • 平安科技Java面试题及参考答案
  • 浅谈人工智能之基于容器云进行文生视频大模型搭建
  • 零基础学安全--云技术基础
  • Vue3常见Composition API详解(适用Vue2学习进入Vue3学习)
  • testImplementation和androidTestImplementation区别
  • 力扣 53. 最大子数组和
  • 《PH47 快速开发教程》发布
  • 华三(HCL)和华为(eNSP)模拟器共存安装手册
  • SpringBoot - 优雅的实现【账号登录错误次数的限制和锁定】