Attach .pfx certificate to exe file

2019-04-16 22:05发布

I have to deploy a software to n clients that will install a certificate and use it. I don't want to deploy two files (.pfx and exe) just one (.exe that will contain the .pfx).

What i'm doing now is import the certificate from a location.

X509Certificate2^ x509 = gcnew X509Certificate2;
x509->Import( "C:\\Tmp\\certficate.pfx" );

Is it possible ?

2条回答
贪生不怕死
2楼-- · 2019-04-16 22:32

You could always embed the certificate data as a resource.

One warning though: if someone gets the executable, they can pull out the PFX file pretty easily.

Are you able to securely distribute the executable?

Here are some rough steps, distilled from: http://www.spikezilla-software.com/blog/?p=24

  • Add the PFX to your project. Then click once on the file, and in the Properties window, set the Build Action to Embedded Resource
  • Read the embedded PFX file and import the certificate

This is C# but you should be able to translate to C++/CLI pretty easily:

var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyFile.pfx");
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
var cert = new X509Certificate2(bytes, "certPassword");
查看更多
Luminary・发光体
3楼-- · 2019-04-16 22:48

This worked for me once I embedded it.

byte[] cert = Properties.Resources.nameOfCertificate;
X509Certificate2 x509 = new X509Certificate2();
x509.Import(cert, "password", X509KeyStorageFlags.MachineKeySet);
查看更多
登录 后发表回答