Java实现RSA非对称加密算法demo工具类,RSA加密解密

Java RSA非对称加密算法demo工具类

安全性:512位的密钥被视为不安全的;768位的密钥不用担心受到除了国家安全管理(NSA)外的其他事物的危害;1024位的密钥几乎是安全的。
运算速度:慢,RSA的速度比对应同样安全级别的对称密码算法要慢1000倍左右
AES对称算法demo:java实现AES 加密解密工具类,AES加密解密.
/**
 * @program: test
 * @description: RSA 非对称加密算法(存在加密公钥、解密私钥) 工具类
 * @author: 闲走天涯
 * @create: 2021-08-24 11:14
 */
public class RSAUtil {
    public static Map<String,String> map = new HashMap<>();

    //秘钥位数 大小为512-1024位
    public static final int KEY_SIZE = 1024;
    /**
     * 生成秘钥对
     * @throws NoSuchAlgorithmException
     */
    public static void createKey() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        //初始化密钥对生成器,密钥大小为96-1024位
        keyPairGen.initialize(KEY_SIZE, new SecureRandom());
        //生成一个密钥对,保存在keyPair中
        KeyPair keyPair = keyPairGen.generateKeyPair();
        //私钥
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        //公钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        //公钥字符串
        String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
        //私钥字符串
        String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
        //保存公钥
        map.put("publicKey", publicKeyString);
        //保存私钥
        map.put("privateKey", privateKeyString);
    }

    /**
     * RSA公钥加密
     * @param encodeStr 需要加密的字符串
     * @param publicKey 公钥
     * @return 密文
     * @throws Exception
     */
    public static String encode(String encodeStr,String publicKey) {
        try {
            //公钥base64解密
            byte[] decode = Base64.decodeBase64(publicKey);
            //key工厂获取RSA对象,根据X509EncodedKeySpec生成公钥对象
            RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decode));
            //RSA加密
            //获取Cipher RSA对象
            Cipher cipher = Cipher.getInstance("RSA");
            //初始化
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            //base64加密
            String out = Base64.encodeBase64String(cipher.doFinal(encodeStr.getBytes(StandardCharsets.UTF_8)));
            return out;
        }catch (Exception e){
            e.printStackTrace();
        }
        //加密失败
        return null;
    }

    /**
     * RSA解密
     * @param decodeStr 密文
     * @param privateKey 私钥
     * @return 明文
     * @throws Exception
     */
    public static String decode(String decodeStr,String privateKey) {
        try{
            //密文base64解密
            byte[] bytes = Base64.decodeBase64(decodeStr.getBytes(StandardCharsets.UTF_8));
            //私钥base64解密
            byte[] decode = Base64.decodeBase64(privateKey);
            //key工厂获取RSA对象,根据PKCS8EncodedKeySpec生成公钥对象
            RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decode));
            //RSA解密
            //获取Cipher RSA对象
            Cipher cipher = Cipher.getInstance("RSA");
            //初始化
            cipher.init(Cipher.DECRYPT_MODE,priKey);
            //解密
            String out = new String(cipher.doFinal(bytes));
            return out;
        }catch (Exception e){
            e.printStackTrace();
        }
        //加密失败
        return null;
    }

    public static void main(String[] args) throws Exception {
        System.out.println("-------------RSA加密 非对称加密算法---开始--------");
        //RSA加密 非对称加密算法
        RSAUtil.createKey();
        String str = "你好,中国!";
        String encodeStr = RSAUtil.encode(str,RSAUtil.map.get("publicKey"));
        String decodeStr = RSAUtil.decode(encodeStr,RSAUtil.map.get("privateKey"));
        System.out.println("待加密明文:"+str);
        System.out.println("已加密密文:"+encodeStr);
        System.out.println("已解密明文:"+decodeStr);
        System.out.println("公钥:"+RSAUtil.map.get("publicKey").length());
        System.out.println("秘钥:"+RSAUtil.map.get("privateKey").length());
        System.out.println("-------------RSA加密 非对称加密算法---结束--------");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值