• Java通用C# SM2加密解密


            起因是对接一个接口 他们只能提供java deom c#的没有,  百度了一天不能适配, 只能看java 还原的C#。

     代码

    1. public class SM2CryptoUtil
    2. {
    3. public SM2CryptoUtil(string pubkey, string privkey, Mode mode = Mode.C1C2C3, bool isPkcs8 = false)
    4. {
    5. if (pubkey != null)
    6. this.pubkey = pubkey;
    7. if (privkey != null)
    8. this.privkey = privkey;
    9. this.mode = mode;
    10. }
    11. string pubkey;
    12. string privkey;
    13. Mode mode;
    14. ICipherParameters _privateKeyParameters;
    15. ICipherParameters PrivateKeyParameters
    16. {
    17. get
    18. {
    19. try
    20. {
    21. var r = _privateKeyParameters;
    22. if (r == null) r = _privateKeyParameters =
    23. new ECPrivateKeyParameters(new BigInteger(privkey, 16),
    24. new ECDomainParameters(GMNamedCurves.GetByName("SM2P256V1")));
    25. return r;
    26. }
    27. catch (Exception ex)
    28. {
    29. return null;
    30. }
    31. }
    32. }
    33. ICipherParameters _publicKeyParameters;
    34. ICipherParameters PublicKeyParameters
    35. {
    36. get
    37. {
    38. try
    39. {
    40. var r = _publicKeyParameters;
    41. if (r == null)
    42. {
    43. //截取64字节有效的SM2公钥(如果公钥首个字节为0x04)
    44. if (pubkey.Length > 128)
    45. {
    46. pubkey = pubkey.Substring(pubkey.Length - 128);
    47. }
    48. //将公钥拆分为x,y分量(各32字节)
    49. String stringX = pubkey.Substring(0, 64);
    50. String stringY = pubkey.Substring(stringX.Length);
    51. //将公钥x、y分量转换为BigInteger类型
    52. BigInteger x = new BigInteger(stringX, 16);
    53. BigInteger y = new BigInteger(stringY, 16);
    54. //通过公钥x、y分量创建椭圆曲线公钥规范
    55. var x9ec = GMNamedCurves.GetByName("SM2P256V1");
    56. r = _publicKeyParameters = new ECPublicKeyParameters(x9ec.Curve.CreatePoint(x,y),
    57. new ECDomainParameters(x9ec));
    58. }
    59. return r;
    60. }
    61. catch (Exception ex)
    62. {
    63. return null;
    64. ;
    65. }
    66. }
    67. }
    68. public byte[] Decrypt(byte[] data)
    69. {
    70. try
    71. {
    72. if (mode == Mode.C1C3C2)
    73. data = C132ToC123(data);
    74. var sm2 = new SM2Engine();
    75. sm2.Init(false, this.PrivateKeyParameters);
    76. return sm2.ProcessBlock(data, 0, data.Length);
    77. }
    78. catch (Exception ex)
    79. {
    80. return null;
    81. }
    82. }
    83. public byte[] Encrypt(byte[] data)
    84. {
    85. try
    86. {
    87. // var sm2 = new SM2Engine(new SM3Digest());
    88. var sm2 = new SM2Engine();
    89. sm2.Init(true, new ParametersWithRandom(this.PublicKeyParameters));
    90. data = sm2.ProcessBlock(data, 0, data.Length);
    91. if (mode == Mode.C1C3C2)
    92. data = C123ToC132(data);
    93. return data;
    94. }
    95. catch (Exception ex)
    96. {
    97. return null;
    98. }
    99. }
    100. static byte[] C123ToC132(byte[] c1c2c3)
    101. {
    102. var gn = GMNamedCurves.GetByName("SM2P256V1");
    103. int c1Len = (gn.Curve.FieldSize + 7) / 8 * 2 + 1;
    104. int c3Len = 32;
    105. byte[] result = new byte[c1c2c3.Length];
    106. Array.Copy(c1c2c3, 0, result, 0, c1Len); //c1
    107. Array.Copy(c1c2c3, c1c2c3.Length - c3Len, result, c1Len, c3Len); //c3
    108. Array.Copy(c1c2c3, c1Len, result, c1Len + c3Len, c1c2c3.Length - c1Len - c3Len); //c2
    109. return result;
    110. }
    111. static byte[] C132ToC123(byte[] c1c3c2)
    112. {
    113. var gn = GMNamedCurves.GetByName("SM2P256V1");
    114. int c1Len = (gn.Curve.FieldSize + 7) / 8 * 2 + 1;
    115. int c3Len = 32;
    116. byte[] result = new byte[c1c3c2.Length];
    117. Array.Copy(c1c3c2, 0, result, 0, c1Len); //c1: 0->65
    118. Array.Copy(c1c3c2, c1Len + c3Len, result, c1Len, c1c3c2.Length - c1Len - c3Len); //c2
    119. Array.Copy(c1c3c2, c1Len, result, c1c3c2.Length - c3Len, c3Len); //c3
    120. return result;
    121. }
    122. public enum Mode
    123. {
    124. C1C2C3, C1C3C2
    125. }
    126. }

    调用方式

    1. //实例化
    2. string pukey="";
    3. string PRKEY="";
    4. var handle = new SM2CryptoUtil(pukey, PRKEY, SM2CryptoUtil.Mode.C1C3C2);
    5. //加密
    6. string str="";
    7. var Encryptdata = handle.Encrypt(System.Text.Encoding.UTF8.GetBytes(str));
    8. string datac = Hex.ToHexString(Encryptdata);
    9. //解密
    10. string str="";
    11. var original = handle.Decrypt(Hex.Decode(str));
    12. string data = System.Text.Encoding.Default.GetString(original);

     如果不能用 请留言
    苦逼呀!!!

  • 相关阅读:
    springboot中的任务处理
    如何在linux系统上部署nginx
    js中如何判断浏览器是否被缩放
    面试如何准备Redis的问题(含缓存穿透,持久化和数据一致性等知识点)
    CyclicBarrier和CountDownLatch的⽤法及区别?
    SpringBoot - @InitBinder注解详解
    17.重定向(redirect)和请求转发(forward)
    找不到d3dx9_37.dll,无法继续执行代码
    Android11.0 修改设备名、型号、厂商
    Redis分布式锁Redisson
  • 原文地址:https://blog.csdn.net/qq_36664772/article/details/126562392