RSA java加密解密验签

一:Java测试

二:pom

复制代码
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15to18</artifactId>
    <version>1.66</version>
</dependency>

三:工具类

复制代码
public class RsaUtils {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }
    public KeyPair genePublicPrivate() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        // 初始化密码生感器
        keyPairGenerator.initialize(512, new SecureRandom());
        //生成密钥对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        //获公钥
        PublicKey publicKey = keyPair.getPublic();
        // 获私钥
        PrivateKey privateKey = keyPair.getPrivate();
        System.out.println(getPublicKeyStr(publicKey));//公钥字符串
        System.err.println(getPrivateKeyStr(privateKey));//私钥字符串
        return keyPair;
    }

    public static PublicKey getPublicKey(String key) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(key);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }

    public static PrivateKey getPrivateKey(String key) throws Exception {
        byte[] keyBytes;
        keyBytes = Base64.getDecoder().decode(key);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }

    public static byte[] encrypt(byte[] bt_plaintext,String pubkey) throws Exception {
        PublicKey publicKey = getPublicKey(pubkey);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bt_encrypted = cipher.doFinal(bt_plaintext);
        return bt_encrypted;
    }

    public static byte[] decrypt(byte[] bt_encrypted,String prikey) throws Exception {
        PrivateKey privateKey = getPrivateKey(prikey);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] bt_original = cipher.doFinal(bt_encrypted);
        return bt_original;
    }

    /**
     * 获取私钥字符串
     *
     * @return 当前的私钥字符串
     */
    public static String getPrivateKeyStr(PrivateKey privateKey) {
        KeyFactory keyFactory = null;
        String privateKeyStr = null;
        try {
            keyFactory = KeyFactory.getInstance("RSA");
            PKCS8EncodedKeySpec keySpec = keyFactory.getKeySpec(privateKey, PKCS8EncodedKeySpec.class);
            byte[] buffer = keySpec.getEncoded();
            privateKeyStr = Base64.getEncoder().encodeToString(buffer);
        } catch (NoSuchAlgorithmException e) {
            System.err.println(e);
        } catch (InvalidKeySpecException e) {
            System.err.println(e);
        }
        return privateKeyStr;
    }

    /**
     * 获取公钥字符串
     *
     * @return 当前的公钥字符串
     */
    public static String getPublicKeyStr(PublicKey publicKey) {
        KeyFactory keyFactory = null;
        String publicKeyStr = null;
        try {
            keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = keyFactory.getKeySpec(publicKey, X509EncodedKeySpec.class);
            byte[] buffer = keySpec.getEncoded();
            publicKeyStr = Base64.getEncoder().encodeToString(buffer);
        } catch (NoSuchAlgorithmException e) {
            System.err.println(e);
        } catch (InvalidKeySpecException e) {
            System.err.println(e);
        }
        return publicKeyStr;
    }

}
相关推荐
llwszx1 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
云泽野2 小时前
【Java|集合类】list遍历的6种方式
java·python·list
二进制person2 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6663 小时前
C++讲解---创建日期类
开发语言·c++·算法
JoJo_Way3 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
小阳拱白菜3 小时前
java异常学习
java
.30-06Springfield3 小时前
人工智能概念之七:集成学习思想(Bagging、Boosting、Stacking)
人工智能·算法·机器学习·集成学习
FrankYoou4 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
麦兜*5 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了5 小时前
JAVA-springboot 整合Redis
java·spring boot·redis