亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于C#實(shí)現(xiàn)SM2加簽驗(yàn)簽工具

 更新時(shí)間:2024年10月21日 10:21:02   作者:天天代碼碼天天  
這篇文章主要為大家詳細(xì)介紹了如何基于C#實(shí)現(xiàn)一個(gè)SM2加簽驗(yàn)簽工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

效果

項(xiàng)目

代碼

using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Asn1.GM;
using System;
using System.Text;
using System.Windows.Forms;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Security;
using System.Linq;
 
namespace SM2VerifySignTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        int toDigit(char ch, int index)
        {
            int digit = Convert.ToInt32(ch.ToString(), 16);
            if (digit == -1)
            {
                throw new SystemException("Illegal hexadecimal character " + ch + " at index " + index);
            }
            return digit;
        }
 
        string HexToBase64(string hexString)
        {
            byte[] bytes = Enumerable.Range(0, hexString.Length)
                                     .Where(x => x % 2 == 0)
                                     .Select(x => Convert.ToByte(hexString.Substring(x, 2), 16))
                                     .ToArray();
            return Convert.ToBase64String(bytes);
        }
 
        byte[] hexStrToByte(String hexStr)
        {
            if ((null == hexStr) || (hexStr.Length == 0))
            {
                return null;
            }
            char[] hexData = hexStr.ToCharArray();
            int len = hexData.Length;
            if ((len & 0x1) != 0)
            {
                throw new SystemException("Odd number of characters.");
            }
            byte[] out1 = new byte[len >> 1];
            int i = 0;
            for (int j = 0; j < len; i++)
            {
                int f = toDigit(hexData[j], j) << 4;
                j++;
                f |= toDigit(hexData[j], j);
                j++;
                out1[i] = ((byte)(f & 0xFF));
            }
            return out1;
        }
 
        X9ECParameters x9ec = GMNamedCurves.GetByName("SM2P256V1");
 
 
        /**
        *生成
        */
        void GenerateKey(out string pubkeyStr, out string prikeyStr)
        {
            var g = new ECKeyPairGenerator();
            g.Init(new ECKeyGenerationParameters(new ECDomainParameters(x9ec), new SecureRandom()));
            var k = g.GenerateKeyPair();
            byte[] pubkey = ((ECPublicKeyParameters)k.Public).Q.GetEncoded(false);
            byte[] privkey = ((ECPrivateKeyParameters)k.Private).D.ToByteArray();
            prikeyStr = BitConverter.ToString(privkey).Replace("-", "");
            pubkeyStr = BitConverter.ToString(pubkey).Replace("-", "");
        }
 
        /**
        *加簽
        */
        string Sign(string prikeyStr, string data)
        {
            byte[] msg = Encoding.UTF8.GetBytes(data);
            byte[] priKey = hexStrToByte(prikeyStr);
            SM2Signer sm2Signer = new SM2Signer();
            ECPrivateKeyParameters privateKeyParameters = new ECPrivateKeyParameters(new BigInteger(1, priKey), new ECDomainParameters(x9ec));
 
            sm2Signer.Init(true, privateKeyParameters);
            sm2Signer.BlockUpdate(msg, 0, msg.Length);
            return Hex.ToHexString(sm2Signer.GenerateSignature());
        }
 
 
        /*
        * 驗(yàn)簽
        */
        bool verifySign(string pubkeyStr, string data, string sign)
        {
            byte[] signHex = hexStrToByte(sign);
            byte[] pubkey = hexStrToByte(pubkeyStr);
            byte[] msgByte = Encoding.UTF8.GetBytes(data);
            SM2Signer sm2Signer = new SM2Signer();
            ECPublicKeyParameters publicKeyParameters = new ECPublicKeyParameters(x9ec.Curve.DecodePoint(pubkey), new ECDomainParameters(x9ec));
            sm2Signer.Init(false, publicKeyParameters);
            sm2Signer.BlockUpdate(msgByte, 0, msgByte.Length);
            return sm2Signer.VerifySignature(signHex);
        }
 
        private void btnVerify_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
            try
            {
                string pubk = txtPubkey.Text;
                string data = txtData.Text;
                string sign = txtSign.Text;
                bool b = verifySign(pubk, data, sign);
                if (b)
                {
                    txtResult.Text = "驗(yàn)證成功";
                }
                else
                {
                    txtResult.Text = "驗(yàn)證失敗";
                }
            }
            catch (Exception ex)
            {
                txtResult.Text = "驗(yàn)證異常:" + ex.Message;
            }
        }
 
        /// <summary>
        /// Base64字符串轉(zhuǎn)Hex字符串↓
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] bytes = Convert.FromBase64String(txtBase64.Text); // 將base64字符串轉(zhuǎn)換為byte數(shù)組
                string hexString = BitConverter.ToString(bytes).Replace("-", ""); // 將byte數(shù)組轉(zhuǎn)換為Hex字符串
                txtHex.Text = hexString;
            }
            catch (Exception ex)
            {
                txtHex.Text = "轉(zhuǎn)換異常:" + ex.Message;
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //string pubkeyStr = "";
            //string prikeyStr = "";
            //GenerateKey(out pubkeyStr, out prikeyStr);
            //txtPriKey.Text = prikeyStr;
            //txtPubkey.Text = pubkeyStr;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string prik = txtPriKey.Text;
                string data = txtData.Text;
                txtSign.Text = Sign(prik, data);
            }
            catch (Exception ex)
            {
                txtSign.Text = "加簽識(shí)別:" + ex.Message;
            }
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            string pubkeyStr = "";
            string prikeyStr = "";
            GenerateKey(out pubkeyStr, out prikeyStr);
            txtPriKey.Text = prikeyStr;
            txtPubkey.Text = pubkeyStr;
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string hexString = txtHex.Text;
                string base64String = HexToBase64(hexString);
                txtBase64.Text = base64String;
            }
            catch (Exception ex)
            {
                txtBase64.Text = "轉(zhuǎn)換異常:" + ex.Message;
            }
        }
 
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                string originalString = txtStr.Text;
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(originalString);
                string base64String = Convert.ToBase64String(bytes);
                txtBase64.Text = base64String;
            }
            catch (Exception ex)
            {
                txtBase64.Text = "編碼異常:" + ex.Message;
            }
        }
 
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                string base64String = txtBase64.Text;
                byte[] bytes = Convert.FromBase64String(base64String);
                string decodedString = Encoding.UTF8.GetString(bytes);
                txtStr.Text = decodedString;
            }
            catch (Exception ex)
            {
                txtStr.Text = "解碼異常:" + ex.Message;
            }
        }
    }
}

