当前位置:   article > 正文

Java 处理Hex字符串,SM2验签_java hex

java hex

基于bc 实现hex格式数据的SM2验签,bc 采用 bcprov-jdk16-1.46.jar ,demo下载链接

点击下载demo

  1. /**
  2. * 模拟服务器SM2 验证签名
  3. * @author ching
  4. *
  5. */
  6. public class Test {
  7. public static void main(String[] args) throws Exception {
  8. byte[] sourceData = "操作类型:二维码限额管理;172308727727".getBytes();
  9. // 国密规范测试公钥
  10. String pubk = "ea09946855fd7a8e444a558dfc9a79efb5a61850265bceb12d736be6758e7898785a67424443ee58aedffab653189c60172fa80da157bb6e201c18f179261570";
  11. String pubkS = new String(Base64.encode(Util.hexToByte(pubk)));
  12. System.out.println("pubkS: " + pubkS);
  13. System.out.println("");
  14. byte[] c = Util.hexToByte(
  15. "3046022100E8E3D56F060C1E29E3A80EBF2687E39B038EBC946B235125C055C66A0D785802022100EE6E92DE1719D35BF9FF68B9022C6F2091347A9F1A987AA85FD53CB07ECCA1C0");
  16. System.out.println("验签: ");
  17. boolean vs = SM2Utils.verifySign(Base64.decode(pubkS.getBytes()), sourceData, c);
  18. System.out.println("验签结果: " + vs);
  19. System.out.println("");
  20. }
  21. }

