本文主要讲解“国密加密算法”SM系列之SM2的C#实现方法,加密规则请详阅国密局发布的文档。

首先需第三方Nuget包:Portable.BouncyCastle (源码来自http://www.bouncycastle.org/csharp/)

SM2的加密需使用到SM3加密处理

1.1 SM2密码计算

    /// <summary>/// 密码计算/// </summary>public class Cipher{private int ct = 1;/// <summary>/// 椭圆曲线E上点P2/// </summary>private ECPoint p2;private SM3Digest sm3keybase;private SM3Digest sm3c3;private readonly byte[] key = new byte[32];private byte keyOff = 0;public Cipher(){}private void Reset(){sm3keybase = new SM3Digest();sm3c3 = new SM3Digest();byte[] p;p = p2.Normalize().XCoord.ToBigInteger().ToByteArray(); sm3keybase.BlockUpdate(p, 0, p.Length);sm3c3.BlockUpdate(p, 0, p.Length);p = p2.Normalize().YCoord.ToBigInteger().ToByteArray();sm3keybase.BlockUpdate(p, 0, p.Length);ct = 1;NextKey();}private void NextKey(){SM3Digest sm3keycur = new SM3Digest(sm3keybase);sm3keycur.Update((byte)(ct >> 24 & 0x00ff));sm3keycur.Update((byte)(ct >> 16 & 0x00ff));sm3keycur.Update((byte)(ct >> 8 & 0x00ff));sm3keycur.Update((byte)(ct & 0x00ff));sm3keycur.DoFinal(key, 0);keyOff = 0;ct++;}public virtual ECPoint InitEnc(SM2 sm2, ECPoint userKey){AsymmetricCipherKeyPair key = sm2.EccKeyPairGenerator.GenerateKeyPair();ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)key.Private;ECPublicKeyParameters ecpub = (ECPublicKeyParameters)key.Public;BigInteger k = ecpriv.D;ECPoint c1 = ecpub.Q;p2 = userKey.Multiply(k);Reset();return c1;}public virtual void Encrypt(byte[] data){//p2.Normalize();sm3c3.BlockUpdate(data, 0, data.Length);for (int i = 0; i < data.Length; i++){if (keyOff == key.Length)NextKey();data[i] ^= key[keyOff++];}}public virtual void InitDec(BigInteger userD, ECPoint c1){p2 = c1.Multiply(userD);Reset();}public virtual void Decrypt(byte[] data){for (int i = 0; i < data.Length; i++){if (keyOff == key.Length)NextKey();data[i] ^= key[keyOff++];}sm3c3.BlockUpdate(data, 0, data.Length);}public virtual void Dofinal(byte[] c3){byte[] p = p2.Normalize().YCoord.ToBigInteger().ToByteArray(); sm3c3.BlockUpdate(p, 0, p.Length);sm3c3.DoFinal(c3, 0);Reset();}}

1.2 加密处理中心

    /// <summary>/// 加密处理中心/// </summary>public class SM2{public static SM2 Instance{get{return new SM2();}}public static SM2 InstanceTest{get{return new SM2();}}#region 曲线参数/// <summary>/// 曲线参数/// </summary>public static readonly string[] CurveParameter = {"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",// p,0"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",// a,1"28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",// b,2"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",// n,3"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",// gx,4"BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0" // gy,5};/// <summary>/// 椭圆曲线参数/// </summary>public string[] EccParam = CurveParameter;/// <summary>/// 椭圆曲线参数P/// </summary>public readonly BigInteger EccP;/// <summary>/// 椭圆曲线参数A/// </summary>public readonly BigInteger EccA;/// <summary>/// 椭圆曲线参数B/// </summary>public readonly BigInteger EccB;/// <summary>/// 椭圆曲线参数N/// </summary>public readonly BigInteger EccN;/// <summary>/// 椭圆曲线参数Gx/// </summary>public readonly BigInteger EccGx;/// <summary>/// 椭圆曲线参数Gy/// </summary>public readonly BigInteger EccGy;#endregion/// <summary>/// 椭圆曲线/// </summary>public readonly ECCurve EccCurve;/// <summary>/// 椭圆曲线的点G/// </summary>public readonly ECPoint EccPointG;/// <summary>/// 椭圆曲线 bc规范/// </summary>public readonly ECDomainParameters EccBcSpec;/// <summary>/// 椭圆曲线密钥对生成器/// </summary>public readonly ECKeyPairGenerator EccKeyPairGenerator;private SM2(){EccParam = CurveParameter;EccP = new BigInteger(EccParam[0], 16);EccA = new BigInteger(EccParam[1], 16);EccB = new BigInteger(EccParam[2], 16);EccN = new BigInteger(EccParam[3], 16);EccGx = new BigInteger(EccParam[4], 16);EccGy = new BigInteger(EccParam[5], 16);ECFieldElement ecc_gx_fieldelement = new FpFieldElement(EccP, EccGx);ECFieldElement ecc_gy_fieldelement = new FpFieldElement(EccP, EccGy);EccCurve = new FpCurve(EccP, EccA, EccB);EccPointG = new FpPoint(EccCurve, ecc_gx_fieldelement, ecc_gy_fieldelement);EccBcSpec = new ECDomainParameters(EccCurve, EccPointG, EccN);ECKeyGenerationParameters ecc_ecgenparam;ecc_ecgenparam = new ECKeyGenerationParameters(EccBcSpec, new SecureRandom());EccKeyPairGenerator = new ECKeyPairGenerator();EccKeyPairGenerator.Init(ecc_ecgenparam);}/// <summary>/// 获取杂凑值H/// </summary>/// <param name="z">Z值</param>/// <param name="data">待签名消息</param>/// <returns></returns>public virtual byte[] Sm2GetH(byte[] z, byte[] data){SM3Digest sm3 = new SM3Digest();//Zsm3.BlockUpdate(z, 0, z.Length);//待签名消息sm3.BlockUpdate(data, 0, data.Length);// Hbyte[] md = new byte[sm3.GetDigestSize()];sm3.DoFinal(md, 0);return md;}/// <summary>/// 获取Z值/// Z=SM3(ENTL∣∣userId∣∣a∣∣b∣∣gx∣∣gy ∣∣x∣∣y) /// </summary>/// <param name="userId">签名方的用户身份标识</param>/// <param name="userKey">签名方公钥</param>/// <returns></returns>public virtual byte[] Sm2GetZ(byte[] userId, ECPoint userKey){SM3Digest sm3 = new SM3Digest();byte[] p;// ENTL由2个字节标识的ID的比特长度 int len = userId.Length * 8;sm3.Update((byte)(len >> 8 & 0x00ff));sm3.Update((byte)(len & 0x00ff));// userId用户身份标识IDsm3.BlockUpdate(userId, 0, userId.Length);// a,b为系统曲线参数;p = EccA.ToByteArray();sm3.BlockUpdate(p, 0, p.Length);p = EccB.ToByteArray();sm3.BlockUpdate(p, 0, p.Length);//  gx、gy为基点p = EccGx.ToByteArray();sm3.BlockUpdate(p, 0, p.Length);p = EccGy.ToByteArray();sm3.BlockUpdate(p, 0, p.Length);// x,y用户的公钥的X和Yp = userKey.Normalize().XCoord.ToBigInteger().ToByteArray();sm3.BlockUpdate(p, 0, p.Length);p = userKey.Normalize().YCoord.ToBigInteger().ToByteArray();sm3.BlockUpdate(p, 0, p.Length);// Zbyte[] md = new byte[sm3.GetDigestSize()];sm3.DoFinal(md, 0);return md;}}

1.3 加密调用

    /// <summary>/// Sm2算法   /// 对标国际RSA算法/// </summary>public class Sm2Crypto {/// <summary>/// 数据/// </summary>public string Str { get; set; }/// <summary>/// 数据/// </summary>public byte[] Data { get; set; }/// <summary>/// 公钥/// </summary>public string PublicKey { get; set; }/// <summary>/// 私钥/// </summary>public string PrivateKey { get; set; }/// <summary>/// 获取密钥/// </summary>/// <param name="privateKey">私钥</param>/// <param name="publicKey">公钥</param>public static void GetKey(out string privateKey, out string publicKey){SM2 sm2 = SM2.Instance;AsymmetricCipherKeyPair key = sm2.EccKeyPairGenerator.GenerateKeyPair();ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)key.Private;ECPublicKeyParameters ecpub = (ECPublicKeyParameters)key.Public;publicKey = Encoding.Default.GetString(Hex.Encode(ecpub.Q.GetEncoded())).ToUpper();privateKey = Encoding.Default.GetString(Hex.Encode(ecpriv.D.ToByteArray())).ToUpper();}#region 解密public object Decrypt(Sm2Crypto entity){var data = !string.IsNullOrEmpty(entity.Str) ?Hex.Decode(entity.Str) :entity.Data;return Encoding.Default.GetString(Decrypt(Hex.Decode(entity.PrivateKey), data));}/// <summary>/// 解密/// </summary>/// <param name="privateKey"></param>/// <param name="encryptedData"></param>/// <returns></returns>private static byte[] Decrypt(byte[] privateKey, byte[] encryptedData){if (null == privateKey || privateKey.Length == 0){return null;}if (encryptedData == null || encryptedData.Length == 0){return null;}String data = Encoding.Default.GetString(Hex.Encode(encryptedData));byte[] c1Bytes = Hex.Decode(Encoding.Default.GetBytes(data.Substring(0, 130)));int c2Len = encryptedData.Length - 97;byte[] c2 = Hex.Decode(Encoding.Default.GetBytes(data.Substring(130, 2 * c2Len)));byte[] c3 = Hex.Decode(Encoding.Default.GetBytes(data.Substring(130 + 2 * c2Len, 64)));SM2 sm2 = SM2.Instance;BigInteger userD = new BigInteger(1, privateKey);ECPoint c1 = sm2.EccCurve.DecodePoint(c1Bytes);//c1.Normalize();Cipher cipher = new Cipher();cipher.InitDec(userD, c1);cipher.Decrypt(c2);cipher.Dofinal(c3);return c2;}#endregion#region 加密public string Encrypt(Sm2Crypto entity){var data = !string.IsNullOrEmpty(entity.Str) ?Encoding.Default.GetBytes(entity.Str) :entity.Data;return Encrypt(Hex.Decode(entity.PublicKey), data);}/// <summary>/// 加密/// </summary>/// <param name="publicKey"></param>/// <param name="data"></param>/// <returns></returns>private static string Encrypt(byte[] publicKey, byte[] data){if (null == publicKey || publicKey.Length == 0){return null;}if (data == null || data.Length == 0){return null;}byte[] source = new byte[data.Length];Array.Copy(data, 0, source, 0, data.Length);Cipher cipher = new Cipher();SM2 sm2 = SM2.Instance;ECPoint userKey = sm2.EccCurve.DecodePoint(publicKey);//userKey.Normalize();ECPoint c1 = cipher.InitEnc(sm2, userKey);cipher.Encrypt(source);byte[] c3 = new byte[32];cipher.Dofinal(c3);String sc1 = Encoding.Default.GetString(Hex.Encode(c1.GetEncoded()));String sc2 = Encoding.Default.GetString(Hex.Encode(source));String sc3 = Encoding.Default.GetString(Hex.Encode(c3));return (sc1 + sc2 + sc3).ToUpper();}#endregion}

【转】C#实现SM2国密加密相关推荐

  1. js 使用sm2 国密加密

    js 使用sm2 国密加密 由于项目中要对数据进行国密加密 注意: 同一个明文,加密后的密文都不同,解密的话要用私钥解密 js用sm2加密,首先要从后端获取公钥,用公钥加密 参考资料 crypto-j ...

  2. 技术分享 | 使用 mPaaS 配置 SM2 国密加密指南

    简介:随着移动智能终端的广泛应用,敏感信息极易被监控或盗取,给国家.企事业及个人带来极大政治.经济损失.金融和重要领域的各个企业正在逐步落实并完成国产密码改造工作.为解决客户侧因更换加密算法造成的种种 ...

  3. SM2 国密加密加签操作工具

    pom引入依bouncycastle赖 注意:bouncycastle版本过低会出现报错(我之前报错的的版本号1.60,修改后使用的1.68) <dependency><groupI ...

  4. Java国密加密SM2代码

    Java国密加密SM2代码 文章目录 Java国密加密SM2代码 前言 一.SM2是什么? 二.使用步骤 1.引入Maven库 2.密码工具类 3.安全工具类 4.SM2工具类 5.SM2工具实用类 ...

  5. SM2国密算法加解密

    接口安全设计原则的一个点就是数据不能明文传输,除了https这个必须的请求外,接口数据加密也是一个重要的方式,下面介绍一下SM2国密算法加解密的使用方式. 这里我就针对目前前后端分离架构的方式来简单介 ...

  6. SM2 国密算法被 Linux 内核社区接受

    喜欢就关注我们吧! 10 月 25 日,有开发者发文称,SM2 国密算法终于被 Linux 内核社区接受了.该作者表示,SM2 的补丁已经更新到了 v7 版本,这个版本的补丁最终被社区接受,目前已经合 ...

  7. 【转】C#实现SM3国密加密

    C#实现SM3国密加密 本文主要讲解"国密加密算法"SM系列之SM3的C#实现方法,加密规则请详阅国密局发布的文档. 首先需第三方Nuget包:Portable.BouncyCas ...

  8. 国密浏览器如何完成SM2国密SSL协议协商

    国密浏览器需要遵循国密SSL协议规范GM/T 0024-2014.GM/T 0024-2014没有单独规范 SSL协议的文件,而是在SSL VPN技术规范中定义了国密SSL协议.国密SSL协议(SSL ...

  9. 记自己发现的—SM2国密算法应用的高危漏洞—CVE-2021-3711

    openssl在8月24日发布了openssl 1.1.1l的稳定版,其中修复了一个高危漏洞:CVE-2021-3711.该漏洞会影响openssl 1.1.1l 之前的所有包含SM2商密算法版本,其 ...

最新文章

  1. 借书证信息管理系统,C语言实现
  2. Python删除list里面的重复元素的俩种方法
  3. python 创建目录_Python虚拟环境的搭建与使用
  4. python输出不重复的单词_python-如何在文本文件中创建所有单词的列表,但字母重复的单词除外...
  5. C#-数组定义及使用数组的好处 046
  6. 剑指offer の 1-10 之javascript实现
  7. 和为S(51Nod-2518)
  8. C++中menset用法
  9. C++常识“屯”和“烫”
  10. 冒泡排序及优化(Java实现)
  11. View的事件处理流程
  12. [Swift通天遁地]二、表格表单-(11)创建星期选项表单和拥有浮动标签的文本框
  13. 爬虫小练(刷访问量)(python+requests(headers+proxy)+Queue+threading)
  14. 淘宝客户端安全生产体系建设
  15. C++之个人银行账户管理程序(二)
  16. 跑深度模型的显卡_不止显卡!这些硬件因素也影响着你的深度学习模型性能
  17. 【BZOJ30472125】Freda的传呼机
  18. 转载 锁机制与原子操作 第四篇
  19. 支付宝授权登录免费源码奉献
  20. win7 打开防火墙端口

热门文章

  1. 如何提高英文的科研写作能力
  2. 北京集训:20180310
  3. 高可用-软件heartbeat的入门介绍
  4. dropify,不错的图片上传预览插件
  5. 移动端弹出层加遮罩后禁止滑动
  6. 引路蜂地图API:Gis.Navigation包定义
  7. 67、ulimit的使用
  8. GridView应用整理
  9. numpy数组按某一维度相加_Python数据分析之NumPy(高级篇)
  10. html中设置负边距的意义,css负边距之详解