从C#中的x509证书字节读取RSA公钥

时间:2017-08-19 05:57:56

标签: c# encryption rsa x509certificate x509certificate2

在C#中,我正在从HTTP请求中检索RSA公钥,它为我提供了以base64编码的密钥。

WebClient webClient = new WebClient();
string rsaPublicKeyBase64 = webClient.DownloadString("http://localhost:8000/getkey");
// rsaPublicKeyBase64 = LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlHZk1BMEdDU3FHU0liM0RRRUJBUVVBQTRHTkFEQ0JpUUtCZ1FEdDAwcXQ2Zi9UUXdMQmVsVExRdVlXb05xSQoxbmRkcFpaOGh0WWs4d0NLbmFuRFJpWkJ1NVo5NnBNT01yNi84RS9JUzB0amV4WGdsVjh0WFlKK0NKc1lDUHhoCnBDUkduUW9rYkE2MnpOODVXNEROVUNMQ0cyMXlXcndscFhjSmxLYkY2dFhxdmd3TGRQb2RwZzUwY3RrWkI4R0UKbDBLS3VOV3JHZXRad045V0NRSURBUUFCCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo=

然后我解码base 64 RSA公钥。

byte[] rsaPublicKey = Convert.FromBase64String(rsaPublicKeyBase64);
/*
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDt00qt6f/TQwLBelTLQuYWoNqI
1nddpZZ8htYk8wCKnanDRiZBu5Z96pMOMr6/8E/IS0tjexXglV8tXYJ+CJsYCPxh
pCRGnQokbA62zN85W4DNUCLCG21yWrwlpXcJlKbF6tXqvgwLdPodpg50ctkZB8GE
l0KKuNWrGetZwN9WCQIDAQAB
-----END PUBLIC KEY-----
*/

我的下一步是将包含我的RSA公钥证书的byte[]转换为RSACryptoServiceProvider类型。我在网上找到了答案,但似乎没有一个对我有用。

这是我目前拥有的(不起作用)。

string rsaPublicKeyFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
X509Certificate2 cert = null;
try {
    File.WriteAllBytes(rsaPublicKeyFile, rsaPublicKey);
    cert = new X509Certificate2(rsaPublicKeyFile);
} finally {
    File.Delete(rsaPublicKeyFile);
}

我收到一个未处理的异常错误,如下面的屏幕截图所示。

Unhandled Exception Error

System.Security.Cryptography.CryptographicException: 'Cannot find the requested object.

1 个答案:

答案 0 :(得分:3)

感谢@ Crypt32,我设法通过引用PublicKey Class documentation

来解决它

我写了一个函数GetCertificateFromBytes(byte[] cert),它写入临时文件以便阅读证书:

public static X509Certificate2 GetCertificateFromBytes(byte[] cert) {
    string certFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
    try {
        File.WriteAllBytes(certFile, cert);

        X509Store store = new X509Store(StoreLocation.CurrentUser);
        try {
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certCollection = store.Certificates;
            return certCollection[0];
        } finally {
            store.Close();
        }
    } finally {
        File.Delete(certFile);
    }
}

然后加密:

X509Certificate2 cert = GetCertificateFromBytes(rsaPublicKey);
RSACryptoServiceProvider publicKeyProvider = (RSACryptoServiceProvider)cert.PublicKey.Key;
byte[] encrypted = publicKeyProvider.Encrypt(data, false);