SM2实现

  1. import java.math.BigInteger;
  2. import java.security.SecureRandom;
  3. import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
  4. import org.bouncycastle.crypto.params.ECDomainParameters;
  5. import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
  6. import org.bouncycastle.math.ec.ECCurve;
  7. import org.bouncycastle.math.ec.ECFieldElement;
  8. import org.bouncycastle.math.ec.ECFieldElement.Fp;
  9. import org.bouncycastle.math.ec.ECPoint;
  10. public class SM2
  11. {
  12. // 测试参数
  13. // public static final String[] ecc_param = {
  14. // "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
  15. // "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
  16. // "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
  17. // "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
  18. // "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
  19. // "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2"
  20. // };
  21. // 正式参数
  22. public static String[] ecc_param = {
  23. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",
  24. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",
  25. "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",
  26. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",
  27. "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",
  28. "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"
  29. };
  30. public static SM2 Instance()
  31. {
  32. return new SM2();
  33. }
  34. public final BigInteger ecc_p;
  35. public final BigInteger ecc_a;
  36. public final BigInteger ecc_b;
  37. public final BigInteger ecc_n;
  38. public final BigInteger ecc_gx;
  39. public final BigInteger ecc_gy;
  40. public final ECCurve ecc_curve;
  41. public final ECPoint ecc_point_g;
  42. public final ECDomainParameters ecc_bc_spec;
  43. public final ECKeyPairGenerator ecc_key_pair_generator;
  44. public final ECFieldElement ecc_gx_fieldelement;
  45. public final ECFieldElement ecc_gy_fieldelement;
  46. public SM2()
  47. {
  48. this.ecc_p = new BigInteger(ecc_param[0], 16);
  49. this.ecc_a = new BigInteger(ecc_param[1], 16);
  50. this.ecc_b = new BigInteger(ecc_param[2], 16);
  51. this.ecc_n = new BigInteger(ecc_param[3], 16);
  52. this.ecc_gx = new BigInteger(ecc_param[4], 16);
  53. this.ecc_gy = new BigInteger(ecc_param[5], 16);
  54. this.ecc_gx_fieldelement = new Fp(this.ecc_p, this.ecc_gx);
  55. this.ecc_gy_fieldelement = new Fp(this.ecc_p, this.ecc_gy);
  56. this.ecc_curve = new ECCurve.Fp(this.ecc_p, this.ecc_a, this.ecc_b);
  57. this.ecc_point_g = new ECPoint.Fp(this.ecc_curve, this.ecc_gx_fieldelement, this.ecc_gy_fieldelement);
  58. this.ecc_bc_spec = new ECDomainParameters(this.ecc_curve, this.ecc_point_g, this.ecc_n);
  59. ECKeyGenerationParameters ecc_ecgenparam;
  60. ecc_ecgenparam = new ECKeyGenerationParameters(this.ecc_bc_spec, new SecureRandom());
  61. this.ecc_key_pair_generator = new ECKeyPairGenerator();
  62. this.ecc_key_pair_generator.init(ecc_ecgenparam);
  63. }
  64. public byte[] sm2GetZ(byte[] userId, ECPoint userKey)
  65. {
  66. SM3Digest sm3 = new SM3Digest();
  67. int len = userId.length * 8;
  68. sm3.update((byte) (len >> 8 & 0xFF));
  69. sm3.update((byte) (len & 0xFF));
  70. sm3.update(userId, 0, userId.length);
  71. byte[] p = Util.byteConvert32Bytes(ecc_a);
  72. sm3.update(p, 0, p.length);
  73. p = Util.byteConvert32Bytes(ecc_b);
  74. sm3.update(p, 0, p.length);
  75. p = Util.byteConvert32Bytes(ecc_gx);
  76. sm3.update(p, 0, p.length);
  77. p = Util.byteConvert32Bytes(ecc_gy);
  78. sm3.update(p, 0, p.length);
  79. p = Util.byteConvert32Bytes(userKey.getX().toBigInteger());
  80. sm3.update(p, 0, p.length);
  81. p = Util.byteConvert32Bytes(userKey.getY().toBigInteger());
  82. sm3.update(p, 0, p.length);
  83. byte[] md = new byte[sm3.getDigestSize()];
  84. sm3.doFinal(md, 0);
  85. return md;
  86. }
  87. public void sm2Sign(byte[] md, BigInteger userD, ECPoint userKey, SM2Result sm2Result)
  88. {
  89. BigInteger e = new BigInteger(1, md);
  90. BigInteger k = null;
  91. ECPoint kp = null;
  92. BigInteger r = null;
  93. BigInteger s = null;
  94. do
  95. {
  96. do
  97. {
  98. // 正式环境
  99. // AsymmetricCipherKeyPair keypair = ecc_key_pair_generator.generateKeyPair();
  100. // ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) keypair.getPrivate();
  101. // ECPublicKeyParameters ecpub = (ECPublicKeyParameters) keypair.getPublic();
  102. // k = ecpriv.getD();
  103. // kp = ecpub.getQ();
  104. k = userD;
  105. kp = userKey;
  106. // 国密规范测试 随机数k
  107. // String kS = "6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F";
  108. // k = new BigInteger(kS, 16);
  109. // kp = this.ecc_point_g.multiply(k);
  110. System.out.println("计算曲线点X1: " + kp.getX().toBigInteger().toString(16));
  111. System.out.println("计算曲线点Y1: " + kp.getY().toBigInteger().toString(16));
  112. System.out.println("");
  113. // r
  114. r = e.add(kp.getX().toBigInteger());
  115. r = r.mod(ecc_n);
  116. } while (r.equals(BigInteger.ZERO) || r.add(k).equals(ecc_n));
  117. // (1 + dA)~-1
  118. BigInteger da_1 = userD.add(BigInteger.ONE);
  119. da_1 = da_1.modInverse(ecc_n);
  120. // s
  121. s = r.multiply(userD);
  122. s = k.subtract(s).mod(ecc_n);
  123. s = da_1.multiply(s).mod(ecc_n);
  124. } while (s.equals(BigInteger.ZERO));
  125. sm2Result.r = r;
  126. sm2Result.s = s;
  127. }
  128. public void sm2Verify(byte md[], ECPoint userKey, BigInteger r, BigInteger s, SM2Result sm2Result)
  129. {
  130. sm2Result.R = null;
  131. BigInteger e = new BigInteger(1, md);
  132. BigInteger t = r.add(s).mod(ecc_n);
  133. if(t.equals(BigInteger.ZERO))
  134. {
  135. return;
  136. }
  137. else
  138. {
  139. ECPoint x1y1 = ecc_point_g.multiply(sm2Result.s);
  140. System.out.println("计算曲线点X0: " + x1y1.getX().toBigInteger().toString(16));
  141. System.out.println("计算曲线点Y0: " + x1y1.getY().toBigInteger().toString(16));
  142. System.out.println("");
  143. x1y1 = x1y1.add(userKey.multiply(t));
  144. System.out.println("计算曲线点X1: " + x1y1.getX().toBigInteger().toString(16));
  145. System.out.println("计算曲线点Y1: " + x1y1.getY().toBigInteger().toString(16));
  146. System.out.println("");
  147. sm2Result.R = e.add(x1y1.getX().toBigInteger()).mod(ecc_n);
  148. System.out.println("R: " + sm2Result.R.toString(16));
  149. return;
  150. }
  151. }
  152. }

