从字节[]/pem字符串获取公共密钥-BouncyCastle C# [英] Get public key from byte [] / pem string - BouncyCastle C#

查看:100
本文介绍了从字节[]/pem字符串获取公共密钥-BouncyCastle C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写与SmartCard通信的程序.智能卡是公钥的来源.我得到的公用密钥为 byte [] ,并将其转换为 String .我想使用密钥创建证书签名请求.

I'm writing programm which communicates with SmartCard. The SmartCard is a source of Public Key. I get the public key as byte [] and convert it to String. I would like to use the key to create certificate signing request.

如何将byte []/字符串(我的公共密钥)转换为任何C#密钥参数(例如RsaKeyParameters或AsymmetricKeyParameter)?

How can I transform byte[] / string (my public key) to any C# key parameters (e.g. RsaKeyParameters or AsymmetricKeyParameter)?

推荐答案

这是从pem字符串中获取 X509证书的简单方法:

Here is a simple way to get X509Certificate from pem string:

pemString = pemString.Replace("-----BEGIN CERTIFICATE-----", "").Replace("-----END CERTIFICATE-----", "");
byte[] buffer = Convert.FromBase64String(pemString);
X509CertificateParser parser = new X509CertificateParser();
_certificate = parser.ReadCertificate(buffer);

当我们拥有X509Certificate对象时,我们可以获得公共密钥:

When we have X509Certificate object we can get public key:

AsymmetricKeyParameter publicKey = certificate.GetPublicKey();

这篇关于从字节[]/pem字符串获取公共密钥-BouncyCastle C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