如何使用PublicKey.EncodedKeyValue.Format(true)从X509Store获取公共密钥

时间:2019-07-02 08:56:48

标签: c# web-services rsa sha256

这是我的新手,我正在为我们的客户端创建一个Web服务,该服务需要使用私钥和标头中使用SHA256withRSA算法的私钥来签名数据。如何使用X509Certificate2的PublicKey.EncodedKeyValue.Format(true)属性产生公钥?

我已经尝试过:https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.publickey.encodedkeyvalue?view=netframework-4.8

string certSubject = "CN=cnName";
            X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            my.Open(OpenFlags.ReadOnly);
            foreach (X509Certificate2 x509 in my.Certificates)
            {
                if (x509.Subject.Contains(certSubject))
                {
                    RSACryptoServiceProvider publicCertifiedRSACryptoServiceProvider = x509.PublicKey.Key as RSACryptoServiceProvider;

                    string publickeyRAW = x509.PublicKey.EncodedKeyValue.Format(true);
                    Console.ReadLine();
                    break;
                }
            }

我期望最终输出为字符串或字节,但这就是我得到的:

30 82 01 0a 02 82 01 01 00 9c 71 98 bc de 99 0b f7 b7 0a bf 79 f7 02 2a 0c c1 a0 45 10 6b 5c a0 d7 e5 02 1e 01 f9 3c d7 69 4e 7c ec ee 56 cf c2 88 74 fd bb fe 97 77 07 6b f7 85 ee 8d e6 1b 2f 8e 90 03 6e 4a f8 42 9e 10 e9 7d f7 1b bd dc ba 2b 38 8e 7b 5a c9 67 2e 87 a7 87 54 98 16 7a 67 aa c7 d7 6c f5 50 4c e6 7d 79 8c 46 f4 c6 39 57 b4 75 ef 8d d5 8d 3a 33 aa 5e b9 c3 48 6f eb 04 fe b0 86 b8 28 20 f3 57 44 1b 57 6f fd ce c0 15 f0 ff cf 73 72 a4 76 55 84 e9 c9 54 f1 3a fe 29 c0 be d4 0d 38 1c 73 1e 41 a5 d2 a5 f7 4c 9f 72 49 ba 28 5d a4 bf db cd 05 68 aa f8 62 66 e0 70 65 35 9f 56 64 48 f7 1b df d1 0e f6 2f e7 36 16 5b 1c ff d5 5a a5 97 63 de 8c 36 da 2c 9d 13 d2 72 09 c1 b6 48 de 8c 67 c1 64 2e 1d 39 cf cd 0c 88 67 3c e3 3c 17 eb 23 fe d2 47 6f 51 20 28 88 7e a7 22 b8 a8 a7 43 d4 5d 02 03 01 00 01

如何解密或将其转换为字符串?

1 个答案:

答案 0 :(得分:0)

根据您可能想要的结果

Convert.ToBase64String(x509.PublicKey.EncodedKeyValue.RawData);