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;
    }

}
相关推荐
Dizzy.5175 分钟前
数据结构(查找)
数据结构·学习·算法
yngsqq2 小时前
c# —— StringBuilder 类
java·开发语言
星星点点洲2 小时前
【操作幂等和数据一致性】保障业务在MySQL和COS对象存储的一致
java·mysql
xiaolingting3 小时前
JVM层面的JAVA类和实例(Klass-OOP)
java·jvm·oop·klass·instanceklass·class对象
分别努力读书3 小时前
acm培训 part 7
算法·图论
武乐乐~3 小时前
欢乐力扣:赎金信
算法·leetcode·职场和发展
风口上的猪20153 小时前
thingboard告警信息格式美化
java·服务器·前端
'Debug3 小时前
算法从0到100之【专题一】- 双指针第一练(数组划分、数组分块)
算法
Fansv5873 小时前
深度学习-2.机械学习基础
人工智能·经验分享·python·深度学习·算法·机器学习
追光少年33224 小时前
迭代器模式
java·迭代器模式