2016-03-07 79 views

回答

2

也許這樣的事情?

public Certificate CreateFromCertFile() 
{ 
    string cerFile = @"c:\cer.txt"; 
    string keyFile = @"c:\key.txt"; 

    var cert = new X509Certificate2(cerFile); 
    cert.PrivateKey = CreateRSAFromFile(keyFile); 

    return cert; 
} 

private RSACryptoServiceProvider CreateRSAFromFile(string filename) 
{ 
    byte[] pvk = null; 
    using (var fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) 
    { 
     pvk = new byte[fs.Length]; 
     fs.Read(pvk, 0, pvk.Length); 
    } 

    var rsa = new RSACryptoServiceProvider(); 
    rsa.ImportCspBlob(pvk); 

    return rsa; 
}