证书工具

This commit is contained in:
cheney 2026-06-10 15:01:42 +08:00
parent cec433ad3b
commit c79aae14e8

View File

@ -0,0 +1,215 @@
package sample;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sunyard.RetWrap;
import racal.sunyard.main.SydApi4j;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class CertKit {
private static String ip1;
private static String ip2;
private static int port;
private static int timeout = 3000;
private static SydApi4j api;
private static void printHelp() {
System.out.println("CertKit - 证书管理工具");
System.out.println("用法: java -jar CertKit.jar <命令> [参数]");
System.out.println();
System.out.println("命令列表:");
System.out.println(" help 显示此帮助信息");
System.out.println(" import <证书文件路径> [机构号] 上传证书到加密设备");
System.out.println(" delete <证书DN> 从加密设备删除指定证书");
System.out.println(" list 列出加密设备中的所有证书");
System.out.println(" set <key> <value> 设置配置项并保存到 config.json");
System.out.println();
System.out.println("可用配置项:");
System.out.println(" ip1 加密设备主IP地址");
System.out.println(" ip2 加密设备备IP地址");
System.out.println(" port 加密设备端口号");
System.out.println(" timeout 连接超时时间(毫秒)");
System.out.println(" dn 默认证书DN");
System.out.println();
System.out.println("配置文件: config.json (需放在当前目录)");
System.out.println(" { \"ip1\": \"172.1.41.3\", \"ip2\": \"172.1.41.139\", \"port\": 8889 }");
}
private static void loadConfig() {
String workDir = System.getProperty("user.dir");
Path filePath = Paths.get(workDir, "config.json");
if (FileUtil.exist(filePath.toFile())) {
String configStr = FileUtil.readString(filePath.toFile(), StandardCharsets.UTF_8);
JSONObject config = JSONUtil.parseObj(configStr);
ip1 = config.getStr("ip1");
ip2 = config.getStr("ip2");
port = config.getInt("port");
System.out.println("配置已加载: ip1=" + ip1 + ", ip2=" + ip2 + ", port=" + port);
} else {
System.err.println("警告: 未找到配置文件 config.json使用默认值");
ip1 = "172.1.41.3";
ip2 = "172.1.41.139";
port = 8889;
}
}
private static void connect() {
SydApi4j.SYD_Set_Parameter_HA(ip1, ip2, port, timeout);
api = (SydApi4j) new SydApi4j();
System.out.println("已连接到加密设备");
}
private static void disconnect() {
if (api != null) {
api.disconnect();
System.out.println("已断开连接");
}
}
private static void importCert(String certFilePath, String organizationId) {
try {
if (!FileUtil.exist(certFilePath)) {
System.err.println("错误: 证书文件不存在 - " + certFilePath);
return;
}
byte[] certData = FileUtil.readBytes(certFilePath);
byte[] orgIdBytes = organizationId != null ? organizationId.getBytes(StandardCharsets.UTF_8) : null;
RetWrap result = api.importCert(certData, orgIdBytes);
System.out.println("证书上传成功");
if (result != null) {
System.out.println("返回结果: " + result.toString());
}
} catch (Exception e) {
System.err.println("证书上传失败: " + e.getMessage());
e.printStackTrace();
}
}
private static void deleteCert(String dn) {
try {
boolean result = api.deleteCert(dn);
if (result) {
System.out.println("证书删除成功");
} else {
System.out.println("证书删除失败");
}
} catch (Exception e) {
System.err.println("证书删除失败: " + e.getMessage());
e.printStackTrace();
}
}
private static void listCerts() {
try {
List<String> certs = api.getAllCert();
System.out.println("证书列表 (" + certs.size() + " 个):");
for (int i = 0; i < certs.size(); i++) {
System.out.println((i + 1) + ". " + certs.get(i));
}
} catch (Exception e) {
System.err.println("获取证书列表失败: " + e.getMessage());
e.printStackTrace();
}
}
private static void setConfig(String key, String value) {
String workDir = System.getProperty("user.dir");
Path filePath = Paths.get(workDir, "config.json");
JSONObject config;
if (FileUtil.exist(filePath.toFile())) {
String configStr = FileUtil.readString(filePath.toFile(), StandardCharsets.UTF_8);
config = JSONUtil.parseObj(configStr);
} else {
config = new JSONObject();
}
config.set(key, value);
try {
FileUtil.writeString(JSONUtil.toJsonStr(config), filePath.toFile(), StandardCharsets.UTF_8);
System.out.println("配置已保存: " + key + " = " + value);
} catch (Exception e) {
System.err.println("保存配置失败: " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length == 0) {
printHelp();
System.exit(1);
}
String command = args[0].toLowerCase();
if ("help".equals(command)) {
printHelp();
return;
}
if ("set".equals(command)) {
if (args.length < 3) {
System.err.println("错误: set 命令需要 key 和 value 参数");
System.err.println("用法: set <key> <value>");
} else {
String key = args[1];
String value = args[2];
setConfig(key, value);
}
return;
}
loadConfig();
try {
connect();
switch (command) {
case "import":
if (args.length < 2) {
System.err.println("错误: import 命令需要证书文件路径参数");
System.err.println("用法: import <证书文件路径> [机构号]");
} else {
String certFilePath = args[1];
String organizationId = args.length > 2 ? args[2] : null;
importCert(certFilePath, organizationId);
}
break;
case "delete":
if (args.length < 2) {
System.err.println("错误: delete 命令需要证书DN参数");
System.err.println("用法: delete <证书DN>");
} else {
String dn = args[1];
deleteCert(dn);
}
break;
case "list":
listCerts();
break;
default:
System.err.println("未知命令: " + command);
printHelp();
break;
}
} catch (Exception e) {
System.err.println("执行命令失败: " + e.getMessage());
e.printStackTrace();
} finally {
disconnect();
}
}
}