C# 实现SM2国密加密帮助类

本人在了解对接国家医疗保障信息平台中定点医药机构接口文档中,传输加密方式使用国密算法之SM2.然后本人在隔离期间研究了一下,将内容进行总结,本文主要讲解“国密加密算法”SM系列之SM2的C#实现方法,加密规则请详阅国密局发布的文档 。
首先需第三方Nuget包:Portable.BouncyCastle
1.SM2密码计算
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Core.Common
{
/// <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();
}
}
}
 
2. 加密处理中心
using System;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System.Text;
using Org.BouncyCastle.Crypto.Digests;
namespace Core.Common
{
/// <summary>
/// SM2主类
/// </summary>
/// <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;


推荐阅读