Sm2KeyPair 密钥实现

  1. /**
  2. * Created by yuhc on 16-2-21.
  3. */
  4. public class Sm2KeyPair {
  5. private byte[] priKey;
  6. private byte[] pubKey;
  7. public Sm2KeyPair(byte[] priKey, byte[] pubKey){
  8. this.priKey = priKey;
  9. this.pubKey = pubKey;
  10. }
  11. public byte[] getPriKey() {
  12. return priKey;
  13. }
  14. public void setPriKey(byte[] priKey) {
  15. this.priKey = priKey;
  16. }
  17. public byte[] getPubKey() {
  18. return pubKey;
  19. }
  20. public void setPubKey(byte[] pubKey) {
  21. this.pubKey = pubKey;
  22. }
  23. }

SM2Result实现

  1. import java.math.BigInteger;
  2. import org.bouncycastle.math.ec.ECPoint;
  3. public class SM2Result
  4. {
  5. public SM2Result() {
  6. }
  7. // 签名/验签
  8. public BigInteger r;
  9. public BigInteger s;
  10. public BigInteger R;
  11. // 密钥交换
  12. public byte[] sa;
  13. public byte[] sb;
  14. public byte[] s1;
  15. public byte[] s2;
  16. public ECPoint keyra;
  17. public ECPoint keyrb;
  18. }

