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