Browse Source

sm2 sm3Demo

fuchaoyang 1 year ago
commit
55e668d14f

+ 111 - 0
.gitignore

@@ -0,0 +1,111 @@
+# ---> C Sharp
+# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
+[Bb]in/
+[Oo]bj/
+
+# mstest test results
+TestResults
+
+## Ignore Visual Studio temporary files, build results, and
+## files generated by popular Visual Studio add-ons.
+
+# User-specific files
+*.suo
+*.user
+*.sln.docstates
+*.db
+
+# Build results
+[Dd]ebug/
+[Rr]elease/
+x64/
+*_i.c
+*_p.c
+*.ilk
+*.meta
+*.obj
+*.pch
+*.pdb
+*.pgc
+*.pgd
+*.rsp
+*.sbr
+*.tlb
+*.tli
+*.tlh
+*.tmp
+*.log
+*.vspscc
+*.vssscc
+.builds
+.vs/
+# Visual C++ cache files
+ipch/
+*.aps
+*.ncb
+*.opensdf
+*.sdf
+
+# Visual Studio profiler
+*.psess
+*.vsp
+*.vspx
+
+# Guidance Automation Toolkit
+*.gpState
+
+# ReSharper is a .NET coding add-in
+_ReSharper*
+
+# NCrunch
+*.ncrunch*
+.*crunch*.local.xml
+
+# Installshield output folder
+[Ee]xpress
+
+# DocProject is a documentation generator add-in
+DocProject/buildhelp/
+DocProject/Help/*.HxT
+DocProject/Help/*.HxC
+DocProject/Help/*.hhc
+DocProject/Help/*.hhk
+DocProject/Help/*.hhp
+DocProject/Help/Html2
+DocProject/Help/html
+
+# Click-Once directory
+publish
+
+# Publish Web Output
+*.Publish.xml
+
+# NuGet Packages Directory
+packages
+
+# Windows Azure Build Output
+csx
+*.build.csdef
+
+# Windows Store app package directory
+AppPackages/
+
+# Others
+[Bb]in
+[Oo]bj
+sql
+TestResults
+[Tt]est[Rr]esult*
+*.Cache
+ClientBin
+[Ss]tyle[Cc]op.*
+~$*
+*.dbmdl
+Generated_Code #added for RIA/Silverlight projects
+
+# Backup & report files from converting an old project file to a newer
+# Visual Studio version. Backup files are not needed, because we have git ;-)
+_UpgradeReport_Files/
+Backup*/
+UpgradeLog*.XML
+

+ 23 - 0
SMTest/Program.cs

@@ -0,0 +1,23 @@
+// See https://aka.ms/new-console-template for more information
+using Org.BouncyCastle.Utilities.Encoders;
+using System.Text;
+using YS.SM2;
+using YS.SM3;
+
+Console.WriteLine("Hello, World!");
+#region SM2加解密
+var (publicKey, privateKey) = SM2Utils.GenerateKeyPair();
+var sm2Data = "123456789";
+var encryptSM2 = sm2Data.EncryptSM2(publicKey);
+Console.WriteLine($"SM2密文:{encryptSM2}");
+var decryptSM2 = encryptSM2.DecryptSM2(privateKey);
+Console.WriteLine($"SM2明文:{decryptSM2}");
+#endregion
+#region SM3加密
+var sm3Data = "123456789";
+var key = "qwertyui";
+var sm3Key = sm3Data.EncryptSM3(key);
+var sm3NotKey= sm3Data.EncryptSM3();
+Console.WriteLine($"SM3自定义Key密文:{sm3Key}");
+Console.WriteLine($"SM3无Key密文:{sm3NotKey}");
+#endregion

+ 15 - 0
SMTest/SMTest.csproj

@@ -0,0 +1,15 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net6.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\YS.SM2\YS.SM2.csproj" />
+    <ProjectReference Include="..\YS.SM3\YS.SM3.csproj" />
+  </ItemGroup>
+
+</Project>

+ 37 - 0
YS.DomesticPassword.sln

