ECDSAUtil.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.crm.rely.backend.util;
  2. import javax.xml.bind.DatatypeConverter;
  3. import java.security.*;
  4. import java.security.spec.ECGenParameterSpec;
  5. import java.security.spec.PKCS8EncodedKeySpec;
  6. import java.security.spec.X509EncodedKeySpec;
  7. /**
  8. * @Author houn
  9. * @Date 2024/4/22 12:51
  10. * @PackageName:com.crm.rely.backend.util
  11. * @ClassName: ECDSAUtil
  12. * @Description: TODO
  13. */
  14. public class ECDSAUtil {
  15. private static final String SIGNALGORITHMS = "SHA256withECDSA";
  16. private static final String ALGORITHM = "EC";
  17. private static final String SECP256K1 = "secp256k1";
  18. public static void main(String[] args) throws Exception {
  19. // 生成公钥私钥
  20. KeyPair keyPair1 = getKeyPair();
  21. PublicKey publicKey1 = keyPair1.getPublic();
  22. PrivateKey privateKey1 = keyPair1.getPrivate();
  23. //密钥转16进制字符串
  24. String publicKey = HexUtil.bytes2Hex(publicKey1.getEncoded());
  25. String privateKey = HexUtil.bytes2Hex(privateKey1.getEncoded());
  26. System.out.println("生成公钥:" + publicKey);
  27. System.out.println("生成私钥:" + privateKey);
  28. // //16进制字符串转密钥对象
  29. // PrivateKey privateKey2 = getPrivateKey(privateKey);
  30. // PublicKey publicKey2 = getPublicKey(publicKey);
  31. // //加签验签
  32. // String data = "需要签名的数据";
  33. // String signECDSA = signECDSA(privateKey2, data);
  34. // boolean verifyECDSA = verifyECDSA(publicKey2, signECDSA, data);
  35. // System.out.println("验签结果:" + verifyECDSA);
  36. System.out.println(signECDSA("bb06971bb6abb30da07f095a286e4e89ffd2b44cc83ea4d14fc68f85924884e1", "12"));
  37. }
  38. /**
  39. * 加签
  40. *
  41. * @param privateKey 私钥
  42. * @param data 数据
  43. * @return
  44. */
  45. public static String signECDSA(String privateKey, String data) throws Exception {
  46. return signECDSA(getPrivateKey(privateKey), data);
  47. }
  48. public static String signECDSA(PrivateKey privateKey, String data) {
  49. String result = "";
  50. try {
  51. //执行签名
  52. Signature signature = Signature.getInstance(SIGNALGORITHMS);
  53. signature.initSign(privateKey);
  54. signature.update(data.getBytes());
  55. byte[] sign = signature.sign();
  56. return HexUtil.bytes2Hex(sign);
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. return result;
  61. }
  62. public static boolean verifyECDSA(String publicKey, String signed, String data) throws Exception {
  63. return verifyECDSA(getPublicKey(publicKey), signed, data);
  64. }
  65. /**
  66. * 验签
  67. *
  68. * @param publicKey 公钥
  69. * @param signed 签名
  70. * @param data 数据
  71. * @return
  72. */
  73. public static boolean verifyECDSA(PublicKey publicKey, String signed, String data) {
  74. try {
  75. //验证签名
  76. Signature signature = Signature.getInstance(SIGNALGORITHMS);
  77. signature.initVerify(publicKey);
  78. signature.update(data.getBytes());
  79. byte[] hex = HexUtil.decode(signed);
  80. boolean bool = signature.verify(hex);
  81. // System.out.println("验证:" + bool);
  82. return bool;
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. return false;
  87. }
  88. /**
  89. * 从string转private key
  90. *
  91. * @param key 私钥的字符串
  92. * @return
  93. * @throws Exception
  94. */
  95. public static PrivateKey getPrivateKey(String key) throws Exception {
  96. byte[] bytes = DatatypeConverter.parseHexBinary(key);
  97. PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
  98. KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
  99. return keyFactory.generatePrivate(keySpec);
  100. }
  101. /**
  102. * 从string转publicKey
  103. *
  104. * @param key 公钥的字符串
  105. * @return
  106. * @throws Exception
  107. */
  108. public static PublicKey getPublicKey(String key) throws Exception {
  109. byte[] bytes = DatatypeConverter.parseHexBinary(key);
  110. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
  111. KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
  112. return keyFactory.generatePublic(keySpec);
  113. }
  114. /**
  115. * 生成密钥对
  116. *
  117. * @return
  118. * @throws Exception
  119. */
  120. public static KeyPair getKeyPair() throws Exception {
  121. ECGenParameterSpec ecSpec = new ECGenParameterSpec(SECP256K1);
  122. KeyPairGenerator kf = KeyPairGenerator.getInstance(ALGORITHM);
  123. kf.initialize(ecSpec, new SecureRandom());
  124. KeyPair keyPair = kf.generateKeyPair();
  125. return keyPair;
  126. }
  127. }