到此這篇關(guān)于基于C#實(shí)現(xiàn)SM2加簽驗(yàn)簽工具的文章就介紹到這了,更多相關(guān)C#SM2加簽驗(yàn)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Unity游戲開發(fā)之2048游戲的實(shí)現(xiàn)

    Unity游戲開發(fā)之2048游戲的實(shí)現(xiàn)

    2048是一款數(shù)字益智游戲,初始數(shù)字則是由2+2組成的基數(shù)4。在操作方面的不同則表現(xiàn)為一步一格的移動(dòng),變成更為爽快的一次到底。相同數(shù)字的方?jīng)r在靠攏、相撞時(shí)會(huì)相加。本文將通過Unity3D實(shí)現(xiàn)這一游戲,需要的可以參考一下
    2022-03-03
  • C#多線程編程之使用ReaderWriterLock類實(shí)現(xiàn)多用戶讀與單用戶寫同步的方法

    C#多線程編程之使用ReaderWriterLock類實(shí)現(xiàn)多用戶讀與單用戶寫同步的方法

    這篇文章主要介紹了C#多線程編程之使用ReaderWriterLock類實(shí)現(xiàn)多用戶讀與單用戶寫同步的方法,涉及C#多線程操作讀寫鎖定的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • C#實(shí)現(xiàn)tostring轉(zhuǎn)換成16進(jìn)制的方法

    C#實(shí)現(xiàn)tostring轉(zhuǎn)換成16進(jìn)制的方法

    本文介紹了在C#中將整數(shù)、字節(jié)數(shù)組、字符串轉(zhuǎn)換為十六進(jìn)制字符串,以及將十六進(jìn)制字符串轉(zhuǎn)換回整數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • Unity UGUI的Outline描邊組件的介紹使用示例

    Unity UGUI的Outline描邊組件的介紹使用示例

    這篇文章主要介紹了Unity UGUI的Outline描邊組件的介紹使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • C#機(jī)器入門學(xué)習(xí)之判斷日?qǐng)?bào)是否合格詳解

    C#機(jī)器入門學(xué)習(xí)之判斷日?qǐng)?bào)是否合格詳解

    這篇文章主要給大家介紹了關(guān)于C#機(jī)器入門學(xué)習(xí)之判斷日?qǐng)?bào)是否合格的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 帶著問題讀CLR via C#(筆記一)CLR的執(zhí)行模型

    帶著問題讀CLR via C#(筆記一)CLR的執(zhí)行模型

    CLR (Common Language Runtime) 是一個(gè)可以由多種編程語(yǔ)言使用的“運(yùn)行時(shí)”。
    2013-04-04
  • C# 實(shí)現(xiàn)特殊字符快速轉(zhuǎn)碼

    C# 實(shí)現(xiàn)特殊字符快速轉(zhuǎn)碼

    這篇文章主要介紹了C# 實(shí)現(xiàn)特殊字符快速轉(zhuǎn)碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • C#與C++與互操作實(shí)例講解

    C#與C++與互操作實(shí)例講解

    在本篇文章里小編給大家整理了關(guān)于C#與C++與互操作實(shí)例以及相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2019-08-08
  • winform 中顯示異步下載的圖片

    winform 中顯示異步下載的圖片

    本文主要介紹利用WebClient異步下載圖片,顯示在GridView上,需要的朋友可以參考下。
    2016-05-05
  • C#并行編程之PLINQ(并行LINQ)

    C#并行編程之PLINQ(并行LINQ)

    這篇文章介紹了C#并行編程之PLINQ(并行LINQ),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論