| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import cn.hutool.crypto.SmUtil;
- import cn.hutool.crypto.asymmetric.SM2;
- import cn.hutool.crypto.digest.SM3;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
- import com.bskj.framework.license.model.LicenseInfo;
- import java.nio.charset.StandardCharsets;
- import java.util.Base64;
- public class TestSignature {
- public static void main(String[] args) throws Exception {
- // 读取现有的许可证文件
- ObjectMapper mapper = new ObjectMapper();
- mapper.registerModule(new JavaTimeModule());
-
- LicenseInfo license = mapper.readValue(new java.io.File("license.json"), LicenseInfo.class);
-
- // 加载私钥
- String privateKeyContent = new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("private.key")));
- privateKeyContent = privateKeyContent.replace("-----BEGIN PRIVATE KEY-----", "")
- .replace("-----END PRIVATE KEY-----", "")
- .replaceAll("\\s", "");
-
- // 加载公钥
- String publicKeyContent = new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("public.key")));
- publicKeyContent = publicKeyContent.replace("-----BEGIN PUBLIC KEY-----", "")
- .replace("-----END PUBLIC KEY-----", "")
- .replaceAll("\\s", "");
-
- byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyContent);
- byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyContent);
- SM2 sm2 = SmUtil.sm2(privateKeyBytes, publicKeyBytes);
-
- // 重新生成签名数据
- String signatureData = createSignatureData(license);
- System.out.println("Signature data: " + signatureData);
- System.out.println("Data length: " + signatureData.length());
-
- // Generate new signature
- SM3 sm3 = new SM3();
- byte[] hashedData = sm3.digest(signatureData.getBytes(StandardCharsets.UTF_8));
- byte[] signatureBytes = sm2.sign(hashedData);
- String newSignature = Base64.getEncoder().encodeToString(signatureBytes);
-
- System.out.println("Original signature: " + license.getSignature());
- System.out.println("New signature: " + newSignature);
-
- // Verify new signature
- boolean isValid = sm2.verify(hashedData, signatureBytes);
- System.out.println("Signature valid: " + isValid);
-
- // Update license file
- license.setSignature(newSignature);
- mapper.writeValue(new java.io.File("license_fixed.json"), license);
- System.out.println("Fixed license saved to license_fixed.json");
- }
-
- private static String createSignatureData(LicenseInfo licenseInfo) {
- StringBuilder sb = new StringBuilder();
-
- appendField(sb, "licenseId", licenseInfo.getLicenseId());
- appendField(sb, "customerName", licenseInfo.getCustomerName());
- appendField(sb, "productName", licenseInfo.getProductName());
- appendField(sb, "productVersion", licenseInfo.getProductVersion());
- appendField(sb, "createdTime", licenseInfo.getCreatedTime());
- appendField(sb, "startTime", licenseInfo.getStartTime());
- appendField(sb, "endTime", licenseInfo.getEndTime());
- appendField(sb, "strategy", licenseInfo.getStrategy());
- appendField(sb, "gracePeriodDays", licenseInfo.getGracePeriodDays());
- appendField(sb, "warningDays", licenseInfo.getWarningDays());
- appendField(sb, "maxUsers", licenseInfo.getMaxUsers());
-
- return sb.toString();
- }
-
- private static void appendField(StringBuilder sb, String fieldName, Object value) {
- sb.append(fieldName).append("=");
- if (value != null) {
- sb.append(value.toString());
- }
- sb.append(";");
- }
- }
|