/// <summary> /// 从Cert证书文件中读取 /// </summary> /// <param name="path"></param> /// <returns></returns> public static RsaCertificate ReadFromCert(string path) { //var x509cert = new X509Certificate2(path); var rsa = new RsaCertificate(); rsa._certificate2 = new X509Certificate2(path); rsa._provider = new RSACryptoServiceProvider(); RSA cng = (RSA)rsa._certificate2.PublicKey.Key; rsa._provider.ImportParameters(cng.ExportParameters(false)); return(rsa); }
/// <summary> /// 从pfx证书文件中读取 /// </summary> /// <param name="path"></param> /// <param name="password"></param> /// <returns></returns> public static RsaCertificate ReadFromPfx(string path, string password) { var rsa = new RsaCertificate(); rsa._provider = new RSACryptoServiceProvider(); rsa._certificate2 = new X509Certificate2(path, password, X509KeyStorageFlags.Exportable); if (rsa._certificate2.HasPrivateKey) { RSA cng = (RSA)rsa._certificate2.PrivateKey; var parameter = cng.ExportParameters(true); rsa._provider.ImportParameters(parameter); } else { //rsa._provider = (RSACryptoServiceProvider)rsa._certificate2.PublicKey.Key; RSA cng = (RSA)rsa._certificate2.PublicKey.Key; var parameter = cng.ExportParameters(false); rsa._provider.ImportParameters(parameter); } return(rsa); }