在C#中使用 makecert 创建自签名的证书 class Program { static void Main(string[] args) { X509Certificate2 cert = CreateCertificate(); //cert.ToString(); Console.Write(cert.ToString()); Console.Read(); } public static X509Certificate2 CreateCertificate() { // makecert -r -pe -n "CN=TestUser" -ss my -sr currentuser // -sky exchange ./TestUser.cer //const string MakeCert = "C://Program Files//Microsoft Visual Studio 8//Common7//Tools//Bin//makecert.exe"; const string MakeCert = "C://Program Files//Microsoft Visual Studio 8//SDK//v2.0//Bin//makecert.exe"; //string fileName = Path.ChangeExtension(Path.GetTempFileName(), "cer"); string fileName = Path.ChangeExtension("d://", "cer"); string userName = Guid.NewGuid().ToString(); string arguments = string.Format("-r -pe -n /"CN={0}/" -ss my -sr currentuser -sky exchange /"{1}/"", userName, fileName); Process p = Process.Start(MakeCert, arguments); p.WaitForExit(); byte[] certBytes = ReadFile(fileName); X509Certificate2 cert = new X509Certificate2(certBytes); return cert; } internal static byte[] ReadFile(string fileName) { using (FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { int size = (int)f.Length; byte[] data = new byte[size]; size = f.Read(data, 0, size); return data; } } }