管道基础大数据平台系统开发-【后端】-Server
13693261870
2024-03-25 b6b0cb226fcf184525ee7b36af3a09471e9c0057
src/main/java/com/lf/server/helper/RsaHelper.java
@@ -1,10 +1,13 @@
package com.lf.server.helper;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import javax.crypto.Cipher;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
@@ -13,11 +16,9 @@
import java.util.HashMap;
import java.util.Map;
/**
 * RSA工具类 默认长度为2048位
 * @author LinTao
 * @date 2022/2/13
 * RSA工具类
 * @author WWW
 */
public class RsaHelper {
    /**
@@ -30,12 +31,20 @@
     */
    private static String publicKey;
    /**
     * 密钥算法
     */
    private static final String KEY_ALGORITHM = "RSA";
    /**
     * RSA密钥长度:1024 或 2048
     */
    private static final int DEFAULT_RSA_KEY_SIZE = 1024;
    /**
     * 日志
     */
    private final static Log log = LogFactory.getLog(RsaHelper.class);
    /**
     * 生成公私钥
@@ -145,8 +154,8 @@
            // 得到私钥字符串
            String pri = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
            result.put("privateKey", pri);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return result;
@@ -161,7 +170,7 @@
     */
    public static String decrypt(String str) throws Exception {
        // 64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
        byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8));
        // Base64编码的私钥
        byte[] decoded = Base64.decodeBase64(getPrivateKey());
@@ -193,7 +202,7 @@
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
        return outStr;
    }