C#国密SM2加密算法实现原创

原创
小哥 3年前 (2022-11-08) 阅读数 18 #C#
文章标签 C#

最近我一直在做数据上报,数据上报需要用到国家机密。SM2加密算法加密和上传,以前从未接触过这个东西,所以做一个简单的记录。平台提供加密的公钥,这样我们就可以根据公钥对数据进行加密,并提交给接口,确保数据的安全传输。

实现代码

  • 需要引入加密算法。 【BouncyCastle 】https://www.bouncycastle.org/ 文件。

    using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities.Encoders; using System; using System.Collections.Generic; using System.Linq; using System.Text;

    namespace HSSB { /****

    • 项目名称: Program.cs
    • 命名空间 : HSSB
    • 类 名 称 : Program
    • 作 者 : RoyeeCai
    • 邮 箱 : caimh0223@163.com
    • 概 述 :
    • 创建时间 : 2022-04-20 12:23:40
    • 更新时间 : 2022-04-20 12:23:40
    • CLR版本 : 4.0.30319.42000

    • Copyright@RoyeeCai 2022 .All rights reserved.

    • */ class Program { static void Main(string[] args) { // 公钥 平台提供 String publicKey = "3059301306072a8648ce3d020106082a811ccf5501822d03420004aaa1dae5c13eacf0bf98a3570070101f1e0ce7be2bc000a702595a136ac18e0c109119314897b51d81e29a746d0913a7f364b5853dfcb901eff6826401981791"; //要加密的字符串 String val = "{"userName":"王小宝","age":"10"}";

          //String s1 = Util4Hex.bytesToHexString(EncryptPublic(publicKey, Encoding.Default.GetBytes(val)));
          String s = Encoding.UTF8.GetString(Hex.Encode(EncryptPublic(publicKey, Encoding.UTF8.GetBytes(val))));
          //Console.WriteLine(s1);
          Console.WriteLine("=========以下是加密的字符串=========");
          Console.WriteLine(s);  
          Console.ReadKey();
      }
      
      /// 
      /// 生成SM2加密串
      /// 
      /// 
      /// 
      /// 
      public static byte[] EncryptPublic(String pubKey, byte[] srcData)
      {
          //byte[] publicKeyByte = Util4Hex.hexStringToBytes(pubKey);
          byte[] publicKeyByte = hexStringToBytes(pubKey);
          ECPublicKeyParameters ECPPublicKey = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(publicKeyByte);
          SM2Engine engine = new SM2Engine();
          ParametersWithRandom pwr = new ParametersWithRandom(ECPPublicKey, new SecureRandom());
          engine.Init(true, pwr);
          return engine.ProcessBlock(srcData, 0, srcData.Length);
      }
      
      /**
       * Convert hex string to byte[]
       * 
       * @param hexString the hex string
       * @return byte[]
       */
      public static byte[] hexStringToBytes(String hexString)
      {
          if (hexString == null || hexString.Equals(""))
          {
              return null;
          }
          hexString = hexString.ToUpper();
          int length = hexString.Length / 2;
          char[] hexChars = hexString.ToCharArray();
          byte[] d = new byte[length];
          for (int i = 0; i < length; i++)
          {
              int pos = i * 2;
              d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
          }
          return d;
      }
      
      /**
       * Convert char to byte
       * 
       * @param c char
       * @return byte
       */
      private static byte charToByte(char c)
      {
          return (byte)"0123456789ABCDEF".IndexOf(c);
      }

      } }

运行效果

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除