现在在研究数字签名技术,基于.net平台,对生成的*.cer文件,可以读出公钥的xml形式,可是无法获的私钥的xml形式?有那位高手知道,指教小弟一下?
解决方案 »
- 模拟post 重定向,应该怎么处理?
- 关于局部变量DataSet对象是否有必要调用Dispose方法?
- 提示错误,未将对象引用设置到对象实例
- 我的listview有2列,三行,
- 请教一个简单的数组问题
- C# treeView如何让 右键新增节点处于可编辑状态???
- 为什么我装完Team Foundation Server,客户端没有版本控制(源代码管理)!!!!
- 关于Replace如何使用正则表达式替换?
- 急急急!!!怎么用WORD的document对象的open方法打开一个Word文档
- combox绑定到dataview,如何实现关联?
- 求C#Windos程序源码?就10分了,都给你
- Form 中嵌入表格
using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;
public class CertInfo
{
//Reads a file.
internal static byte[] ReadFile(string fileName)
{
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);
f.Close();
return data;
}
//Main method begins here.
public 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("c:\\textWHX.cer");
x509.Import(rawData); //Print to console information contained in the certificate
Console.WriteLine("{0}证书名称Subject: {1}{0}", Environment.NewLine, x509.Subject);
Console.WriteLine("{0}证书颁发机构的名称Issuer: {1}{0}", Environment.NewLine, x509.Issuer);
Console.WriteLine("{0}版本Version: {1}{0}", Environment.NewLine, x509.Version);
Console.WriteLine("{0}证书生效的本地时间中的日期Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore);
Console.WriteLine("{0}该日期后证书不再有效Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter);
Console.WriteLine("{0}证书的指纹Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint);
Console.WriteLine("{0}证书的序列号Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber);
Console.WriteLine("{0}公钥标识符友好名称Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName);
Console.WriteLine("{0}公钥的ASN。1码Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(true));
Console.WriteLine("{0}证书的原始数据的长度Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length);
Console.WriteLine("{0}以文本格式显示X509证书Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(true)); Console.WriteLine("{0}公钥以XML形式表式Certificate to XML String: {1}{0}", Environment.NewLine, x509.PublicKey.Key.ToXmlString(false));
//Console.WriteLine("{0}私钥以XML形式表式Certificate to XML String: {1}{0}", Environment.NewLine, x509.PrivateKey.ToXmlString(false ));
上面这注释的这一句老是出错?
//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.");
}
}}
标准可交换cer证书文件是不包含私钥的私人信息文件pfx里面才会包含有私钥
不然那还叫什么私钥。