rsa加密工具类

这是我的网站刚开始写的工具类,用rsa加密

package com.kongjs.diary.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RsaCryptoUtils {
    private static final Log logger = LogFactory.getLog(RsaCryptoUtils.class);
    private static byte[] publicKey;
    private static byte[] privateKey;
    private static byte[] slatKey;

    private RsaCryptoUtils() { }
    public static void init(byte[] slatKey){
        init(2048, slatKey);
    }
    public static void init(CharSequence slatKey) {
        init(2048, slatKey.toString().getBytes(StandardCharsets.UTF_8));
    }
    // 获取公钥私钥
    public static void init(int keySize, byte[] slatKey) {
        KeyPairGenerator keyPairGenerator;
        SecureRandom random;
        KeyPair keyPair;
        try {
            keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(slatKey);
            keyPairGenerator.initialize(keySize, random);// or 2048
            keyPair = keyPairGenerator.generateKeyPair(); // 生成密钥对
            PrivateKey aPrivate = keyPair.getPrivate();
            PublicKey aPublic = keyPair.getPublic();
            publicKey = aPublic.getEncoded();
            privateKey = aPrivate.getEncoded();
            logger.info("RSA 获取密钥对");
        } catch (NoSuchAlgorithmException | NullPointerException e) {
            logger.error("RSA 获取密钥对失败");
        }
    }

    public static String getStrPublicKey() {
        logger.info("RSA 公钥 base64字符串");
        return Base64.getEncoder().encodeToString(publicKey);
    }

    public static String getStrPrivateKey() {
        logger.info("RSA 私钥 base64字符串");
        return Base64.getEncoder().encodeToString(privateKey);
    }

    public static byte[] getPublicKey() {
        logger.info("RSA 公钥");
        return publicKey;
    }

    public static byte[] getPrivateKey() {
        logger.info("RSA 私钥");
        return privateKey;
    }

    public static String publicEncrypt(byte[] data, byte[] publicKey) {
        EncodedKeySpec keySpec;
        KeyFactory keyFactory;
        Cipher cipher;
        PublicKey key;
        byte[] bytes;
        String result = null;
        try {
            keySpec = new X509EncodedKeySpec(publicKey);
            keyFactory = KeyFactory.getInstance("RSA");
            cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
            key = keyFactory.generatePublic(keySpec);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            bytes = cipher.doFinal(data);
            result = Base64.getEncoder().encodeToString(bytes);
            logger.info("RSA 公钥加密");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            logger.error("RSA 公钥加密失败", e);
        }
        return result;
    }

    public static String publicEncrypt(String data, String publicKey) {
        return publicEncrypt(data.getBytes(StandardCharsets.UTF_8), Base64.getDecoder().decode(publicKey));
    }
    public static String publicEncrypt(String data){
        return publicEncrypt(Base64.getDecoder().decode(data),publicKey);
    }

    public static String privateDecrypt(byte[] data, byte[] privateKey) {
        PKCS8EncodedKeySpec keySpec;
        KeyFactory keyFactory;
        Cipher cipher;
        PrivateKey key;
        byte[] bytes;
        String result = null;
        try {
            keySpec = new PKCS8EncodedKeySpec(privateKey);
            keyFactory = KeyFactory.getInstance("RSA");
            cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
            key = keyFactory.generatePrivate(keySpec);
            cipher.init(Cipher.DECRYPT_MODE, key);
            bytes = cipher.doFinal(data);
            result = new String(bytes);
            logger.info("RSA 私钥解密");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            logger.error("RSA 私钥解密失败", e);
        }
        return result;
    }

    public static String privateDecrypt(String data, String privateKey) {
        return privateDecrypt(Base64.getDecoder().decode(data), Base64.getDecoder().decode(privateKey));
    }
    public static String privateDecrypt(String data) {
        return privateDecrypt(Base64.getDecoder().decode(data), privateKey);
    }
    public static byte[] getSlatKey() {
        return slatKey;
    }

    public static void setSlatKey(byte[] slatKey) {
        RsaCryptoUtils.slatKey = slatKey;
    }
    public static String getStrSlatKey() {
        return Base64.getEncoder().encodeToString(slatKey);
    }
    public static void setStrSlatKey(String slatKey) {
        RsaCryptoUtils.slatKey = slatKey.getBytes(StandardCharsets.UTF_8);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值