@@ -0,0 +1,37 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.0.31903.59
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YS.SM2", "YS.SM2\YS.SM2.csproj", "{FC558B50-596B-41C2-A47E-71859B72653C}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMTest", "SMTest\SMTest.csproj", "{E12ED78C-D599-41B0-ADF6-86B06C101A2B}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YS.SM3", "YS.SM3\YS.SM3.csproj", "{18C74F28-5428-4F4F-91D9-B1746FDCE672}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{FC558B50-596B-41C2-A47E-71859B72653C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{FC558B50-596B-41C2-A47E-71859B72653C}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{FC558B50-596B-41C2-A47E-71859B72653C}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{FC558B50-596B-41C2-A47E-71859B72653C}.Release|Any CPU.Build.0 = Release|Any CPU
+		{E12ED78C-D599-41B0-ADF6-86B06C101A2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{E12ED78C-D599-41B0-ADF6-86B06C101A2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{E12ED78C-D599-41B0-ADF6-86B06C101A2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{E12ED78C-D599-41B0-ADF6-86B06C101A2B}.Release|Any CPU.Build.0 = Release|Any CPU
+		{18C74F28-5428-4F4F-91D9-B1746FDCE672}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{18C74F28-5428-4F4F-91D9-B1746FDCE672}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{18C74F28-5428-4F4F-91D9-B1746FDCE672}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{18C74F28-5428-4F4F-91D9-B1746FDCE672}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {7FCF121A-C1E5-4D90-9443-229CB15C5448}
+	EndGlobalSection
+EndGlobal

+ 115 - 0
YS.SM2/Cipher.cs

@@ -0,0 +1,115 @@
+using Org.BouncyCastle.Crypto.Digests;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Math.EC;
+
+namespace YS.SM2
+{
+    /// <summary>
+    /// 密码
+    /// </summary>
+    public class Cipher
+    {
+        private int ct = 1;
+        private ECPoint p2;
+        private SM3Digest sm3keybase;
+        private SM3Digest sm3c3;
+        private byte[] key = new byte[32];
+        private byte keyOff = 0;
+        /// <summary>
+        /// 重置
+        /// </summary>
+        private void Reset()
+        {
+            sm3keybase = new SM3Digest();
+            sm3c3 = new SM3Digest();
+            var p = p2.GetEncoded();
+            sm3keybase.BlockUpdate(p, 0, p.Length);
+            sm3c3.BlockUpdate(p, 0, p.Length);
+            p = p2.GetEncoded();
+            sm3keybase.BlockUpdate(p, 0, p.Length);
+            ct = 1;
+            NextKey();
+        }
+        /// <summary>
+        /// 下一步处理
+        /// </summary>
+        private void NextKey()
+        {
+            var 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++;
+        }
+        /// <summary>
+        /// 初始化EC
+        /// </summary>
+        /// <param name="sm2">SM2代理</param>
+        /// <param name="userKey">用户Key</param>
+        /// <returns></returns>
+        public virtual ECPoint Init_enc(SM2Provider sm2, ECPoint userKey)
+        {
+            var key = sm2.Ecc_Key_Pair_Generator.GenerateKeyPair();
+            var ecpriv = (ECPrivateKeyParameters)key.Private;
+            var ecpub = (ECPublicKeyParameters)key.Public;
+            var k = ecpriv.D;
+            var c1 = ecpub.Q;
+            p2 = userKey.Multiply(k);
+            Reset();
+            return c1;
+        }
+        /// <summary>
+        /// 加密
+        /// </summary>
+        /// <param name="data">需要加密的数据</param>
+        public virtual void Encrypt(byte[] data)
+        {
+            sm3c3.BlockUpdate(data, 0, data.Length);
+            for (int i = 0; i < data.Length; i++)
+            {
+                if (keyOff == key.Length)
+                    NextKey();
+                data[i] ^= key[keyOff++];
+            }
+        }
+        /// <summary>
+        /// 初始化
+        /// </summary>
+        /// <param name="userD">userD</param>
+        /// <param name="c1">c1</param>
+        public virtual void Init_dec(BigInteger userD, ECPoint c1)
+        {
+            p2 = c1.Multiply(userD);
+            Reset();
+        }
+        /// <summary>
+        /// 解密
+        /// </summary>
+        /// <param name="data">需要解密的数据</param>
+        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);
+        }
+        /// <summary>
+        /// 最后处理
+        /// </summary>
+        /// <param name="c3">c3</param>
+        public virtual void Dofinal(byte[] c3)
+        {
+            var p = p2.GetEncoded();
+            sm3c3.BlockUpdate(p, 0, p.Length);
+            sm3c3.DoFinal(c3, 0);
+            Reset();
+        }
+    }
+}

+ 156 - 0
YS.SM2/SM2Provider.cs

@@ -0,0 +1,156 @@
+using Org.BouncyCastle.Crypto.Digests;
+using Org.BouncyCastle.Crypto.Generators;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Math.EC;
+using Org.BouncyCastle.Security;
+using System.Collections.Concurrent;
+
+namespace YS.SM2
+{
+    /// <summary>
+    /// SM2代理
+    /// </summary>
+    public class SM2Provider
+    {
+        private static readonly ConcurrentDictionary<string, string> sm2_param = new ConcurrentDictionary<string, string>
+        {
+            ["Ecc_Q"] = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",
+            ["Ecc_A"] = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",
+            ["Ecc_B"] = "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",
+            ["Ecc_N"] = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",
+            ["Ecc_X"] = "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",
+            ["Ecc_Y"] = "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0",
+        };
+        /// <summary>
+        /// Ecc_Q
+        /// </summary>
+        private BigInteger Ecc_Q
+        {
+            get
+            {
+                return new BigInteger(sm2_param.GetValueOrDefault("Ecc_Q"), 16);
+            }
+        }
+        /// <summary>
+        /// Ecc_A
+        /// </summary>
+        private BigInteger Ecc_A
+        {
+            get
+            {
+                return new BigInteger(sm2_param.GetValueOrDefault("Ecc_A"), 16);
+            }
+        }
+        /// <summary>
+        /// Ecc_B
+        /// </summary>
+        private BigInteger Ecc_B
+        {
+            get
+            {
+                return new BigInteger(sm2_param.GetValueOrDefault("Ecc_B"), 16);
+            }
+        }
+        /// <summary>
+        /// Ecc_N
+        /// </summary>
+        private BigInteger Ecc_N
+        {
+            get
+            {
+                return new BigInteger(sm2_param.GetValueOrDefault("Ecc_N"), 16);
+            }
+        }
+        /// <summary>
+        /// Ecc_X
+        /// </summary>
+        private BigInteger Ecc_X
+        {
+            get
+            {
+                return new BigInteger(sm2_param.GetValueOrDefault("Ecc_X"), 16);
+            }
+        }
+        /// <summary>
+        /// Ecc_Y
+        /// </summary>
+        private BigInteger Ecc_Y
+        {
+            get
+            {
+                return new BigInteger(sm2_param.GetValueOrDefault("Ecc_Y"), 16);
+            }
+        }
+        /// <summary>
+        /// Ecc_Curve
+        /// </summary>
+        public ECCurve Ecc_Curve { get; }
+        /// <summary>
+        /// Ecc_Key_Pair_Generator
+        /// </summary>
+        public ECKeyPairGenerator Ecc_Key_Pair_Generator { get; }
+        private static SM2Provider _instance;
+        private static readonly object _lock = new object();
+        /// <summary>
+        /// 构造SM2代理
+        /// </summary>
+        private SM2Provider()
+        {
+            Ecc_Curve = new FpCurve(Ecc_Q, Ecc_A, Ecc_B, null, null);
+            var ecc_point_g = Ecc_Curve.CreatePoint(Ecc_X, Ecc_Y);
+            var ecc_bc_spec = new ECDomainParameters(Ecc_Curve, ecc_point_g, Ecc_N);
+            var ecc_ecgenparam = new ECKeyGenerationParameters(ecc_bc_spec, new SecureRandom());
+            Ecc_Key_Pair_Generator = new ECKeyPairGenerator();
+            Ecc_Key_Pair_Generator.Init(ecc_ecgenparam);
+        }
+        /// <summary>
+        /// 单例实例
+        /// </summary>
+        public static SM2Provider Instance
+        {
+            get
+            {
+                if (_instance == null)
+                {
+                    lock (_lock)
+                    {
+                        if (_instance == null)
+                            _instance = new SM2Provider();
+                    }
+                }
+                return _instance;
+            }
+        }
+        /// <summary>
+        /// 获取SM2Z
+        /// </summary>
+        /// <param name="userId">userId</param>
+        /// <param name="userKey">userKey</param>
+        /// <returns></returns>
+        public virtual byte[] Sm2GetZ(byte[] userId, ECPoint userKey)
+        {
+            var sm3 = new SM3Digest();
+            byte[] p;
+            var len = userId.Length * 8;
+            sm3.Update((byte)(len >> 8 & 0x00ff));
+            sm3.Update((byte)(len & 0x00ff));
+            sm3.BlockUpdate(userId, 0, userId.Length);
+            p = Ecc_A.ToByteArray();
+            sm3.BlockUpdate(p, 0, p.Length);
+            p = Ecc_B.ToByteArray();
+            sm3.BlockUpdate(p, 0, p.Length);
+            p = Ecc_X.ToByteArray();
+            sm3.BlockUpdate(p, 0, p.Length);
+            p = Ecc_Y.ToByteArray();
+            sm3.BlockUpdate(p, 0, p.Length);
+            p = userKey.GetEncoded();
+            sm3.BlockUpdate(p, 0, p.Length);
+            p = userKey.GetEncoded();
+            sm3.BlockUpdate(p, 0, p.Length);
+            var md = new byte[sm3.GetDigestSize()];
+            sm3.DoFinal(md, 0);
+            return md;
+        }
+    }
+}

+ 89 - 0
YS.SM2/SM2Utils.cs

@@ -0,0 +1,89 @@
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Utilities.Encoders;
+using System.Text;
+using Org.BouncyCastle.Math.EC;
+
+namespace YS.SM2
+{
+    /// <summary>
+    /// SM2加解密
+    /// </summary>
+    public static class SM2Utils
+    {
+        /// <summary>
+        /// 获取公钥和私钥
+        /// </summary>
+        public static (string, string) GenerateKeyPair()
+        {
+            SM2Provider sm2 = SM2Provider.Instance;
+            AsymmetricCipherKeyPair key = sm2.Ecc_Key_Pair_Generator.GenerateKeyPair();
+            ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)key.Private;
+            ECPublicKeyParameters ecpub = (ECPublicKeyParameters)key.Public;
+            BigInteger privateKey = ecpriv.D;
+            ECPoint publicKey = ecpub.Q;
+            var _publicKey = Encoding.Default.GetString(Hex.Encode(publicKey.GetEncoded())).ToUpper();
+            var _privateKey = Encoding.Default.GetString(Hex.Encode(privateKey.ToByteArray())).ToUpper();
+            Console.WriteLine($"公钥:{_publicKey}");
+            Console.WriteLine($"私钥: {_privateKey}");
+            return (_publicKey, _privateKey);
+        }
+        /// <summary>
+        /// 加密
+        /// </summary>
+        /// <param name="publicKey">公钥</param>
+        /// <param name="data">需要加密的数据</param>
+        /// <returns></returns>
+        public static string EncryptSM2(this string data, string publicKey)
+        {
+            if (string.IsNullOrWhiteSpace(data))
+                return null;
+            if (string.IsNullOrWhiteSpace(publicKey))
+                return null;
+            var _data = Encoding.UTF8.GetBytes(data);
+            var _publicKey = Hex.Decode(publicKey);
+            var source = new byte[data.Length];
+            Array.Copy(_data, 0, source, 0, data.Length);
+            var cipher = new Cipher();
+            var sm2 = SM2Provider.Instance;
+            var userKey = sm2.Ecc_Curve.DecodePoint(_publicKey);
+            var c1 = cipher.Init_enc(sm2, userKey);
+            cipher.Encrypt(source);
+            var c3 = new byte[32];
+            cipher.Dofinal(c3);
+            var sc1 = Encoding.Default.GetString(Hex.Encode(c1.GetEncoded()));
+            var sc2 = Encoding.Default.GetString(Hex.Encode(source));
+            var sc3 = Encoding.Default.GetString(Hex.Encode(c3));
+            return (sc1 + sc2 + sc3).ToUpper();
+        }
+        /// <summary>
+        /// 解密
+        /// </summary>
+        /// <param name="encryptedData">需要解密的数据</param>
+        /// <param name="privateKey">私钥</param>
+        /// <returns></returns>
+        public static string DecryptSM2(this string encryptedData, string privateKey)
+        {
+            if (string.IsNullOrWhiteSpace(encryptedData))
+                return null;
+            if (string.IsNullOrWhiteSpace(privateKey))
+                return null;
+            var _encryptedData = Hex.Decode(encryptedData);
+            var _privateKey = Hex.Decode(privateKey);
+            var data = Encoding.UTF8.GetString(Hex.Encode(_encryptedData));
+            var c1Bytes = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(0, 130)));
+            var c2Len = _encryptedData.Length - 97;
+            var c2 = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(130, 2 * c2Len)));
+            var c3 = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(130 + 2 * c2Len, 64)));
+            var sm2 = SM2Provider.Instance;
+            var userD = new BigInteger(1, _privateKey);
+            var c1 = sm2.Ecc_Curve.DecodePoint(c1Bytes);
+            var cipher = new Cipher();
+            cipher.Init_dec(userD, c1);
+            cipher.Decrypt(c2);
+            cipher.Dofinal(c3);
+            return Encoding.UTF8.GetString(c2);
+        }
+    }
+}

+ 12 - 0
YS.SM2/YS.SM2.csproj

@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net6.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
+  </ItemGroup>
+
+</Project>

+ 144 - 0
YS.SM3/GeneralDigest .cs

@@ -0,0 +1,144 @@
+using Org.BouncyCastle.Crypto;
+
+namespace YS.SM3
+{
+    public abstract class GeneralDigest : IDigest
+    {
+        /// <summary>
+        /// 无参构造函数
+        /// </summary>
+        internal GeneralDigest()
+        {
+            SBuf = new byte[4];
+        }
+        /// <summary>
+        /// 定义内部缓存区大小
+        /// </summary>
+        private const int ByteLength = 64;
+        /// <summary>
+        /// 内部消息摘要
+        /// </summary>
+        private readonly byte[] SBuf;
+        /// <summary>
+        /// 待更新的内部消息摘要索引
+        /// </summary>
+        private int SBufoff;
+        /// <summary>
+        /// 待更新的内部消息摘要索引大小
+        /// </summary>
+        private int SBufoffCount;
+        /// <summary>
+        /// 算法名称
+        /// </summary>
+        public abstract string AlgorithmName { get; }
+        /// <summary>
+        /// 有参构造函数
+        /// </summary>
+        /// <param name="noop"></param>
+        internal GeneralDigest(GeneralDigest noop)
+        {
+            SBuf = new byte[noop.SBuf.Length];
+            Buffer.BlockCopy(noop.SBuf, 0, SBuf, 0, noop.SBuf.Length);
+            SBufoff = noop.SBufoff;
+            SBufoffCount = noop.SBufoffCount;
+        }
+        /// <summary>
+        /// 处理消息摘要
+        /// ABCDEFGH 串联
+        /// </summary>
+        /// <param name="input"></param>
+        /// <param name="inOff"></param>
+        internal abstract void ProcessWord(byte[] input, int inOff);
+        internal abstract void ProcessLength(long bitlength);
+        /// <summary>
+        /// 迭代压缩
+        /// </summary>
+        internal abstract void ProcessBlock();
+        /// <summary>
+        /// 字节快更新整个消息摘要
+        /// </summary>
+        /// <param name="input">输入</param>
+        /// <param name="inOff">z坐标</param>
+        /// <param name="length">长度</param>
+        /// <exception cref="NotImplementedException"></exception>
+        public void BlockUpdate(byte[] input, int inOff, int length)
+        {
+            //更新当前的消息摘要
+            while ((SBufoff != 0) && (length > 0))
+            {
+                Update(input[inOff]);
+                inOff++;
+                length--;
+            }
+            //处理完整的消息摘要
+            while (length > SBuf.Length)
+            {
+                ProcessWord(input, inOff);
+
+                inOff += SBuf.Length;
+                length -= SBuf.Length;
+                SBufoffCount += SBuf.Length;
+            }
+            //填充剩余的消息摘要
+            while (length > 0)
+            {
+                Update(input[inOff]);
+                inOff++;
+                length--;
+            }
+        }
+        /// <summary>
+        /// 关闭摘要,产生最终的摘要值,dofinal调用使得摘要复位
+        /// </summary>
+        /// <param name="output"></param>
+        /// <param name="outOff"></param>
+        /// <returns></returns>
+        public abstract int DoFinal(byte[] output, int outOff);
+        /// <summary>
+        /// 摘要应用其压缩功能的内部缓冲区的大小
+        /// </summary>
+        /// <returns></returns>
+        public int GetByteLength() => ByteLength;
+        /// <summary>
+        /// 消息摘要生成的摘要的大小
+        /// </summary>
+        /// <returns></returns>
+        public abstract int GetDigestSize();
+        /// <summary>
+        /// 重启
+        /// </summary>
+        public virtual void Reset()
+        {
+            SBufoffCount = 0;
+            SBufoff = 0;
+            Array.Clear(SBuf, 0, SBuf.Length);
+        }
+        /// <summary>
+        /// 完成消息摘要,产生最终的结果值
+        /// </summary>
+        public void Finish()
+        {
+            var bitLength = SBufoffCount << 3;
+            //添加字节占位
+            Update(unchecked((byte)128));
+            while (SBufoff != 0) Update(unchecked(0));
+            ProcessLength(bitLength);
+            ProcessBlock();
+        }
+        /// <summary>
+        /// 字节更新摘要
+        /// </summary>
+        /// <param name="input">输入字节</param>
+        /// <exception cref="NotImplementedException"></exception>
+        public void Update(byte input)
+        {
+            SBuf[SBufoff++] = input;
+            if (SBufoff == SBuf.Length)
+            {
+                ProcessWord(SBuf, 0);
+                SBufoff = 0;
+            }
+            SBufoffCount = 0;
+        }
+    }
+}

