RSA非对称加密

1.生成密钥对

 /**
     * 生成密钥对
     * @return
     * @throws Exception
     */
    public static KeyPair generateKeyPair() throws Exception {
        // 获取KeyPairGenerator实例,并指定使用RSA算法
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        // 初始化KeyPairGenerator,设置密钥大小
        keyPairGenerator.initialize(2048);
        // 生成密钥对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }

2.使用公钥加密

 /**
     * 使用公钥加密
     * @param plainText
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
        // 获取Cipher实例,并指定使用RSA算法
        Cipher cipher = Cipher.getInstance("RSA");
        // 初始化Cipher为加密模式,并传入公钥
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        // 执行加密操作
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
        // 将加密后的字节数组转换为Base64编码的字符串
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

3.使用私钥解密

/**
     * 使用私钥解密
     * @param encryptedText
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
        // 获取Cipher实例,并指定使用RSA算法
        Cipher cipher = Cipher.getInstance("RSA");
        // 初始化Cipher为解密模式,并传入私钥
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        // 将Base64编码的字符串转换为字节数组
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
        // 执行解密操作
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        // 将解密后的字节数组转换为字符串
        return new String(decryptedBytes, "UTF-8");
    }

4.获取公钥字符串

 /**
     * 获取公钥字符串
     * @param publicKey
     * @return
     */
    public static String encodePublicKeyToBase64(PublicKey publicKey) {
        // 获取公钥的字节表示
        byte[] publicKeyBytes = publicKey.getEncoded();
        // 对字节数组进行Base64编码
        String encodedPublicKey = Base64.getEncoder().encodeToString(publicKeyBytes);
        return encodedPublicKey;
    }

5.公钥字符串构建PublicKey对象

 /**
     * 公钥字符串构建PublicKey对象
     * @param encodedPublicKey
     * @return
     * @throws Exception
     */
    public static PublicKey decodePublicKeyFromBase64(String encodedPublicKey) throws Exception {
        // 对Base64编码的字符串进行解码
        byte[] publicKeyBytes = Base64.getDecoder().decode(encodedPublicKey);

        // 根据公钥的算法(这里假设是RSA)创建KeyFactory
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        // 使用X509EncodedKeySpec来解析公钥的字节表示
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);

        // 生成PublicKey对象
        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        return publicKey;
    }

6.示例

  public static void main(String[] args) {
        try {
            // 生成密钥对
            KeyPair keyPair = generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
            
            // 模仿第三方应用拿到公钥字符串生成PublicKey
             String publicKeyString = encodePublicKeyToBase64(publicKey);
             PublicKey newPublicKey = decodePublicKeyFromBase64(publicKeyString);

            // 原始文本
            String originalText = "Hello, RSA!";

            // 使用公钥加密
            String encryptedText = encrypt(originalText, newPublicKey);
            System.out.println("Encrypted Text: " + encryptedText);

            // 使用私钥解密
            String decryptedText = decrypt(encryptedText, privateKey);
            System.out.println("Decrypted Text: " + decryptedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值