随笔-20  评论-10  文章-0  trackbacks-0

在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;
            }

        }


    }
posted on 2007-04-27 10:39 冰封王座(.net)博客 阅读(3985) 评论(3) 编辑 收藏

评论:
#1楼 2007-08-03 11:06 | 李海[未注册用户]
非常不错!加分100
 回复 引用   
#2楼 2008-05-15 18:51 | QQ签名[未注册用户]
非常不错!
 回复 引用   
#3楼 2008-08-05 16:11 | 阿新      
我用你的命名好像没有产生签名要的私钥
 回复 引用 查看   
昵称:冰封王座(.net)博客
园龄:5年8个月
粉丝:0
关注:0
<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

搜索

 
 

常用链接

随笔档案

文章分类

最新评论

阅读排行榜

评论排行榜

推荐排行榜