最近在做现货市场的数据采集,发现api接口返回的数据是被加密的。通过查看官方提供的硬件USBKey里面的信息,发现这里使用的SM2算法。
于是搜索了下,基本是直接使用第三方库实现,但是这些库有许多版本,最终加解密的结果也不尽相同。
比如在nuget中,有名为BouncyCastle.Crypto.dll的包,最后更新时间为2016年1月25日,经过测试,这个包过于陈旧,得不到当前项目的加解密结果。
后来试了试下载量最大的包,名为Portable.BouncyCastle,版本1.9,同时可以设置C1C3C2的模式,而上面提到的包却不能设置。
工具类如下

public class SM2CryptoUtil
{
    public SM2CryptoUtil(byte[] pubkey, byte[] privkey, Mode mode)
    {
        this.pubkey = pubkey;
        this.privkey = privkey;
        this.mode = mode;
    }
    public SM2CryptoUtil(string pubkey, string privkey, Mode mode = Mode.C1C2C3, bool isPkcs8 = false)
    {
        if (!isPkcs8)
        {
            if (pubkey != null) this.pubkey = Decode(pubkey);
            if (privkey != null) this.privkey = Decode(privkey);
        }
        else
        {
            if (pubkey != null) this.pubkey = ((ECPublicKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(pubkey))).Q.GetEncoded();
            if (privkey != null) this.privkey = ((ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privkey))).D.ToByteArray();
        }
        this.mode = mode;
    }
    byte[] pubkey;
    byte[] privkey;
    Mode mode;
    ICipherParameters _privateKeyParameters;
    ICipherParameters PrivateKeyParameters
    {
        get
        {
            var r = _privateKeyParameters;
            if (r == null) r = _privateKeyParameters = new ECPrivateKeyParameters(new BigInteger(1, privkey), new ECDomainParameters(GMNamedCurves.GetByName("SM2P256V1")));
            return r;
        }
    }
    ICipherParameters _publicKeyParameters;
    ICipherParameters PublicKeyParameters
    {
        get
        {
            var r = _publicKeyParameters;
            if (r == null)
            {
                var x9ec = GMNamedCurves.GetByName("SM2P256V1");
                r = _publicKeyParameters = new ECPublicKeyParameters(x9ec.Curve.DecodePoint(pubkey), new ECDomainParameters(x9ec));
            }
            return r;
        }
    }

    public static void GenerateKeyHex(out string pubkey, out string privkey)
    {
        GenerateKey(out var a, out var b);
        pubkey = Hex.ToHexString(a);
        privkey = Hex.ToHexString(b);
    }
    public static void GenerateKey(out byte[] pubkey, out byte[] privkey)
    {
        var g = new ECKeyPairGenerator();
        g.Init(new ECKeyGenerationParameters(new ECDomainParameters(GMNamedCurves.GetByName("SM2P256V1")), new SecureRandom()));
        var k = g.GenerateKeyPair();
        pubkey = ((ECPublicKeyParameters)k.Public).Q.GetEncoded(false);
        privkey = ((ECPrivateKeyParameters)k.Private).D.ToByteArray();
    }

    public byte[] Decrypt(byte[] data)
    {
        if (mode == Mode.C1C3C2) data = C132ToC123(data);
        var sm2 = new SM2Engine(new SM3Digest());
        sm2.Init(false, this.PrivateKeyParameters);
        return sm2.ProcessBlock(data, 0, data.Length);
    }
    public byte[] Encrypt(byte[] data)
    {
        var sm2 = new SM2Engine(new SM3Digest());
        sm2.Init(true, new ParametersWithRandom(PublicKeyParameters));
        data = sm2.ProcessBlock(data, 0, data.Length);
        if (mode == Mode.C1C3C2) data = C123ToC132(data);
        return data;
    }
    public byte[] Sign(byte[] msg, byte[] id = null)
    {
        var sm2 = new SM2Signer(new SM3Digest());
        ICipherParameters cp;
        if (id != null) cp = new ParametersWithID(new ParametersWithRandom(PrivateKeyParameters), id);
        else cp = new ParametersWithRandom(PrivateKeyParameters);
        sm2.Init(true, cp);
        sm2.BlockUpdate(msg, 0, msg.Length);
        return sm2.GenerateSignature();
    }
    public bool VerifySign(byte[] msg, byte[] signature, byte[] id = null)
    {
        var sm2 = new SM2Signer(new SM3Digest());
        ICipherParameters cp;
        if (id != null) cp = new ParametersWithID(PublicKeyParameters, id);
        else cp = PublicKeyParameters;
        sm2.Init(false, cp);
        sm2.BlockUpdate(msg, 0, msg.Length);
        return sm2.VerifySignature(signature);
    }
    static byte[] C123ToC132(byte[] c1c2c3)
    {
        var gn = GMNamedCurves.GetByName("SM2P256V1");
        int c1Len = (gn.Curve.FieldSize + 7) / 8 * 2 + 1; //sm2p256v1的这个固定65。可看GMNamedCurves、ECCurve代码。
        int c3Len = 32; //new SM3Digest().getDigestSize();
        byte[] result = new byte[c1c2c3.Length];
        Array.Copy(c1c2c3, 0, result, 0, c1Len); //c1
        Array.Copy(c1c2c3, c1c2c3.Length - c3Len, result, c1Len, c3Len); //c3
        Array.Copy(c1c2c3, c1Len, result, c1Len + c3Len, c1c2c3.Length - c1Len - c3Len); //c2
        return result;
    }
    static byte[] C132ToC123(byte[] c1c3c2)
    {
        var gn = GMNamedCurves.GetByName("SM2P256V1");
        int c1Len = (gn.Curve.FieldSize + 7) / 8 * 2 + 1;
        int c3Len = 32; //new SM3Digest().getDigestSize();
        byte[] result = new byte[c1c3c2.Length];
        Array.Copy(c1c3c2, 0, result, 0, c1Len); //c1: 0->65
        Array.Copy(c1c3c2, c1Len + c3Len, result, c1Len, c1c3c2.Length - c1Len - c3Len); //c2
        Array.Copy(c1c3c2, c1Len, result, c1c3c2.Length - c3Len, c3Len); //c3
        return result;
    }
    //static byte[] Decode(string key)
    //{
    //    return Regex.IsMatch(key, "^[0-9a-f]+$", RegexOptions.IgnoreCase) ? Hex.Decode(key) : Convert.FromBase64String(key);
    //}
    public enum Mode
    {
        C1C2C3, C1C3C2
    }
    /// <summary>
    /// 16进制转字节数组
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public byte[] Decode(string key)
    {

        return Hex.Decode(key);
        //return Regex.IsMatch(key, "^[0-9a-f]+$", RegexOptions.IgnoreCase) ? Hex.Decode(key) : Convert.FromBase64String(key);
    }
}

使用方法:

SM2CryptoUtil s=new SM2CryptoUtil(公钥, 私钥, SM2CryptoUtil.Mode.C1C3C2);
string result= s.Decrypt("待解密字节数组")

一般数据会被转换成16进制数据,所以我在工具类里提供了Decode方法

最后修改:2023 年 12 月 22 日
如果觉得我的文章对你有用,请随意赞赏