X509Certificate2
次のコードを使用して、証明書の公開鍵をエクスポートしようとしています。
X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certificateStore.Open(OpenFlags.ReadOnly);
var exportCertificates = certificateStore.Certificates.Find(X509FindType.FindByThumbprint, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", false);
certificateStore.Close();
// Get Base64 string of the public key
byte[] arr = exportCertificates[0].PublicKey.EncodedKeyValue.RawData;
string b64ExportCertificate = Convert.ToBase64String(arr);
// Import the certificate
X509Certificate2 importCertificate = new X509Certificate2(Convert.FromBase64String(b64ExportCertificate));
最後の行を実行すると、次の例外がスローされます。
System.Security.Cryptography.CryptographicException
Cannot find the requested object
誰かがこれを解決する方法を知っていますか?
注:上記のコードサンプルは「機能的」ですが、疑似コードです。実際には、あるアプリケーションで証明書をエクスポートしてから、デジタル署名の目的で別のアプリケーションに送信しています(したがって、公開鍵のみを送信します)