百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

C# SM2 SM3 SM4 使用 sm2代理服务未启动怎么解决

lipiwang 2024-11-04 14:33 13 浏览 0 评论

效果-SM2


公钥:04ca3e272e11b5633681cb0fbbfd8c162be08918ce5b644cd33d49c17be8674caf6c20a11de8b65333924dfe7d42246abb4a4c36b663bef1aafc624a35acf4d2b1
私钥:27e9d8598679a6066f4dfebb2b5d5fe830ce6c6b8b9cf4a4e515e55678ba44a9
原文:测试信息lxw123456!@#$%^&*()abcDEFG
结果:043267543680002a384bdcc8e2db3648f1d5d1a5956ca4798bdfe3ca4a667a620d4df25683e260147ab846a049505ae88ff572f983078f3b1c7d4692c384e6c045292023ff0d69ae3c4f4139b19843a3a5ffa130a1e6758659f4d11a51d32d17a617dda0612319a091a4dcac7e6a67d55f4145c882ca9bbb687641182468a2962d5a0c6bf25ddc

在线校验

地址: https://the-x.cn/cryptography/Sm2.aspx

https://the-x.cn/cryptography/Sm2.aspx

效果-SM3

效果-SM4


项目

代码

SM2Utils.cs

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Text;

namespace SMDemo
{
public class SM2KeyPair
{
public string PrivateKey { get; set; } //私钥
public string PublicKey { get; set; } //公钥

}
public class SM2Utils
{
//国密标准256位曲线参数
BigInteger SM2_ECC_P = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16);
BigInteger SM2_ECC_A = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16);
BigInteger SM2_ECC_B = new BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16);
BigInteger SM2_ECC_N = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16);
BigInteger SM2_ECC_H = BigInteger.One;
BigInteger SM2_ECC_GX = new BigInteger("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16);
BigInteger SM2_ECC_GY = new BigInteger("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16);

public SM2KeyPair GenerateKey()
{
ECCurve curve = new FpCurve(SM2_ECC_P, SM2_ECC_A, SM2_ECC_B, SM2_ECC_N, SM2_ECC_H);
ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N);
ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
ECKeyGenerationParameters aKeyGenParams = new ECKeyGenerationParameters(domainParams, new SecureRandom());
keyPairGenerator.Init(aKeyGenParams);
AsymmetricCipherKeyPair aKp = keyPairGenerator.GenerateKeyPair();
ECPublicKeyParameters aPub = (ECPublicKeyParameters)aKp.Public;
ECPrivateKeyParameters aPriv = (ECPrivateKeyParameters)aKp.Private;

BigInteger privateKey = aPriv.D;
ECPoint publicKey = aPub.Q;

byte[] pubkey = Hex.Encode(publicKey.GetEncoded());
string temp_pubkey = Encoding.UTF8.GetString(pubkey);

string temp_prikey = Encoding.UTF8.GetString(Hex.Encode(privateKey.ToByteArray()));

return new SM2KeyPair() { PrivateKey = temp_prikey, PublicKey = temp_pubkey };
}

/// <summary>
/// 加密函数
/// </summary>
/// <param name="publicKeyStr"></param>
/// <param name="plainText"></param>
/// <returns></returns>
public string Encrypt(string publicKeyStr, string plainText)
{
byte[] publicKey = Hex.Decode(publicKeyStr);
byte[] data = Encoding.UTF8.GetBytes(plainText);

if ( == publicKey || publicKey.Length == 0)
{
return ;
}
if (data == || data.Length == 0)
{
return ;
}
byte[] source = new byte[data.Length];
Array.Copy(data, 0, source, 0, data.Length);
ECCurve curve = new FpCurve(SM2_ECC_P, SM2_ECC_A, SM2_ECC_B, SM2_ECC_N, SM2_ECC_H);
ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N);
ECPoint userkey = curve.DecodePoint(publicKey);
ECPublicKeyParameters aPub = new ECPublicKeyParameters(userkey, domainParams);

SM2Engine sm2Engine = new SM2Engine();
sm2Engine.Init(true, new ParametersWithRandom(aPub));
byte[] enc = sm2Engine.ProcessBlock(source, 0, source.Length);
return Encoding.UTF8.GetString(Hex.Encode(enc));

}


