hwj 1 year ago
commit c4e669cc40
  1. 33
      .gitignore
  2. 114
      pom.xml
  3. 13
      src/main/java/com/dky/KeyGenerateToolApplication.java
  4. 21
      src/main/java/com/dky/controller/GenerateKeyController.java
  5. 16
      src/main/java/com/dky/entity/KeyInfo.java
  6. 7
      src/main/java/com/dky/service/GenerateKey.java
  7. 5
      src/main/java/com/dky/service/GetPrivateKey.java
  8. 54
      src/main/java/com/dky/service/Iml/GenerateKeyIml.java
  9. 14
      src/main/java/com/dky/service/Iml/GetPrivateKeyIml.java
  10. 20
      src/main/resources/application.yml
  11. 109
      src/main/resources/static/index.html
  12. 13
      src/test/java/com/dky/KeyGenerateToolApplicationTests.java

33
.gitignore vendored

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dky</groupId>
<artifactId>key-generate-tool</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>key-generate-tool</name>
<description>key-generate-tool</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.dky.KeyGenerateToolApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,13 @@
package com.dky;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class KeyGenerateToolApplication {
public static void main(String[] args) {
SpringApplication.run(KeyGenerateToolApplication.class, args);
}
}

@ -0,0 +1,21 @@
package com.dky.controller;
import com.dky.service.GenerateKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
import java.util.Map;
@Controller
public class GenerateKeyController {
@Autowired
GenerateKey generateKey;
@RequestMapping("/get_key")
public String getGenerateKey() {
Map map = new HashMap();
map.put("name", "dky");
return generateKey.generateKey(map);
}
}

@ -0,0 +1,16 @@
package com.dky.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class KeyInfo {
public Integer id;
public String privateKey;
public String cpuId;
public String companyName;
public String key;
}

@ -0,0 +1,7 @@
package com.dky.service;
import java.util.Map;
public interface GenerateKey {
public String generateKey(Map map);
}

@ -0,0 +1,5 @@
package com.dky.service;
public interface GetPrivateKey {
public String getPrivateKey();
}

@ -0,0 +1,54 @@
package com.dky.service.Iml;
import com.dky.service.GenerateKey;
import org.springframework.stereotype.Service;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Base64;
import java.util.Map;
@Service
public class GenerateKeyIml implements GenerateKey {
private static final String SM4_KEY = "sm4demo123456789"; // 16 bytes key
private static final String ALGORITHM = "SM4/ECB/PKCS5Padding";
@Override
public String generateKey(Map map) {
String key;
try {
Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
SecretKeySpec secretKey = new SecretKeySpec(SM4_KEY.getBytes(StandardCharsets.UTF_8), "SM4");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(mapToString(map).getBytes(StandardCharsets.UTF_8));
key=Base64.getEncoder().encodeToString(encrypted);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}
return key;
}
private static String mapToString(Map<String, String> map) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : map.entrySet()) {
sb.append(entry.getKey()).append('=').append(entry.getValue()).append('&');
}
return sb.toString();
}
}

@ -0,0 +1,14 @@
package com.dky.service.Iml;
import com.dky.service.GenerateKey;
import java.util.Map;
public class GetPrivateKeyIml implements GenerateKey {
@Override
public String generateKey(Map map) {
//都数据库mybatis
return null;
}
}

@ -0,0 +1,20 @@
server:
port: 8080
spring:
thymeleaf:
cache: false
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/key_generate_tool?serverTimezone=GMT%2B8&useSSL=true
username: root
password: hwj_lyq
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: classpath:mapper/*.xml
devtools:
restart:
enabled: true

@ -0,0 +1,109 @@
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<head>
<title>密钥生成工具</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.key-row {
margin-bottom: 10px;
}
.main{
width: 50%; /* 调整这个值以改变div的宽度 */
height: 50%;
background-color: #f32929;
color: #fff;
}
.title{
text-align: center;
font-size: 15px;
}
.formdiv{
display: flex;
width: 500px;
margin-top: 50px;
justify-content: center;
border: 1px solid #ccc;
margin:auto;
.key-form{
margin-top: 20px;
}
}
.inputCpuid{
height: 100px;
width: 320px;
border: 2px solid #ccc;
overflow-y: auto;
}
</style>
<script>
$(document).ready(function() {
// 处理复制按钮点击事件
$('.copy-button').click(function() {
var privateKey = $(this).prev().val();
copyTextToClipboard(privateKey);
alert('私钥已复制到剪贴板!');
});
// 处理更换按钮点击事件
$('.change-button').click(function() {
var privateKeyInput = $(this).parent().find('input[name="privateKey"]');
var newPrivateKey = privateKeyInput.val();
privateKeyInput.val('');
$(this).before('<p class="key-row">私钥:' + newPrivateKey + '</p>');
$(this).remove();
});
});
// 使用jQuery的copyTextToClipboard函数实现复制到剪贴板
function copyTextToClipboard(text) {
var $textarea = $('<textarea>');
$textarea.text(text);
$('body').append($textarea);
$textarea.select();
document.execCommand('copy');
$textarea.remove();
}
</script>
</head>
<div class="main">
<div class="title"><h1>密钥生成工具</h1></div>
<div class="formdiv">
<form class="key-form" action="/generate" method="post">
<div class="key-row">
<label for="privateKey">私钥:</label>
<input type="text" name="privateKey" readonly>
<button class="copy-button">复制</button>
<button class="change-button">更换</button>
</div>
<div class="key-row">
<label for="cpuid">cpuid:</label>
<div class="inputCpuid">
<span>djiefieji;</span>
<span>djiefieji;</span>
<span>djiefieji;</span>
<span>djiefieji;</span>
</div>
<button type="button" onclick="addElement()">添加</button>
</div>
<div class="key-row">
<div class="key-row">
<label for="expiration">有效期:</label>
<input type="datetime-local" name="expiration">
</div>
<div class="key-row">
<label for="unitName">单位名称:</label>
<input type="text" name="unitName">
</div>
<button type="submit">生成</button>
</form>
</div>
</div>
</body>
</html>

@ -0,0 +1,13 @@
package com.dky;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class KeyGenerateToolApplicationTests {
@Test
void contextLoads() {
}
}
Loading…
Cancel
Save