+ 274 - 0
YS.SM3/SM3Provider.cs

@@ -0,0 +1,274 @@
+namespace YS.SM3
+{
+    public class SM3Provider : GeneralDigest
+    {
+        public override string AlgorithmName
+        {
+            get
+            {
+                return "SM3";
+            }
+
+        }
+        /// <summary>
+        /// 消息摘要生成的摘要的大小
+        /// </summary>
+        /// <returns></returns>
+        public override int GetDigestSize()
+        {
+            return DigestLength;
+        }
+        /// <summary>
+        /// SM3算法产生的哈希值大小
+        /// </summary>
+        private const int DigestLength = 32;
+        /// <summary>
+        /// 初始值IV
+        /// </summary>
+        private static readonly int[] IV = new int[] {
+            0x7380166f, 0x4914b2b9, 0x172442d7,
+            unchecked((int)0xda8a0600), unchecked((int)0xa96f30bc), 0x163138aa,
+            unchecked((int)0xe38dee4d), unchecked((int)0xb0fb0e4e)
+        };
+        /// <summary>
+        /// 备份的字寄存器
+        /// </summary>
+        private readonly int[] BackupRegister = new int[8];
+        /// <summary>
+        /// 使用中的字寄存器
+        /// </summary>
+        private readonly int[] DoingRegister = new int[8];
+        private static readonly int[] X0 = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+        private readonly int[] xSpace = new int[68];
+        private int xOff;
+        /// <summary>
+        /// 0到15的Tj常量
+        /// </summary>
+        private readonly int TOne = 0x79cc4519;
+        /// <summary>
+        /// 16到63的Tj常量
+        /// </summary>
+        private readonly int TSecond = 0x7a879d8a;
+        public SM3Provider()
+        {
+            Reset();
+        }
+        /// <summary>
+        /// 复制构造函数
+        /// </summary>
+        /// <param name="t"></param>
+        public SM3Provider(SM3Provider t) : base(t)
+        {
+            Buffer.BlockCopy(t.xSpace, 0, xSpace, 0, t.xSpace.Length);//效率更高
+            xOff = t.xOff;
+            Buffer.BlockCopy(t.BackupRegister, 0, BackupRegister, 0, t.BackupRegister.Length);//效率更高
+        }
+        /// <summary>
+        /// 将复制的对象状态还原到该对象。
+        /// 此方法的实现应尝试避免或最小化内存分配以执行重置。
+        /// </summary>
+        public override void Reset()
+        {
+            base.Reset();
+            Buffer.BlockCopy(IV, 0, BackupRegister, 0, IV.Length);//效率更高
+            xOff = 0;
+            Buffer.BlockCopy(X0, 0, xSpace, 0, X0.Length);//效率更高
+        }
+        internal override void ProcessBlock()
+        {
+            int j;
+            var ww = xSpace;
+            //64位比特串
+            var ww_ = new int[64];
+            #region 块消息扩展
+            //消息扩展16 TO 67
+            for (j = 16; j < 68; j++)
+            {
+                ww[j] = P1(ww[j - 16] ^ ww[j - 9] ^ (Rotate(ww[j - 3], 15))) ^ (Rotate(ww[j - 13], 7)) ^ ww[j - 6];
+            }
+            //消息扩展0 TO 63
+            for (j = 0; j < 64; j++)
+            {
+                ww_[j] = ww[j] ^ ww[j + 4];
+            }
+            #endregion
+            #region 压缩函数
+            var backupRegister = BackupRegister;
+            var doingRegister = DoingRegister;//A,B,C,D,E,F,G,H为字寄存器
+            Buffer.BlockCopy(backupRegister, 0, doingRegister, 0, IV.Length);//效率更高
+            //中间变量SS1,SS2,TT1,TT2
+            int SS1, SS2, TT1, TT2;
+            int aaa;
+            //将消息分组B(i)划分为16个字
+            for (j = 0; j < 16; j++)
+            {
+                aaa = Rotate(doingRegister[0], 12);
+                SS1 = aaa + doingRegister[4] + Rotate(TOne, j);
+                SS1 = Rotate(SS1, 7);
+                SS2 = SS1 ^ aaa;
+                TT1 = FFOne(doingRegister[0], doingRegister[1], doingRegister[2]) + doingRegister[3] + SS2 + ww_[j];
+                TT2 = GGOne(doingRegister[4], doingRegister[5], doingRegister[6]) + doingRegister[7] + SS1 + ww[j];
+                #region 更新各个寄存器
+                doingRegister[3] = doingRegister[2];
+                doingRegister[2] = Rotate(doingRegister[1], 9);
+                doingRegister[1] = doingRegister[0];
+                doingRegister[0] = TT1;
+                doingRegister[7] = doingRegister[6];
+                doingRegister[6] = Rotate(doingRegister[5], 19);
+                doingRegister[5] = doingRegister[4];
+                doingRegister[4] = P0(TT2);
+                #endregion
+            }
+            for (j = 16; j < 64; j++)
+            {
+                aaa = Rotate(doingRegister[0], 12);
+                SS1 = aaa + doingRegister[4] + Rotate(TSecond, j);
+                SS1 = Rotate(SS1, 7);
+                SS2 = SS1 ^ aaa;
+                TT1 = FFSecond(doingRegister[0], doingRegister[1], doingRegister[2]) + doingRegister[3] + SS2 + ww_[j];
+                TT2 = GGSecond(doingRegister[4], doingRegister[5], doingRegister[6]) + doingRegister[7] + SS1 + ww[j];
+                #region 更新各个寄存器
+                doingRegister[3] = doingRegister[2];
+                doingRegister[2] = Rotate(doingRegister[1], 9);
+                doingRegister[1] = doingRegister[0];
+                doingRegister[0] = TT1;
+                doingRegister[7] = doingRegister[6];
+                doingRegister[6] = Rotate(doingRegister[5], 19);
+                doingRegister[5] = doingRegister[4];
+                doingRegister[4] = P0(TT2);
+                #endregion
+            }
+            #endregion
+            //256比特的杂凑值y =doingRegister(j+1) ABCDEFGH
+            for (j = 0; j < 8; j++)
+            {
+                backupRegister[j] ^= doingRegister[j];
+            }
+            // Reset
+            xOff = 0;
+            Buffer.BlockCopy(X0, 0, xSpace, 0, X0.Length);//效率更高
+        }
+        internal override void ProcessWord(byte[] in_Renamed, int inOff)
+        {
+            var n = in_Renamed[inOff] << 24;
+            n |= (in_Renamed[++inOff] & 0xff) << 16;
+            n |= (in_Renamed[++inOff] & 0xff) << 8;
+            n |= (in_Renamed[++inOff] & 0xff);
+            xSpace[xOff] = n;
+            if (++xOff == 16)
+                ProcessBlock();
+        }
+        internal override void ProcessLength(long bitLength)
+        {
+            if (xOff > 14)
+                ProcessBlock();
+            xSpace[14] = (int)(SM3SupportGeneral.URShift(bitLength, 32));
+            xSpace[15] = (int)(bitLength & unchecked((int)0xffffffff));
+        }
+        /// <summary>
+        /// 写入到大端
+        /// </summary>
+        /// <param name="n"></param>
+        /// <param name="bs"></param>
+        /// <param name="off"></param>
+        public static void IntToBigEndian(int n, byte[] bs, int off)
+        {
+            bs[off] = (byte)(SM3SupportGeneral.URShift(n, 24));
+            bs[++off] = (byte)(SM3SupportGeneral.URShift(n, 16));
+            bs[++off] = (byte)(SM3SupportGeneral.URShift(n, 8));
+            bs[++off] = (byte)(n);
+        }
+        /// <summary>
+        /// 关闭摘要,产生最终的摘要值。doFinal调用使摘要复位。
+        /// </summary>
+        /// <param name="out_Renamed"></param>
+        /// <param name="outOff"></param>
+        /// <returns></returns>
+        public override int DoFinal(byte[] out_Renamed, int outOff)
+        {
+            Finish();
+            for (int i = 0; i < 8; i++)
+            {
+                IntToBigEndian(BackupRegister[i], out_Renamed, outOff + i * 4);
+            }
+            Reset();
+            return DigestLength;
+        }
+        /// <summary>
+        /// x循环左移n比特运算
+        /// </summary>
+        /// <param name="x"></param>
+        /// <param name="n"></param>
+        /// <returns></returns>
+        private static int Rotate(int x, int n)
+        {
+            return (x << n) | (SM3SupportGeneral.URShift(x, (32 - n)));
+        }
+        #region 置换函数
+        /// <summary>
+        /// 置换函数P0
+        /// </summary>
+        /// <param name="x"></param>
+        /// <returns></returns>
+        private static int P0(int x)
+        {
+            return (x) ^ Rotate(x, 9) ^ Rotate(x, 17);
+        }
+        /// <summary>
+        /// 置换函数P1
+        /// </summary>
+        /// <param name="x"></param>
+        /// <returns></returns>
+        private static int P1(int x)
+        {
+            return (x) ^ Rotate(x, 15) ^ Rotate(x, 23);
+        }
+        #endregion
+        #region 布尔函数  坐标
+        /// <summary>
+        /// 0到15的布尔函数FF (X⊕^Y⊕Z)
+        /// </summary>
+        /// <param name="X"></param>
+        /// <param name="Y"></param>
+        /// <param name="Z"></param>
+        /// <returns></returns>
+        private static int FFOne(int X, int Y, int Z)
+        {
+            return (X ^ Y ^ Z);
+        }
+        /// <summary>
+        /// 16到63的布尔函数FF
+        /// </summary>
+        /// <param name="X"></param>
+        /// <param name="Y"></param>
+        /// <param name="Z"></param>
+        /// <returns></returns>
+        private static int FFSecond(int X, int Y, int Z)
+        {
+            return ((X & Y) | (X & Z) | (Y & Z));
+        }
+        /// <summary>
+        /// 0到15的布尔函数GG
+        /// </summary>
+        /// <param name="X"></param>
+        /// <param name="Y"></param>
+        /// <param name="Z"></param>
+        /// <returns></returns>
+        private static int GGOne(int X, int Y, int Z)
+        {
+            return (X ^ Y ^ Z);
+        }
+        /// <summary>
+        /// 16到63的布尔函数GG
+        /// </summary>
+        /// <param name="X"></param>
+        /// <param name="Y"></param>
+        /// <param name="Z"></param>
+        /// <returns></returns>
+        private static int GGSecond(int X, int Y, int Z)
+        {
+            return ((X & Y) | (~X & Z));
+        }
+        #endregion
+    }
+}

