재우니의 블로그

CryptographicException 에서 공급자 형식이 명시되지 않았습니다. 오류 발생

 

“Invalid provider type specified” CryptographicException when trying to load private key of certificate

 

코드를 구현할때 아래 처럼 인증서를 bytes 와 비밀번호 받는 정도로 구현할 수 있습니다. 대부분 보통 msdn 이나 예제가 이렇게 되어 있습니다. 나의 PC 인 localhost (NET 3.5 and NET 4.7) 환경에서는 오류가 발생되지 않습니다.

 

"X509Certificate2(certificateBytes, password)" 

 var certificate = new X509Certificate2(certificateBytes, password);

 string xml = "....";
 XmlDocument xmlDocument = new XmlDocument();
 xmlDocument.PreserveWhitespace = true;
 xmlDocument.LoadXml(xml);

 SignedXml signedXml = new SignedXml(xmlDocument);
 signedXml.SigningKey = certificate.PrivateKey;

 

하지만 AWS 의 EC2 나 애저 환경에서는 아래와 같이 공급자가 명시되지 않았다고 오류가 발생하면서 exception 이 됩니다. 

 

Invalid provider type specified” CryptographicException when trying to load private key of certificate

 

이를 해결 하기 위해서는 X509Certificate2(certificateBytes, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); 를 추가하면 이를 오류 없이 해결이 가능합니다. 

 

 var certificate = new X509Certificate2(certificateBytes, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
                                                                   //^ Here
 string xml = "....";
 XmlDocument xmlDocument = new XmlDocument();
 xmlDocument.PreserveWhitespace = true;
 xmlDocument.LoadXml(xml);

 SignedXml signedXml = new SignedXml(xmlDocument);
 signedXml.SigningKey = certificate.GetRSAPrivateKey();

 

 

stackoverflow.com/a/55346856

 

"Invalid provider type specified" CryptographicException when trying to load private key of certificate

I'm trying to read the private key of a certificate which has been shared with me by a third-party service provider, so I can use it to encrypt some XML before sending it to them over the wire. I'm...

stackoverflow.com