本文整理汇总了C#中X509Certificate2.Import方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2.Import方法的具体用法?C# X509Certificate2.Import怎么用?C# X509Certificate2.Import使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509Certificate2
的用法示例。
在下文中一共展示了X509Certificate2.Import方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCert
public static bool TestCert(CertificateInfo ci)
{
bool bRes = true;
try {
X509Certificate2 cert;
if (ci.Password != null)
cert = new X509Certificate2(ci.FileName, ci.Password);
else
cert = new X509Certificate2(ci.FileName);
if (!ci.Matches(cert)) bRes = false;
// Console.WriteLine("ToString: " + cert.ToString());
// Console.WriteLine("ToString(true): " + cert.ToString(true));
X509Certificate2 certImp = new X509Certificate2();
if (ci.Password != null)
certImp.Import(ci.FileName, ci.Password, X509KeyStorageFlags.DefaultKeySet);
else
certImp.Import(ci.FileName);
if (!ci.Matches(certImp)) bRes = false;
}
catch(Exception e)
{
bRes = false;
Console.WriteLine("Exception is caught:" + Environment.NewLine + e.ToString());
}
return bRes;
}
示例2: TestCert
public static bool TestCert(CertificateInfo ci)
{
bool bRes = true;
try {
// read a certificate file to a byte array
FileStream fs = new FileStream(ci.FileName, FileMode.Open, FileAccess.Read);
byte[] certBytes = new byte[(int)fs.Length];
fs.Read(certBytes, 0, certBytes.Length);
X509Certificate2 cert;
if (ci.Password != null)
cert = new X509Certificate2(certBytes, ci.Password);
else
cert = new X509Certificate2(certBytes);
if (!ci.Matches(cert)) bRes = false;
// Console.WriteLine("ToString: " + cert.ToString());
// Console.WriteLine("ToString(true): " + cert.ToString(true));
X509Certificate2 certImp = new X509Certificate2();
if (ci.Password != null)
certImp.Import(certBytes, ci.Password, X509KeyStorageFlags.DefaultKeySet);
else
certImp.Import(certBytes);
if (!ci.Matches(certImp)) bRes = false;
}
catch(Exception e)
{
bRes = false;
Console.WriteLine("Exception is caught:" + Environment.NewLine + e.ToString());
}
return bRes;
}
示例3: Main
//Main method begins here.
static void Main(string[] args)
{
//Test for correct number of arguments.
if (args.Length < 1)
{
Console.WriteLine("Usage: CertInfo <filename>");
return;
}
try
{
X509Certificate2 x509 = new X509Certificate2();
//Create X509Certificate2 object from .cer file.
byte[] rawData = ReadFile(args[0]);
x509.Import(rawData);
//Print to console information contained in the certificate.
Console.WriteLine(x509.Thumbprint);
//Add the certificate to a X509Store.
X509Store store = new X509Store();
store.Open(OpenFlags.MaxAllowed);
store.Add(x509);
store.Close();
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("Error: The directory specified could not be found.");
}
catch (IOException)
{
Console.WriteLine("Error: A file in the directory could not be accessed.");
}
catch (NullReferenceException)
{
Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.");
}
}
示例4: verify
/**
* 根据传入的参数做验签
* @param srcMsg 签名用源串
* @param signMsg 通联响应中给出的签名串
* @param certPath 证书路径
* @param isAbsolatePath 是否绝对路径,如certpath参数值为证书绝对路径,则填true,否则填false
*/
private bool verify(String srcMsg, String signMsg, String certPath, Boolean isAbsolatePath)
{
//base64解码签名串
Byte[] signMsgBytes = decode(signMsg);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//读取x509证书
X509Certificate2 x509 = new X509Certificate2();
if (isAbsolatePath)
{
//设置证书的绝对路径
//x509.Import(@"D:\cert\TLCert.cer");
x509.Import(certPath);
}
else
{
//或者设置证书的相对路径
//x509.Import(HttpContext.Current.Server.MapPath("../cert/TLCert.cer"));
x509.Import(HttpContext.Current.Server.MapPath(certPath));
}
//x509.PublicKey.Key.ToXmlString();
//灌注到rsa
rsa.FromXmlString(x509.PublicKey.Key.ToXmlString(false));
bool verifyResult = rsa.VerifyData(System.Text.Encoding.UTF8.GetBytes(srcMsg), "SHA1", signMsgBytes);
return verifyResult;
}
示例5: LoadCertificate
public void LoadCertificate(byte[] certificate)
{
cert = new X509Certificate2();
cert.Import(certificate);
}
示例6: SignatureDeformatter
//---------------------------------------以下代码请勿更动------------------------------------------------------------
public bool SignatureDeformatter(string srcMsgData, string signMsgData,String certPath, Boolean isAbsolatePath)
{
//base64解码签名串
Byte[] signMsgBytes = decode(signMsg);
//源串UTF8格式化
byte[] srcMsgBytes = System.Text.Encoding.UTF8.GetBytes(srcMsgData);
//读取x509证书
X509Certificate2 x509 = new X509Certificate2();
if (isAbsolatePath)
{
//设置证书的绝对路径
//x509.Import(@"c:\Projects\MyWebSite\cert\TLCert.cer");
x509.Import(certPath);
}
else
{
//或者设置证书的相对路径
//x509.Import(HttpContext.Current.Server.MapPath("../cert/TLCert.cer"));
x509.Import(HttpContext.Current.Server.MapPath(certPath));
}
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(x509.PublicKey.Key.ToXmlString(true));
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密摘要算法为SHA1
RSADeformatter.SetHashAlgorithm("SHA1");
return RSADeformatter.VerifySignature(srcMsgBytes, signMsgBytes);
}