java x509certificate2_使用X509Certificate2类操作证书文件

public classCertHelper

{string pfxPath = @"E:\开发辅助项目\cert\taisuyuanqing.pfx";string cerPath = @"E:\开发辅助项目\cert\taisuyuanqing.cer";#region *从文件中读取公钥和私钥

#region +从文件中读取公钥

///

///从文件中读取公钥///

///

public stringGetPlulicKey()

{try{

X509Certificate2 x509= newX509Certificate2(cerPath);return x509.PublicKey.Key.ToXmlString(false);

}catch(Exception ex)

{return string.Empty;

}

}#endregion

#region +从私钥文件中读取私钥

///

///从私钥文件中读取私钥///

public voidGetPrivateKey()

{try{

X509Certificate2 x509= new X509Certificate2(pfxPath, "123456", X509KeyStorageFlags.Exportable);var publicKey = x509.PublicKey.Key.ToXmlString(false);var privateKey = x509.PrivateKey.ToXmlString(true);var str = "逍遥帝君 15210253 xiaoyaodijun";var result =RSAHelper.RSAEncrypt(publicKey, str);var content =RSAHelper.RSADecrypt(privateKey, result);

}catch(Exception)

{throw;

}

}#endregion

#endregion

#region + 从证书库中获取证书

///

///从证书库中获取证书///

/// 证书名字

///

public X509Certificate2 GetCertificateFromStore(stringsubjectName)

{try{

subjectName= "CN=" +subjectName;

X509Store store= newX509Store(StoreName.My, StoreLocation.CurrentUser);

store.Open(OpenFlags.ReadWrite);

X509Certificate2Collection storecollection=(X509Certificate2Collection)store.Certificates;foreach (X509Certificate2 x509 instorecollection)

{if (x509.Subject ==subjectName)

{returnx509;

}

}

store.Close();

store= null;

storecollection= null;return null;

}catch(Exception)

{throw;

}

}#endregion

#region *导出证书文件

#region +从证书库中导出私钥文件

///

///从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书,///并导出为pfx文件,同时为其指定一个密码///并将证书从个人区删除(如果isDelFromstor为true)///

/// 证书主题,不包含CN=

/// pfx文件名

/// pfx文件密码

/// 是否从存储区删除

///

public static bool ExportToPfxFile(string subjectName, stringpfxFileName,string password, boolisDelFromStore)

{

subjectName= "CN=" +subjectName;

X509Store store= newX509Store(StoreName.My, StoreLocation.CurrentUser);

store.Open(OpenFlags.ReadWrite);

X509Certificate2Collection storecollection=(X509Certificate2Collection)store.Certificates;foreach (X509Certificate2 x509 instorecollection)

{if (x509.Subject ==subjectName)

{byte[] pfxByte =x509.Export(X509ContentType.Pfx, password);using (FileStream fileStream = newFileStream(pfxFileName, FileMode.Create))

{//Write the data to the file, byte by byte.

for (int i = 0; i < pfxByte.Length; i++)

fileStream.WriteByte(pfxByte[i]);//Set the stream position to the beginning of the file.

fileStream.Seek(0, SeekOrigin.Begin);//Read and verify the data.

for (int i = 0; i < fileStream.Length; i++)

{if (pfxByte[i] !=fileStream.ReadByte())

{

fileStream.Close();return false;

}

}

fileStream.Close();

}if (isDelFromStore == true)

store.Remove(x509);

}

}

store.Close();

store= null;

storecollection= null;return true;

}#endregion

#region + 从证书库中导出公钥文件

///

///从证书库中导出公钥文件///

/// 证书名字

/// 存放公钥的文件路径

public void ExportToCerFile(string subjectName, stringcerFileName)

{try{

subjectName= "CN=" +subjectName;

X509Store store= newX509Store(StoreName.My, StoreLocation.LocalMachine);

store.Open(OpenFlags.ReadWrite);

X509Certificate2Collection storecollection=(X509Certificate2Collection)store.Certificates;foreach (X509Certificate2 x509 instorecollection)

{if (x509.Subject != subjectName) continue;byte[] cerByte =x509.Export(X509ContentType.Cert);using (FileStream fileStream = newFileStream(cerFileName, FileMode.Create))

{//Write the data to the file, byte by byte.

for (int i = 0; i < cerByte.Length; i++)

fileStream.WriteByte(cerByte[i]);//Set the stream position to the beginning of the file.

fileStream.Seek(0, SeekOrigin.Begin);//Read and verify the data.

for (int i = 0; i < fileStream.Length; i++)

{if (cerByte[i] !=fileStream.ReadByte())

{

fileStream.Close();

}

}

fileStream.Close();

}

}

}catch(Exception)

{throw;

}

}#endregion

#endregion

#region + 从证书库中获取私钥和公钥

///

///从证书库中获取私钥和公钥///

///

///

///

public string GetPrivateKeyFromStore(string subjectName, stringpassword)

{try{

subjectName= "CN=" +subjectName;//CurrentUser=当前用户 LocalMachine=本地计算机

X509Store store = newX509Store(StoreName.My, StoreLocation.CurrentUser);

store.Open(OpenFlags.ReadWrite);

X509Certificate2Collection storecollection=(X509Certificate2Collection)store.Certificates;foreach (X509Certificate2 x509 instorecollection)

{if (x509.Subject != subjectName) continue;

x509.Export(X509ContentType.Pfx, password);var privateKey = x509.PrivateKey.ToXmlString(true);var publicKey = x509.PublicKey.Key.ToXmlString(false);var str = "逍遥帝君 15210253 xiaoyaodijun";var result =RSAHelper.RSAEncrypt(publicKey, str);var content =RSAHelper.RSADecrypt(privateKey, result);

}return "";

}catch(Exception)

{return "";

}

}#endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值