首页 > 编程语言 >C#.NET 国密 BASE64编码的私钥提取16进制私钥

C#.NET 国密 BASE64编码的私钥提取16进制私钥

时间:2023-04-08 11:34:00浏览次数:43  
标签:公钥 私钥 进制 16 C# BASE64 String

C#.NET 国密 BASE64编码的私钥提取16进制私钥,

从BASE64编码的公钥中提取16进制字符串公钥,

 从BASE64编码的私钥中提取16进制字符串私钥,

 锦州银行在使用这种私钥 。

 

String mchtPubKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAElmWpvTHHsQEUMSLoMcDssXAjCkdgjCkncPXNnnapIEkSXAYZMV7/f0ba+b/VKsZTRgvZ2InkxSRLFHVznnf1lg==";
                String mchtPriKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgGKTw1sFkats23rL373anMYL+r1dlIv1MWor8PCIqrmugCgYIKoEcz1UBgi2hRANCAASWZam9McexARQxIugxwOyxcCMKR2CMKSdw9c2edqkgSRJcBhkxXv9/Rtr5v9UqxlNGC9nYieTFJEsUdXOed/WW";

String bankSm2PublicKey = SM2ExtUtil.GetPublicKey(bankPubKey);
                Console.WriteLine("公钥16进制字符串:" + bankSm2PublicKey);
                String bankSm2PrivateKey = SM2ExtUtil.GetPrivateKey(bankPriKey);
                Console.WriteLine("私钥16进制字符串:" + bankSm2PrivateKey);

 

工具类SM2ExtUtil:

需要nuget 引用 BouncyCastle,搜索 Portable.BouncyCastle 。

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

namespace CommonUtils
{
    public static class SM2ExtUtil
    {
        /// <summary>
        /// 从BASE64编码的公钥中提取16进制字符串公钥
        /// </summary>
        /// <param name="publicKeyBase64"></param>
        /// <returns></returns>
        public static String GetPublicKey(String publicKeyBase64)
        {
            var pubkey1=  ((ECPublicKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKeyBase64))).Q.GetEncoded();
            //把公钥的04截取掉,只保留后128位。
            var pubkey2 = Hex.ToHexString(pubkey1).Substring(2).ToUpper();
            return pubkey2;
        }

        /// <summary>
        /// 从BASE64编码的私钥中提取16进制字符串私钥
        /// </summary>
        /// <param name="PrivateKeyBase64"></param>
        /// <returns></returns>
        public static String GetPrivateKey(String PrivateKeyBase64)
        {
            var bankPriKey1 = ((ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(PrivateKeyBase64))).D.ToByteArray();
            var bankPriKey2 = Hex.ToHexString(bankPriKey1).ToUpper();
            return bankPriKey2;
        }
    }
}

--

如果你只需要 AsymmetricKeyParameter 对象:

AsymmetricKeyParameter mchtPubKeyObj = PublicKeyFactory.CreateKey(Convert.FromBase64String(mchtPubKeyBase64));

 

AsymmetricKeyParameter objPrivateKey = PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKeyBase64));

 

标签:公钥,私钥,进制,16,C#,BASE64,String
From: https://www.cnblogs.com/runliuv/p/17298228.html

相关文章

  • css:writing-mode控制文字水平排布或垂直排布
    writing-mode属性定义了文本水平或垂直排布以及在块级元素中文本的行进方向。文档https://developer.mozilla.org/zh-CN/docs/Web/CSS/writing-mode语法/*关键字值*/writing-mode:horizontal-tb;writing-mode:vertical-rl;writing-mode:vertical-lr;效果在线demo......
  • openstack高可用(pike版本)-架构
    1、API服务包括*-api,neutron-server,glance-registry,nova-novncproxy,keystone,httpd等。由HAProxy提供负载均衡,将请求按照一定的算法转到某个节点上的API服务。由Pacemaker提供VIP。2、内部组件包括*-scheduler,nova-conductor,nova-cert等。它们都是无状态的,因此可以......
  • Vue进阶(四十九):在Webapp上使用input:file, 指定capture属性调用默认相机,摄像,录音功能
    在webapp上使用input:file,指定capture属性调用默许相机,摄像,录音功能。在webapp上使用input:file,指定capture属性调用默认相机,摄像,录音功能在iOS6下开发webapp,使用input之file,很有用<inputtype="file"accept="image/*"capture="camera"><inputtype="file"acce......
  • Simple Finance的表变化讲解
                  ......
  • c语言结构体
    #include<iostream>#include<string>usingnamespacestd;structm{inta;stringb;};intmain(void){inti,j;inta[10];charb[10];char*e,*f;mk[10];k[0]=k[1];//e=f;//b[0]=b[1];//a[0]......
  • :)关于torch函数中dim的解释-读这篇就够了-|
    关于torch函数中dim的解释-读这篇就够了1dim的取值范围1)-1的作用0,1,2,-1. 其中-1最后一维即20,1,2,3,-1其中-1最后一维即32)维度0,1,2,3表示BCHW,常在CV任务中使用。0,1,2表示CHW,常在NLP任务中使用。3)用图来说明  2NLP代码中实战dimfromtorchimportnnf......
  • COMP20007 Task 2: C Problem
    Task2:CProblemAssignment1GeneralTask1:AlgorithmicDesignTask2:CProblemAssignmentSubmissionAcademicHonestyLatePolicyRequirements:CProgrammingProgrammingStyleMarkBreakdownAdditionalSupportAcknowledgementsTask2:CProblemOlivia'sgrandfa......
  • 2-CUDA
    1.环境搭建NVIDIA于2006年推出CUDA(ComputeUnifiedDevicesArchitecture),可以利用其推出的GPU进行通用计算,将并行计算从大型集群扩展到了普通显卡,使得用户只需要一台带有Geforce显卡的笔记本就能跑较大规模的并行处理程序。使用显卡的好处是,和大型集群相比功耗非常低,成本也不高,......
  • pg-AutoVacuum(1)
    根据之前月报的分析,PostgreSQL中的MVCC机制(详见月报)同时存储新旧版本的元组,对于经常更新的表来说,会造成表膨胀的情况。为了解决这个问题,PostgreSQL引入了VACUUM和ANALYZE命令,并且引入了AutoVacuum自动清理。在PostgreSQL中,AutoVacuum自动清理操作包括:删除或重用无效元组的磁盘......
  • c_pointer
    #include<iostream>usingnamespacestd;intf(int**r,int**s){inttemp=**r;inttemp2=**s;int*z=*r;*r=*s;*s=z;printf("**r=%d\n",**r);printf("**s=%d\n",**......