RSAUtils 非对称加密hutool

1、

使用hutool的rsa加解密工具,自定义公钥私钥字符串

2、

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.CN;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

@Slf4j
public class RSAUtils {
    private static RSA rsa;

    static {
        try {
            rsa = new RSA();
            rsa.setPublicKey(getPublicKeyFromPem("pem/public.pem"));
            rsa.setPrivateKey(getPrivateKeyFromPem("pem/private.pem"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String s1 = "大概和我说fadg`12@$%&+%";
        String s2 = encrypt(s1);
        String s3 = decrypt(s2);
        System.out.printf("======== \ns1=%s \ns2=%s \ns3=%s \ns1.equals(s3)=%s \n", s1, s2, s3, s1.equals(s3));
    }

    public static String encrypt(String data) {
        String cc = rsa.encryptBase64(data, KeyType.PublicKey);
        return cc;
    }

    public static String decrypt(String data) {
        String cc = rsa.decryptStr(data, KeyType.PrivateKey);
        return cc;
    }

    public static byte[] encrypt(byte[] data) {
        byte[] bytes = rsa.encrypt(data, KeyType.PublicKey);
        return bytes;
    }

    public static byte[] decrypt(byte[] data) {
        byte[] bytes = rsa.decrypt(data, KeyType.PrivateKey);
        return bytes;
    }

    private static PrivateKey getPrivateKeyFromPem(String fileName) {
        try {
            String str = getResourceFileContent(fileName);
            byte[] b = Base64Decoder.decode(str);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(b);
            PrivateKey privateKey = kf.generatePrivate(keySpec);
            return privateKey;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static PublicKey getPublicKeyFromPem(String fileName) {
        try {
            String str = getResourceFileContent(fileName);
            byte[] b = Base64Decoder.decode(str);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(b);
            PublicKey pubKey = kf.generatePublic(keySpec);
            return pubKey;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String getResourceFileContent(String fileName) {
        InputStream publicKeyStream = RSAUtils.class.getClassLoader().getResourceAsStream(fileName);
        try (BufferedReader br = new BufferedReader(new InputStreamReader(publicKeyStream))) {
            String line;
            StringBuilder str = new StringBuilder();
            while (CN.isNotEmpty((line = br.readLine()))) {
                if (line.startsWith("-")) {
                    continue;
                }
                str.append(line);
            }
            return str.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
posted @ 2024-05-30 15:31  一只桔子2233  阅读(99)  评论(0编辑  收藏  举报