您的位置:首页 > 编程语言 > C#

在C#中使用 makecert 创建自签名的证书

2007-05-24 13:38 561 查看
在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;


}


}




}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: