SM2.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Org.BouncyCastle.Crypto.Generators;
  2. using Org.BouncyCastle.Crypto.Parameters;
  3. using Org.BouncyCastle.Math;
  4. using Org.BouncyCastle.Math.EC;
  5. using Org.BouncyCastle.Security;
  6. namespace Admin.NET.Core;
  7. public class SM2
  8. {
  9. public static SM2 Instance
  10. {
  11. get
  12. {
  13. return new SM2();
  14. }
  15. }
  16. public static SM2 InstanceTest
  17. {
  18. get
  19. {
  20. return new SM2();
  21. }
  22. }
  23. public static readonly string[] sm2_param = {
  24. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",// p,0
  25. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",// a,1
  26. "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",// b,2
  27. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",// n,3
  28. "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",// gx,4
  29. "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0" // gy,5
  30. };
  31. public string[] ecc_param = sm2_param;
  32. public readonly BigInteger ecc_p;
  33. public readonly BigInteger ecc_a;
  34. public readonly BigInteger ecc_b;
  35. public readonly BigInteger ecc_n;
  36. public readonly BigInteger ecc_gx;
  37. public readonly BigInteger ecc_gy;
  38. public readonly ECCurve ecc_curve;
  39. public readonly ECPoint ecc_point_g;
  40. public readonly ECDomainParameters ecc_bc_spec;
  41. public readonly ECKeyPairGenerator ecc_key_pair_generator;
  42. private SM2()
  43. {
  44. ecc_param = sm2_param;
  45. ecc_p = new BigInteger(ecc_param[0], 16);
  46. ecc_a = new BigInteger(ecc_param[1], 16);
  47. ecc_b = new BigInteger(ecc_param[2], 16);
  48. ecc_n = new BigInteger(ecc_param[3], 16);
  49. ecc_gx = new BigInteger(ecc_param[4], 16);
  50. ecc_gy = new BigInteger(ecc_param[5], 16);
  51. ecc_curve = new FpCurve(ecc_p, ecc_a, ecc_b, null, null);
  52. ecc_point_g = ecc_curve.CreatePoint(ecc_gx, ecc_gy);
  53. ecc_bc_spec = new ECDomainParameters(ecc_curve, ecc_point_g, ecc_n);
  54. ECKeyGenerationParameters ecc_ecgenparam;
  55. ecc_ecgenparam = new ECKeyGenerationParameters(ecc_bc_spec, new SecureRandom());
  56. ecc_key_pair_generator = new ECKeyPairGenerator();
  57. ecc_key_pair_generator.Init(ecc_ecgenparam);
  58. }
  59. public virtual byte[] Sm2GetZ(byte[] userId, ECPoint userKey)
  60. {
  61. var sm3 = new SM3Digest();
  62. byte[] p;
  63. // userId length
  64. int len = userId.Length * 8;
  65. sm3.Update((byte)(len >> 8 & 0x00ff));
  66. sm3.Update((byte)(len & 0x00ff));
  67. // userId
  68. sm3.BlockUpdate(userId, 0, userId.Length);
  69. // a,b
  70. p = ecc_a.ToByteArray();
  71. sm3.BlockUpdate(p, 0, p.Length);
  72. p = ecc_b.ToByteArray();
  73. sm3.BlockUpdate(p, 0, p.Length);
  74. // gx,gy
  75. p = ecc_gx.ToByteArray();
  76. sm3.BlockUpdate(p, 0, p.Length);
  77. p = ecc_gy.ToByteArray();
  78. sm3.BlockUpdate(p, 0, p.Length);
  79. // x,y
  80. p = userKey.AffineXCoord.ToBigInteger().ToByteArray();
  81. sm3.BlockUpdate(p, 0, p.Length);
  82. p = userKey.AffineYCoord.ToBigInteger().ToByteArray();
  83. sm3.BlockUpdate(p, 0, p.Length);
  84. // Z
  85. byte[] md = new byte[sm3.GetDigestSize()];
  86. sm3.DoFinal(md, 0);
  87. return md;
  88. }
  89. }