/// 数据
/// </summary>
public byte[] Data { get; set; }
/// <summary>
/// 公钥
/// </summary>
public string PublicKey { get; set; }
/// <summary>
/// 私钥
/// </summary>
public string PrivateKey { get; set; }
/// <summary>
/// 获取密钥
/// </summary>
/// <param name="privateKey">私钥</param>
/// <param name="publicKey">公钥</param>
public static void GetKey(out string privateKey, out string publicKey)
{
SM2 sm2 = SM2.Instance;
AsymmetricCipherKeyPair key = sm2.EccKeyPairGenerator.GenerateKeyPair();
ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)key.Private;
ECPublicKeyParameters ecpub = (ECPublicKeyParameters)key.Public;
publicKey = Encoding.Default.GetString(Hex.Encode(ecpub.Q.GetEncoded())).ToUpper();
privateKey = Encoding.Default.GetString(Hex.Encode(ecpriv.D.ToByteArray())).ToUpper();
}
#region 解密
public object Decrypt(Sm2Crypto entity)
{
var data = https://www.isolves.com/it/cxkf/yy/C/2022-03-29/!string.IsNullOrEmpty(entity.Str) ?
Hex.Decode(entity.Str) :
entity.Data;
return Encoding.Default.GetString(Decrypt(Hex.Decode(entity.PrivateKey), data));
}
/// <summary>
/// 解密
/// </summary>
/// <param name="privateKey"></param>
/// <param name="encryptedData"></param>
/// <returns></returns>
private static byte[] Decrypt(byte[] privateKey, byte[] encryptedData)
{
if (null == privateKey || privateKey.Length == 0)
{
return null;
}
if (encryptedData =https://www.isolves.com/it/cxkf/yy/C/2022-03-29/= null || encryptedData.Length == 0)
{
return null;
}
String data = https://www.isolves.com/it/cxkf/yy/C/2022-03-29/Encoding.Default.GetString(Hex.Encode(encryptedData));
byte[] c1Bytes = Hex.Decode(Encoding.Default.GetBytes(data.Substring(0, 130)));
int c2Len = encryptedData.Length - 97;
byte[] c2 = Hex.Decode(Encoding.Default.GetBytes(data.Substring(130, 2 * c2Len)));
byte[] c3 = Hex.Decode(Encoding.Default.GetBytes(data.Substring(130 + 2 * c2Len, 64)));
SM2 sm2 = SM2.Instance;
BigInteger userD = new BigInteger(1, privateKey);
ECPoint c1 = sm2.EccCurve.DecodePoint(c1Bytes);
//c1.Normalize();
Cipher cipher = new Cipher();
cipher.InitDec(userD, c1);
cipher.Decrypt(c2);
cipher.Dofinal(c3);
return c2;
}
#endregion
#region 加密
public string Encrypt(Sm2Crypto entity)
{
var data = https://www.isolves.com/it/cxkf/yy/C/2022-03-29/!string.IsNullOrEmpty(entity.Str) ?
Encoding.Default.GetBytes(entity.Str) :
entity.Data;
return Encrypt(Hex.Decode(entity.PublicKey), data);
}
/// <summary>
/// 加密
/// </summary>
/// <param name="publicKey"></param>
/// <param name="data"></param>
/// <returns></returns>
private static string Encrypt(byte[] publicKey, byte[] data)
{
if (null == publicKey || publicKey.Length == 0)
{
return null;
}
if (data =https://www.isolves.com/it/cxkf/yy/C/2022-03-29/= null || data.Length == 0)
{
return null;
}
byte[] source = new byte[data.Length];
Array.Copy(data, 0, source, 0, data.Length);
Cipher cipher = new Cipher();
SM2 sm = SM2.Instance;
ECPoint userKey = sm.EccCurve.DecodePoint(publicKey);
//userKey.Normalize();
ECPoint c1 = cipher.InitEnc(sm, userKey);
cipher.Encrypt(source);
byte[] c3 = new byte[32];
cipher.Dofinal(c3);
String sc1 = Encoding.Default.GetString(Hex.Encode(c1.GetEncoded()));
String sc2 = Encoding.Default.GetString(Hex.Encode(source));
String sc3 = Encoding.Default.GetString(Hex.Encode(c3));
return (sc1 + sc2 + sc3).ToUpper();
}
#endregion
}
}
4.最终调用结果

文章插图

文章插图
希望这边文章能帮助相关人员,欢迎评论+转发,谢谢所有水友们!!!
【C# 实现SM2国密加密帮助类】
推荐阅读
- SpringBoot WebFlux整合R2DBC实现数据库反应式编程
- 求职|九零,零零后的各位,你们工作能实现双休吗?
- [空气开关]选择空气开关
- vr看房技术如何实现 vr看房怎么 ***
- 生科医学|上海:全域静态管理 全员核酸筛查!尽早实现社会面清零
- 三种方法可以实现扩列的效果 扩列是什么意思
- 荣耀|荣耀、理想汽车互联合作:荣耀Magic4可以实现无感解锁
- 最全摆地摊指南:没有这些技巧如何实现月入5万
- 职业教育|电视剧《我们的婚姻》,女主想工作的原因,是想实现人生价值吧
- 【静电地板安装】实现完美安装的四个步骤
