SM2Util.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. package com.ruoyi.common.utils;
  3. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  4. import org.springframework.stereotype.Component;
  5. import java.security.*;
  6. import java.security.spec.PKCS8EncodedKeySpec;
  7. import java.security.spec.X509EncodedKeySpec;
  8. import java.util.Base64;
  9. */
  10. /**
  11. * SM2非对称加密工具类 - 兼容版本
  12. *//*
  13. @Component
  14. public class SM2Util {
  15. static {
  16. Security.addProvider(new BouncyCastleProvider());
  17. }
  18. */
  19. /**
  20. * 生成SM2密钥对
  21. *//*
  22. public static KeyPair generateKeyPair() {
  23. try {
  24. // 使用标准的KeyPairGenerator
  25. KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
  26. // 使用SM2参数
  27. keyPairGenerator.initialize(256); // SM2使用256位
  28. return keyPairGenerator.generateKeyPair();
  29. } catch (Exception e) {
  30. throw new RuntimeException("生成SM2密钥对失败", e);
  31. }
  32. }
  33. */
  34. /**
  35. * 使用私钥对数据进行签名
  36. *//*
  37. public static String sign(byte[] data, PrivateKey privateKey) {
  38. try {
  39. Signature signature = Signature.getInstance("SM3withSM2", "BC");
  40. signature.initSign(privateKey);
  41. signature.update(data);
  42. byte[] signBytes = signature.sign();
  43. return Base64.getEncoder().encodeToString(signBytes);
  44. } catch (Exception e) {
  45. throw new RuntimeException("SM2签名失败", e);
  46. }
  47. }
  48. */
  49. /**
  50. * 使用公钥验证签名
  51. *//*
  52. public static boolean verify(byte[] data, String signature, PublicKey publicKey) {
  53. try {
  54. Signature verifier = Signature.getInstance("SM3withSM2", "BC");
  55. verifier.initVerify(publicKey);
  56. verifier.update(data);
  57. byte[] signatureBytes = Base64.getDecoder().decode(signature);
  58. return verifier.verify(signatureBytes);
  59. } catch (Exception e) {
  60. throw new RuntimeException("SM2验签失败", e);
  61. }
  62. }
  63. */
  64. /**
  65. * 从Base64字符串加载私钥
  66. *//*
  67. public static PrivateKey loadPrivateKey(String base64PrivateKey) {
  68. try {
  69. byte[] keyBytes = Base64.getDecoder().decode(base64PrivateKey);
  70. PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
  71. KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
  72. return keyFactory.generatePrivate(keySpec);
  73. } catch (Exception e) {
  74. System.out.println(ExceptionUtil.getRootErrorMessage(e));
  75. throw new RuntimeException("加载SM2私钥失败", e);
  76. }
  77. }
  78. */
  79. /**
  80. * 从Base64字符串加载公钥
  81. *//*
  82. public static PublicKey loadPublicKey(String base64PublicKey) {
  83. try {
  84. byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
  85. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
  86. KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
  87. return keyFactory.generatePublic(keySpec);
  88. } catch (Exception e) {
  89. throw new RuntimeException("加载SM2公钥失败", e);
  90. }
  91. }
  92. */
  93. /**
  94. * 将私钥转换为Base64字符串
  95. *//*
  96. public static String privateKeyToString(PrivateKey privateKey) {
  97. return Base64.getEncoder().encodeToString(privateKey.getEncoded());
  98. }
  99. */
  100. /**
  101. * 将公钥转换为Base64字符串
  102. *//*
  103. public static String publicKeyToString(PublicKey publicKey) {
  104. return Base64.getEncoder().encodeToString(publicKey.getEncoded());
  105. }
  106. }*/