TestSignature.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import cn.hutool.crypto.SmUtil;
  2. import cn.hutool.crypto.asymmetric.SM2;
  3. import cn.hutool.crypto.digest.SM3;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  6. import com.bskj.framework.license.model.LicenseInfo;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.Base64;
  9. public class TestSignature {
  10. public static void main(String[] args) throws Exception {
  11. // 读取现有的许可证文件
  12. ObjectMapper mapper = new ObjectMapper();
  13. mapper.registerModule(new JavaTimeModule());
  14. LicenseInfo license = mapper.readValue(new java.io.File("license.json"), LicenseInfo.class);
  15. // 加载私钥
  16. String privateKeyContent = new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("private.key")));
  17. privateKeyContent = privateKeyContent.replace("-----BEGIN PRIVATE KEY-----", "")
  18. .replace("-----END PRIVATE KEY-----", "")
  19. .replaceAll("\\s", "");
  20. // 加载公钥
  21. String publicKeyContent = new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("public.key")));
  22. publicKeyContent = publicKeyContent.replace("-----BEGIN PUBLIC KEY-----", "")
  23. .replace("-----END PUBLIC KEY-----", "")
  24. .replaceAll("\\s", "");
  25. byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyContent);
  26. byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyContent);
  27. SM2 sm2 = SmUtil.sm2(privateKeyBytes, publicKeyBytes);
  28. // 重新生成签名数据
  29. String signatureData = createSignatureData(license);
  30. System.out.println("Signature data: " + signatureData);
  31. System.out.println("Data length: " + signatureData.length());
  32. // Generate new signature
  33. SM3 sm3 = new SM3();
  34. byte[] hashedData = sm3.digest(signatureData.getBytes(StandardCharsets.UTF_8));
  35. byte[] signatureBytes = sm2.sign(hashedData);
  36. String newSignature = Base64.getEncoder().encodeToString(signatureBytes);
  37. System.out.println("Original signature: " + license.getSignature());
  38. System.out.println("New signature: " + newSignature);
  39. // Verify new signature
  40. boolean isValid = sm2.verify(hashedData, signatureBytes);
  41. System.out.println("Signature valid: " + isValid);
  42. // Update license file
  43. license.setSignature(newSignature);
  44. mapper.writeValue(new java.io.File("license_fixed.json"), license);
  45. System.out.println("Fixed license saved to license_fixed.json");
  46. }
  47. private static String createSignatureData(LicenseInfo licenseInfo) {
  48. StringBuilder sb = new StringBuilder();
  49. appendField(sb, "licenseId", licenseInfo.getLicenseId());
  50. appendField(sb, "customerName", licenseInfo.getCustomerName());
  51. appendField(sb, "productName", licenseInfo.getProductName());
  52. appendField(sb, "productVersion", licenseInfo.getProductVersion());
  53. appendField(sb, "createdTime", licenseInfo.getCreatedTime());
  54. appendField(sb, "startTime", licenseInfo.getStartTime());
  55. appendField(sb, "endTime", licenseInfo.getEndTime());
  56. appendField(sb, "strategy", licenseInfo.getStrategy());
  57. appendField(sb, "gracePeriodDays", licenseInfo.getGracePeriodDays());
  58. appendField(sb, "warningDays", licenseInfo.getWarningDays());
  59. appendField(sb, "maxUsers", licenseInfo.getMaxUsers());
  60. return sb.toString();
  61. }
  62. private static void appendField(StringBuilder sb, String fieldName, Object value) {
  63. sb.append(fieldName).append("=");
  64. if (value != null) {
  65. sb.append(value.toString());
  66. }
  67. sb.append(";");
  68. }
  69. }