java实现RSA加解密(不探究底层原理,只管代码实现)

package com.example.xxx.utls;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
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.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: deng
 * @Date: 2024/2/6 10:11
 */
public class EncryptUtil {
    public static Map<String, String> keyMap = new HashMap<>();

    public static void main(String[] args) throws Exception {
        generatorKeys();
        String content = "fasf3432#@(";
        String encrypt = encrypt(content, keyMap.get("pubk"));
        decrypt(encrypt, keyMap.get("prik"));
    }

    /**
     * 生成密钥对
     */
    public static void generatorKeys() throws NoSuchAlgorithmException {
        KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
        // 初始化长度
        rsa.initialize(2048);
        // 生成密钥对
        KeyPair keyPair = rsa.generateKeyPair();
        // 获取公钥
        PublicKey publicKey = keyPair.getPublic();
        // 获取私钥
        PrivateKey privateKey = keyPair.getPrivate();

        // 获取公钥,私钥的编码
        byte[] publicKeyEncoded = publicKey.getEncoded();
        byte[] privateKeyEncoded = privateKey.getEncoded();

        // 生成密钥对字符串
        Base64.Encoder encoder = Base64.getEncoder();
        String pulicKeyString = encoder.encodeToString(publicKeyEncoded);
        String privateKeyString = encoder.encodeToString(privateKeyEncoded);
        System.out.println("pubk:"+pulicKeyString);
        System.out.println("prik:"+privateKeyString);
        keyMap.put("pubk", pulicKeyString);
        keyMap.put("prik", privateKeyString);
    }

    /**
     * 加密
     * @param content
     * @param pubk
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static String encrypt(String content, String pubk) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        // 对公钥字符串base64解码
        byte[] pubkDecode = Base64.getDecoder().decode(pubk);
        RSAPublicKey rsaPublicKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubkDecode));
        Cipher cipher = Cipher.getInstance("RSA");
        // 初始化加密模式
        cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
        byte[] doFinal = cipher.doFinal(content.getBytes());
        // 生成加密后的base64编码字符串
        String encryptString = Base64.getEncoder().encodeToString(doFinal);
        System.out.println("encryptString: "+encryptString);
        return encryptString;
    }

    /**
     * 解密
     * @param content
     * @param prik
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static void decrypt(String content, String prik) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        // base64解码
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] contentDecode = decoder.decode(content);
        byte[] prikDecode = decoder.decode(prik);

        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(prikDecode));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
        byte[] doFinal = cipher.doFinal(contentDecode);
//        String decryptStr = new String(doFinal, 0, 0, doFinal.length);
        String decryptStr = new String(doFinal);
        System.out.println("decryptStr: "+decryptStr);
    }
}

运行结果:
在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值