C#实现SM2国密签名签验签和加密解密

java版本已放出 对应的java版本
话不多说,上码。
依赖,BouncyCastle.Crypto.dll ,经测试,可与java版本的bcprov-jdk15on-1.62.jar互通。

在这里插入代码片
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Signers;
using System;
using System.Text;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Asn1.GM;
using Cbel.netSdk.utils;
using log4net;

namespace sm2
{
   
    class SM2
    {
   

        private static readonly ILog log = LogManager.GetLogger(typeof(SM2));

        private ECPrivateKeyParameters privateKeyParameters;


        private ECPublicKeyParameters publicKeyParameters;

        private SM2Param sm2Param;

        private static X9ECParameters x9ECParameters = GMNamedCurves.GetByName("sm2p256v1");

        public SM2(string priKey, string pubKey)
        {
   
            this.init(HexUtils.decode(priKey), HexUtils.decode(pubKey));
        }

        private void init(byte[] priKey, byte[] pubKey)
        {
   
            this.sm2Param = new SM2Param();
            if (null != priKey && this.privateKeyParameters == null)
            {
   
                this.privateKeyParameters = new ECPrivateKeyParameters(new BigInteger(1, priKey), this.sm2Param.ecc_bc_spec);
            }
            if (null != pubKey && this.publicKeyParameters == null)
            {
   
                this.publicKeyParameters = new ECPublicKeyParameters(this.sm2Param.ecc_curve.DecodePoint(pubKey), this.sm2Param.ecc_bc_spec);

            }
        }


        /**
         * 加签
         */
        public string Sign(string data)
        {
   
            byte[] msg = Encoding.UTF8.GetBytes(data);
            SM2Signer sm2Signer = new SM2Signer();
            sm2Signer.Init(true, this.privateKeyParameters);
            sm2Signer.BlockUpdate(msg, 0, msg.Length);
            return Hex.ToHexString(sm2Signer.GenerateSignature());
        }


        /*
         * 验签
         */
        public bool verifySign(
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 25
    评论
实现SM2数字签名,加密和解密需要使用SM2算法库,可以使用Python的第三方库pycryptodome或gmssl。以下是使用pycryptodome实现SM2数字签名,加密和解密的示例代码: 1. SM2数字签名 ```python from Crypto.PublicKey import ECC from Crypto.Signature import DSS from Crypto.Hash import SHA256 from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad from binascii import b2a_hex, a2b_hex # 生成SM2密钥对 private_key = ECC.generate(curve='sm2') public_key = private_key.public_key() # 待签名数据 data = b'hello, world' # 计算消息摘要 digest = SHA256.new(data) # 对消息摘要进行数字签名 signer = DSS.new(private_key, 'fips-186-3') signature = signer.sign(digest) # 验证数字签名 verifier = DSS.new(public_key, 'fips-186-3') try: verifier.verify(digest, signature) print("Signature is valid.") except ValueError: print("Signature is invalid.") ``` 2. SM2加密和解密 ```python from Crypto.Cipher import SM2Cipher from Crypto.PublicKey import ECC from Crypto.Util.Padding import pad, unpad from binascii import b2a_hex, a2b_hex # 生成SM2密钥对 private_key = ECC.generate(curve='sm2') public_key = private_key.public_key() # 加密数据 plaintext = b'hello, world' cipher = SM2Cipher.new(public_key) ciphertext = cipher.encrypt(plaintext) # 解密数据 cipher = SM2Cipher.new(private_key) decrypttext = cipher.decrypt(ciphertext) print("Plaintext:", plaintext) print("Ciphertext:", b2a_hex(ciphertext)) print("Decrypttext:", decrypttext) ``` 注意,以上代码只是示例,实际使用时需要注意安全性和数据格式转换等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值