/// <summary>
/// 解密函数
/// </summary>
/// <param name="privateKey"></param>
/// <param name="encryptedData"></param>
/// <returns></returns>
public string Decrypt(string privateKeyStr, string chiperText)
{
byte[] privateKey = Hex.Decode(privateKeyStr);
byte[] encryptedData = Hex.Decode(chiperText);

if ( == privateKey || privateKey.Length == 0)
{
return ;
}
if (encryptedData == || encryptedData.Length == 0)
{
return ;
}

byte[] enc = new byte[encryptedData.Length];
Array.Copy(encryptedData, 0, enc, 0, encryptedData.Length);
BigInteger userD = new BigInteger(1, privateKey);
ECCurve curve = new FpCurve(SM2_ECC_P, SM2_ECC_A, SM2_ECC_B, SM2_ECC_N, SM2_ECC_H);
ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N);
ECPrivateKeyParameters aPriv = new ECPrivateKeyParameters(userD, domainParams);

SM2Engine sm2Engine = new SM2Engine();
sm2Engine.Init(false, aPriv);
byte[] dec = sm2Engine.ProcessBlock(enc, 0, enc.Length);
return Encoding.UTF8.GetString(dec);
}
}
}

Sm3Utils.cs

using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Text;

namespace SMDemo
{
public class Sm3Utils
{
public static string GetHash(string plainText)
{
String resultHexString = "";
// 将字符串转换成byte数组
byte[] srcData = Encoding.UTF8.GetBytes(plainText);
SM3Digest digest = new SM3Digest();
digest.BlockUpdate(srcData, 0, srcData.Length);
byte[] hash = new byte[digest.GetDigestSize()];
digest.DoFinal(hash, 0);
// 将返回的hash值转换成16进制字符串
resultHexString = new UTF8Encoding().GetString(Hex.Encode(hash));
return resultHexString;
}
}
}

Sm4Utils.cs

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
using System.Text;

namespace SMDemo
{
public class Sm4Utils
{
public static string EncryptEBC(string plaintext, string key)
{
var dataBytes = Encoding.UTF8.GetBytes(plaintext);
var keyBytes = Encoding.UTF8.GetBytes(key);
KeyParameter keyParam = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
IBufferedCipher inCipher = CipherUtilities.GetCipher("SM4/ECB/PKCS7Padding");
inCipher.Init(true, keyParam);
byte[] cipher = inCipher.DoFinal(dataBytes);
return Hex.ToHexString(cipher, 0, cipher.Length);
}

public static string DecryptEBC(string chipherText, string key)
{
var dataBytes = Hex.Decode(chipherText);
var keyBytes = Encoding.UTF8.GetBytes(key);
KeyParameter keyParam = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
IBufferedCipher inCipher = CipherUtilities.GetCipher("SM4/ECB/PKCS7Padding");
inCipher.Init(false, keyParam);
byte[] plain = inCipher.DoFinal(dataBytes);
return Encoding.UTF8.GetString(plain);
}


/// <summary>
/// CBC模式加密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string EncryptCBC(string plaintext, string key)
{
var dataBytes = Encoding.UTF8.GetBytes(plaintext);
var keyBytes = Encoding.UTF8.GetBytes(key);
KeyParameter keyParam = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
ParametersWithIV keyParamWithIv = new ParametersWithIV(keyParam, keyBytes);
IBufferedCipher inCipher = CipherUtilities.GetCipher("SM4/CBC/PKCS7Padding");
inCipher.Init(true, keyParamWithIv);
byte[] cipher = inCipher.DoFinal(dataBytes);
return Hex.ToHexString(cipher, 0, cipher.Length);
}

/// <summary>
/// CBC模式解密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DecryptCBC(string chipherText, string key)
{
var dataBytes = Hex.Decode(chipherText);
var keyBytes = Encoding.UTF8.GetBytes(key);
KeyParameter keyParam = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
ParametersWithIV keyParamWithIv = new ParametersWithIV(keyParam, keyBytes);
IBufferedCipher inCipher = CipherUtilities.GetCipher("SM4/CBC/PKCS7Padding");
inCipher.Init(false, keyParamWithIv);
byte[] plain = inCipher.DoFinal(dataBytes);
return Encoding.UTF8.GetString(plain);
}

}
}

