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(