+ 55 - 0
YS.SM3/SM3SupportGeneral.cs

@@ -0,0 +1,55 @@
+namespace YS.SM3
+{
+    /// <summary>
+    /// 可能存在不同的组合,所以覆盖多种参数不同过的应对方法
+    /// </summary>
+    public class SM3SupportGeneral
+    {
+        /// <summary>
+        /// 使用特定字符执行无符号按位右移动
+        /// </summary>
+        /// <param name="number">要操作的编号</param>
+        /// <param name="bits">要移动的位数</param>
+        /// <returns></returns>
+        public static int URShift(int number, long bits)
+        {
+            return URShift(number, (int)bits);
+        }
+        /// <summary>
+        /// 使用特定数字进行无符号按位右移
+        /// </summary>
+        /// <param name="number">要操作的编号</param>
+        /// <param name="bits">要移动的位数</param>
+        /// <returns></returns>
+        public static long URShift(long number, int bits)
+        {
+            if (number >= 0)
+                return number >> bits;
+            else
+                return (number >> bits) + (2L << ~bits);
+        }
+        /// <summary>
+        /// 使用特定数字进行无符号右位移
+        /// </summary>
+        /// <param name="number">要操作的编号</param>
+        /// <param name="bits">要移动的位数</param>
+        /// <returns></returns>
+        public static int URShift(int number, int bits)
+        {
+            if (number >= 0)
+                return number >> bits;
+            else
+                return (number >> bits) + (2 << ~bits);
+        }
+        /// <summary>
+        /// 使用特定数字进行无符号按位右移
+        /// </summary>
+        /// <param name="num">要操作的编号</param>
+        /// <param name="bits">要移动的位数</param>
+        /// <returns></returns>
+        public static long URShift(long num, long bits)
+        {
+            return URShift(num, (int)bits);
+        }
+    }
+}