SM2Utils实现:

  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.math.BigInteger;
  5. import java.util.Enumeration;
  6. import org.bouncycastle.asn1.ASN1EncodableVector;
  7. import org.bouncycastle.asn1.ASN1InputStream;
  8. import org.bouncycastle.asn1.ASN1Sequence;
  9. import org.bouncycastle.asn1.DERInteger;
  10. import org.bouncycastle.asn1.DERObject;
  11. import org.bouncycastle.asn1.DEROctetString;
  12. import org.bouncycastle.asn1.DEROutputStream;
  13. import org.bouncycastle.asn1.DERSequence;
  14. import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
  15. import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
  16. import org.bouncycastle.crypto.params.ECPublicKeyParameters;
  17. import org.bouncycastle.math.ec.ECPoint;
  18. import org.bouncycastle.util.encoders.Base64;
  19. public class SM2Utils
  20. {
  21. public static byte[] encrypt(byte[] publicKey, byte[] data)
  22. {
  23. if (publicKey == null || publicKey.length == 0)
  24. {
  25. return null;
  26. }
  27. if (data == null || data.length == 0)
  28. {
  29. return null;
  30. }
  31. byte[] source = new byte[data.length];
  32. System.arraycopy(data, 0, source, 0, data.length);
  33. byte[] formatedPubKey;
  34. if (publicKey.length == 64){
  35. //添加�?字节标识,用于ECPoint解析
  36. formatedPubKey = new byte[65];
  37. formatedPubKey[0] = 0x04;
  38. System.arraycopy(publicKey,0,formatedPubKey,1,publicKey.length);
  39. }
  40. else
  41. formatedPubKey = publicKey;
  42. Cipher cipher = new Cipher();
  43. SM2 sm2 = SM2.Instance();
  44. ECPoint userKey = sm2.ecc_curve.decodePoint(formatedPubKey);
  45. ECPoint c1 = cipher.Init_enc(sm2, userKey);
  46. cipher.Encrypt(source);
  47. byte[] c3 = new byte[32];
  48. cipher.Dofinal(c3);
  49. DERInteger x = new DERInteger(c1.getX().toBigInteger());
  50. DERInteger y = new DERInteger(c1.getY().toBigInteger());
  51. DEROctetString derDig = new DEROctetString(c3);
  52. DEROctetString derEnc = new DEROctetString(source);
  53. ASN1EncodableVector v = new ASN1EncodableVector();
  54. v.add(x);
  55. v.add(y);
  56. v.add(derDig);
  57. v.add(derEnc);
  58. DERSequence seq = new DERSequence(v);
  59. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  60. DEROutputStream dos = new DEROutputStream(bos);
  61. try {
  62. dos.writeObject(seq);
  63. return bos.toByteArray();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. return null;
  67. }
  68. }
  69. public static byte[] decrypt(byte[] privateKey, byte[] encryptedData)
  70. {
  71. if (privateKey == null || privateKey.length == 0)
  72. {
  73. return null;
  74. }
  75. if (encryptedData == null || encryptedData.length == 0)
  76. {
  77. return null;
  78. }
  79. byte[] enc = new byte[encryptedData.length];
  80. System.arraycopy(encryptedData, 0, enc, 0, encryptedData.length);
  81. SM2 sm2 = SM2.Instance();
  82. BigInteger userD = new BigInteger(1, privateKey);
  83. ByteArrayInputStream bis = new ByteArrayInputStream(enc);
  84. ASN1InputStream dis = new ASN1InputStream(bis);
  85. try {
  86. DERObject derObj = dis.readObject();
  87. ASN1Sequence asn1 = (ASN1Sequence) derObj;
  88. DERInteger x = (DERInteger) asn1.getObjectAt(0);
  89. DERInteger y = (DERInteger) asn1.getObjectAt(1);
  90. ECPoint c1 = sm2.ecc_curve.createPoint(x.getValue(), y.getValue(), true);
  91. Cipher cipher = new Cipher();
  92. cipher.Init_dec(userD, c1);
  93. DEROctetString data = (DEROctetString) asn1.getObjectAt(3);
  94. enc = data.getOctets();
  95. cipher.Decrypt(enc);
  96. byte[] c3 = new byte[32];
  97. cipher.Dofinal(c3);
  98. return enc;
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. return null;
  102. }
  103. }
  104. /**
  105. * 使用默认ID计算
  106. * @param privateKey
  107. * @param sourceData
  108. * @return
  109. */
  110. public static byte[] sign(byte[] privateKey, byte[] sourceData){
  111. String userId = "1234567812345678";
  112. return sign(userId.getBytes(), privateKey, sourceData);
  113. }
  114. public static byte[] sign(byte[] userId, byte[] privateKey, byte[] sourceData)
  115. {
  116. if (privateKey == null || privateKey.length == 0)
  117. {
  118. return null;
  119. }
  120. if (sourceData == null || sourceData.length == 0)
  121. {
  122. return null;
  123. }
  124. SM2 sm2 = SM2.Instance();
  125. BigInteger userD = new BigInteger(privateKey);
  126. System.out.println("userD: " + userD.toString(16));
  127. System.out.println("");
  128. ECPoint userKey = sm2.ecc_point_g.multiply(userD);
  129. System.out.println("椭圆曲线点X: " + userKey.getX().toBigInteger().toString(16));
  130. System.out.println("椭圆曲线点Y: " + userKey.getY().toBigInteger().toString(16));
  131. System.out.println("");
  132. SM3Digest sm3 = new SM3Digest();
  133. byte[] z = sm2.sm2GetZ(userId, userKey);
  134. System.out.println("SM3摘要Z: " + Util.getHexString(z));
  135. System.out.println("");
  136. System.out.println("M: " + Util.getHexString(sourceData));
  137. System.out.println("");
  138. sm3.update(z, 0, z.length);
  139. sm3.update(sourceData, 0, sourceData.length);
  140. byte[] md = new byte[32];
  141. sm3.doFinal(md, 0);
  142. System.out.println("SM3摘要�?: " + Util.getHexString(md));
  143. System.out.println("");
  144. SM2Result sm2Result = new SM2Result();
  145. sm2.sm2Sign(md, userD, userKey, sm2Result);
  146. System.out.println("r: " + sm2Result.r.toString(16));
  147. System.out.println("s: " + sm2Result.s.toString(16));
  148. System.out.println("");
  149. DERInteger d_r = new DERInteger(sm2Result.r);
  150. DERInteger d_s = new DERInteger(sm2Result.s);
  151. ASN1EncodableVector v2 = new ASN1EncodableVector();
  152. v2.add(d_r);
  153. v2.add(d_s);
  154. DERObject sign = new DERSequence(v2);
  155. return sign.getDEREncoded();
  156. }
  157. /**
  158. * 使用默认id计算
  159. * @param publicKey
  160. * @param sourceData
  161. * @param signData
  162. * @return
  163. */
  164. public static boolean verifySign(byte[] publicKey, byte[] sourceData, byte[] signData){
  165. String userId = "1234567812345678";
  166. return verifySign(userId.getBytes(),publicKey,sourceData,signData);
  167. }
  168. @SuppressWarnings("unchecked")
  169. public static boolean verifySign(byte[] userId, byte[] publicKey, byte[] sourceData, byte[] signData)
  170. {
  171. if (publicKey == null || publicKey.length == 0)
  172. {
  173. return false;
  174. }
  175. if (sourceData == null || sourceData.length == 0)
  176. {
  177. return false;
  178. }
  179. byte[] formatedPubKey;
  180. if (publicKey.length == 64){
  181. //添加�?字节标识,用于ECPoint解析
  182. formatedPubKey = new byte[65];
  183. formatedPubKey[0] = 0x04;
  184. System.arraycopy(publicKey,0,formatedPubKey,1,publicKey.length);
  185. }
  186. else
  187. formatedPubKey = publicKey;
  188. SM2 sm2 = SM2.Instance();
  189. ECPoint userKey = sm2.ecc_curve.decodePoint(formatedPubKey);
  190. SM3Digest sm3 = new SM3Digest();
  191. byte[] z = sm2.sm2GetZ(userId, userKey);
  192. sm3.update(z, 0, z.length);
  193. sm3.update(sourceData, 0, sourceData.length);
  194. byte[] md = new byte[32];
  195. sm3.doFinal(md, 0);
  196. System.out.println("SM3摘要�?: " + Util.getHexString(md));
  197. System.out.println("");
  198. ByteArrayInputStream bis = new ByteArrayInputStream(signData);
  199. ASN1InputStream dis = new ASN1InputStream(bis);
  200. SM2Result sm2Result = null;
  201. try {
  202. DERObject derObj = dis.readObject();
  203. Enumeration<DERInteger> e = ((ASN1Sequence) derObj).getObjects();
  204. BigInteger r = ((DERInteger)e.nextElement()).getValue();
  205. BigInteger s = ((DERInteger)e.nextElement()).getValue();
  206. sm2Result = new SM2Result();
  207. sm2Result.r = r;
  208. sm2Result.s = s;
  209. System.out.println("r: " + sm2Result.r.toString(16));
  210. System.out.println("s: " + sm2Result.s.toString(16));
  211. System.out.println("");
  212. sm2.sm2Verify(md, userKey, sm2Result.r, sm2Result.s, sm2Result);
  213. return sm2Result.r.equals(sm2Result.R);
  214. } catch (IOException e1) {
  215. e1.printStackTrace();
  216. return false;
  217. }
  218. }
  219. public static void main(String[] args) throws Exception
  220. {
  221. String plainText = "message digest";
  222. byte[] sourceData = plainText.getBytes();
  223. // 国密规范测试私钥
  224. String prik = "444E6EA3EE0C7E0AAA5EE5C6BBC7A2D8DE3FB3FA990AD470232D07FB445F92D7";
  225. String prikS = new String(Base64.encode(Util.hexToByte(prik)));
  226. System.out.println("prikS: " + prikS);
  227. System.out.println("");
  228. // 国密规范测试用户ID
  229. String userId = "1234567812345678";
  230. System.out.println("ID: " + Util.getHexString(userId.getBytes()));
  231. System.out.println("");
  232. System.out.println("签名: ");
  233. byte[] c = SM2Utils.sign(userId.getBytes(), Base64.decode(prikS.getBytes()), sourceData);
  234. System.out.println("sign: " + Util.getHexString(c));
  235. System.out.println("");
  236. sourceData = "<?xml version='1.0' encoding='utf-8'?><t><d><M><k>转出帐号:</k><v>1014 5101 0100 0005 1321 5</v></M><M><k>转入帐号:</k><v>6212 8060 1000 4100 061</v></M><M><k>转入户名:</k><v>白素�?</v></M><M><k>金额:</k><v>1.00</v></M></d></t>".getBytes();
  237. // 国密规范测试公钥
  238. // String pubk = "2E9173C4DB1DB0B22980DD3235ABF99B787DE8E5C6D08BDBA4503D61EE2B32F0F7083CC46D92DAE72FD0223305D0B44A95D438142C45382B23B2A58122E1F3DF";
  239. String pubk = "b96fa0249b43ca3cea944d92ed97d6688107c84525b271704f604133a0fc05ef0b59850e9920a7a4f2a1170aeb44a3aa18bff223125754d218a930f7df5f6f33";
  240. String pubkS = new String(Base64.encode(Util.hexToByte(pubk)));
  241. System.out.println("pubkS: " + pubkS);
  242. System.out.println("");
  243. c= Util.hexToByte("3045022100cbbe02d89cd21c74f5b16355752e11777c64a18f44363746013ab1cdc46a05540220563730d0faebfb29a752352662d5aeb0de4c1b9f40f73c2808bbfd2c7dfabde7");
  244. System.out.println("验签: ");
  245. boolean vs = SM2Utils.verifySign(Base64.decode(pubkS.getBytes()), sourceData, c);
  246. System.out.println("验签结果: " + vs);
  247. System.out.println("");
  248. }
  249. public static Sm2KeyPair generateKeyPair(){
  250. SM2 sm2 = SM2.Instance();
  251. AsymmetricCipherKeyPair keypair = sm2.ecc_key_pair_generator.generateKeyPair();
  252. ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) keypair.getPrivate();
  253. ECPublicKeyParameters ecpub = (ECPublicKeyParameters) keypair.getPublic();
  254. // System.out.println("私钥: " + ecpriv.getD().toString(16).toUpperCase());
  255. // System.out.println("公钥: " + ecpub.getQ().getX().toBigInteger().toString(16).toUpperCase() +
  256. // ecpub.getQ().getY().toBigInteger().toString(16).toUpperCase());
  257. byte[] priKey = new byte[32];
  258. byte[] pubKey = new byte[64];
  259. byte[] bigNumArray = ecpriv.getD().toByteArray();
  260. System.arraycopy(bigNumArray, bigNumArray[0]==0?1:0, priKey, 0, 32);
  261. System.arraycopy(ecpub.getQ().getEncoded(), 1, pubKey, 0, 64);
  262. // System.out.println("私钥bigNumArray: " + Util.getHexString(bigNumArray));
  263. // System.out.println("私钥: " + Util.getHexString(priKey));
  264. // System.out.println("公钥: " + Util.getHexString(pubKey));
  265. return new Sm2KeyPair(priKey, pubKey);
  266. }
  267. public static void main(){
  268. String plainText = "Hello SM !";
  269. byte[] sourceData = plainText.getBytes();
  270. // 国密规范测试私钥
  271. String prik = "444E6EA3EE0C7E0AAA5EE5C6BBC7A2D8DE3FB3FA990AD470232D07FB445F92D7";
  272. byte[] c = SM2Utils.sign(Util.hexToByte(prik), sourceData);
  273. System.out.println("sign: " + Util.getHexString(c));
  274. // 国密规范测试公钥
  275. String pubk = "2E9173C4DB1DB0B22980DD3235ABF99B787DE8E5C6D08BDBA4503D61EE2B32F0F7083CC46D92DAE72FD0223305D0B44A95D438142C45382B23B2A58122E1F3DF";
  276. boolean vs = SM2Utils.verifySign(Util.hexToByte(pubk), sourceData, c);
  277. System.out.println("验签结果: " + vs);
  278. System.out.println("加密: ");
  279. byte[] cipherText = SM2Utils.encrypt(Util.hexToByte(pubk), sourceData);
  280. System.out.println(Util.getHexString(cipherText));
  281. System.out.println("解密: ");
  282. plainText = new String(SM2Utils.decrypt(Util.hexToByte(prik), cipherText));
  283. System.out.println(plainText);
  284. }
  285. public static void Sm2Test(){
  286. String plainText = "Hello SM !";
  287. byte[] sourceData = plainText.getBytes();
  288. Sm2KeyPair keyPair = generateKeyPair();
  289. System.out.println("私钥: " + Util.getHexString(keyPair.getPriKey()));
  290. System.out.println("公钥: " + Util.getHexString(keyPair.getPubKey()));
  291. byte[] c = SM2Utils.sign(keyPair.getPriKey(), sourceData);
  292. System.out.println("sign: " + Util.getHexString(c));
  293. boolean vs = SM2Utils.verifySign(keyPair.getPubKey(), sourceData, c);
  294. System.out.println("验签结果: " + vs);
  295. System.out.println("加密: ");
  296. byte[] cipherText = SM2Utils.encrypt(keyPair.getPubKey(), sourceData);
  297. System.out.println(Util.getHexString(cipherText));
  298. System.out.println("解密: ");
  299. plainText = new String(SM2Utils.decrypt(keyPair.getPriKey(), cipherText));
  300. System.out.println(plainText);
  301. }
  302. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/449995
推荐阅读
相关标签
  

闽ICP备14008679号