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