48 lines
1.5 KiB
Java
48 lines
1.5 KiB
Java
package com.example.springboot4.util;
|
|
|
|
|
|
import org.springframework.util.ResourceUtils;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.security.PrivateKey;
|
|
import java.security.PublicKey;
|
|
|
|
/**
|
|
* RSA密钥加载器
|
|
* 用于从本地文件加载RSA密钥对
|
|
*/
|
|
public class RsaKeyLoader {
|
|
|
|
/**
|
|
* 从指定路径加载私钥
|
|
*
|
|
* @param privateKeyPath 私钥文件路径
|
|
* @return PrivateKey 私钥对象
|
|
* @throws IOException IO异常
|
|
* @throws ClassNotFoundException 类未找到异常
|
|
*/
|
|
public static PrivateKey loadPrivateKey(String privateKeyPath) throws IOException, ClassNotFoundException {
|
|
try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Path.of(privateKeyPath)))) {
|
|
return (PrivateKey) ois.readObject();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 从指定路径加载公钥
|
|
*
|
|
* @param publicKeyPath 公钥文件路径
|
|
* @return PublicKey 公钥对象
|
|
* @throws IOException IO异常
|
|
* @throws ClassNotFoundException 类未找到异常
|
|
*/
|
|
public static PublicKey loadPublicKey(String publicKeyPath) throws IOException, ClassNotFoundException {
|
|
try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Path.of(publicKeyPath)))) {
|
|
return (PublicKey) ois.readObject();
|
|
}
|
|
}
|
|
} |