+ 77 - 0
YS.SM3/SM3Utils.cs

@@ -0,0 +1,77 @@
+using Org.BouncyCastle.Crypto.Macs;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Utilities.Encoders;
+using System.Text;
+
+namespace YS.SM3
+{
+    /// <summary>
+    /// SM3加密
+    /// </summary>
+    public static class SM3Utils
+    {
+        /// <summary>
+        /// SM3加密
+        /// </summary>
+        /// <param name="data">待加密字符串</param>
+        /// <param name="key">Key</param>
+        /// <returns></returns>
+        public static string EncryptSM3(this string data, string key)
+        {
+            var msg1 = Encoding.Default.GetBytes(data);
+            var key1 = Encoding.Default.GetBytes(key);
+            var keyParameter = new KeyParameter(key1);
+            var sm3 = new SM3Provider();
+            var mac = new HMac(sm3);//带密钥的杂凑算法
+            mac.Init(keyParameter);
+            mac.BlockUpdate(msg1, 0, msg1.Length);
+            var result = new byte[mac.GetMacSize()];
+            mac.DoFinal(result, 0);
+            var hexResult = Hex.Encode(result);
+            return Convert.ToBase64String(hexResult);
+        }
+        /// <summary>
+        /// SM3加密
+        /// </summary>
+        /// <param name="data">待加密字符串</param>
+        /// <returns></returns>
+        public static string EncryptSM3(this string data)
+        {
+            var msg = data.ToHexByte();//把字符串转成16进制的ASCII码 
+            var sm3 = new SM3Provider();
+            sm3.BlockUpdate(msg, 0, msg.Length);
+            var md = new byte[sm3.GetDigestSize()];//SM3算法产生的哈希值大小
+            sm3.DoFinal(md, 0);
+            var hexResult = Hex.Encode(md);
+            return Convert.ToBase64String(hexResult);
+        }
+        /// <summary>
+        /// 字符串转16进制字节数组
+        /// </summary>
+        /// <param name="data"></param>
+        /// <returns></returns>
+        private static byte[] ToHexByte(this string data)
+        {
+            var msg1 = Encoding.Default.GetBytes(data);
+            var hexString = BytesToHexString(msg1);
+            var returnBytes = new byte[hexString.Length / 2];
+            for (int i = 0; i < returnBytes.Length; i++)
+                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 10);
+            return returnBytes;
+        }
+        /// <summary>
+        /// byte[]数组转16进制字符串
+        /// </summary>
+        /// <param name="input">byte[]数组</param>
+        /// <returns>16进制字符串</returns>
+        private static string BytesToHexString(byte[] input)
+        {
+            var hexString = new StringBuilder(64);
+            for (int i = 0; i < input.Length; i++)
+            {
+                hexString.Append(String.Format("{0:X2}", input[i]));
+            }
+            return hexString.ToString();
+        }
+    }
+}

+ 12 - 0
YS.SM3/YS.SM3.csproj

@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net6.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
+  </ItemGroup>
+
+</Project>