相关推荐

前端入门——css 网格轨道详细介绍

上篇前端入门——cssGrid网格基础知识整体大概介绍了cssgrid的基本概念及使用方法,本文将介绍创建网格容器时会发生什么?以及在网格容器上使用行、列属性如何定位元素。在本文中,将介绍:...

Islands Architecture(孤岛架构)在携程新版首页的实践

一、项目背景2022,携程PC版首页终于迎来了首次改版,完成了用户体验与技术栈的全面升级。作为与用户连接的重要入口,旧版PC首页已经陪伴携程走过了22年,承担着重要使命的同时,也遇到了很多问题:维护/...

HTML中script标签中的那些属性

HTML中的<script>标签详解在HTML中,<script>标签用于包含或引用JavaScript代码,是前端开发中不可或缺的一部分。通过合理使用<scrip...

CSS 中各种居中你真的玩明白了么

页面布局中最常见的需求就是元素或者文字居中了,但是根据场景的不同,居中也有简单到复杂各种不同的实现方式,本篇就带大家一起了解下,各种场景下,该如何使用CSS实现居中前言页面布局中最常见的需求就是元...

CSS样式更改——列表、表格和轮廓

上篇文章主要介绍了CSS样式更改篇中的字体设置Font&边框Border设置,这篇文章分享列表、表格和轮廓,一起来看看吧。1.列表List1).列表的类型<ulstyle='list-...

一文吃透 CSS Flex 布局

原文链接:一文吃透CSSFlex布局教学游戏这里有两个小游戏,可用来练习flex布局。塔防游戏送小青蛙回家Flexbox概述Flexbox布局也叫Flex布局,弹性盒子布局。它决定了...

css实现多行文本的展开收起

背景在我们写需求时可能会遇到类似于这样的多行文本展开与收起的场景:那么,如何通过纯css实现这样的效果呢?实现的难点(1)位于多行文本右下角的展开收起按钮。(2)展开和收起两种状态的切换。(3)文本...

css 垂直居中的几种实现方式

前言设计是带有主观色彩的,同样网页设计中的css一样让人摸不头脑。网上列举的实现方式一大把,或许在这里你都看到过,但既然来到这里我希望这篇能让你看有所收获,毕竟这也是前端面试的基础。实现方式备注:...

WordPress固定链接设置

WordPress设置里的最后一项就是固定链接设置,固定链接设置是决定WordPress文章及静态页面URL的重要步骤,从站点的SEO角度来讲也是。固定链接设置决定网站URL,当页面数少的时候,可以一...

面试发愁!吃透 20 道 CSS 核心题,大厂 Offer 轻松拿

前端小伙伴们,是不是一想到面试里的CSS布局题就发愁?写代码时布局总是对不齐,面试官追问兼容性就卡壳,想跳槽却总被“多列等高”“响应式布局”这些问题难住——别担心!从今天起,咱们每天拆解一...

3种CSS清除浮动的方法

今天这篇文章给大家介绍3种CSS清除浮动的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。首先,这里就不讲为什么我们要清楚浮动,反正不清除浮动事多多。下面我就讲3种常用清除浮动的...

2025 年 CSS 终于要支持强大的自定义函数了?

大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发!1.什么是CSS自定义属性CSS自...

css3属性(transform)的一个css3动画小应用

闲言碎语不多讲,咱们说说css3的transform属性:先上效果:效果说明:当鼠标移到a标签的时候,从右上角滑出二维码。实现方法:HTML代码如下:需要说明的一点是,a链接的跳转需要用javasc...

CSS基础知识(七)CSS背景

一、CSS背景属性1.背景颜色(background-color)属性值:transparent(透明的)或color(颜色)2.背景图片(background-image)属性值:none(没有)...

CSS 水平居中方式二

<divid="parent"><!--定义子级元素--><divid="child">居中布局</div>...

取消回复欢迎 发表评论: