PKI技术(public key infrastructure)里面,cer文件和pfx文件是非经常见的。通常cer文件中面保存着公钥以及用户的一些信息,pfx里面则含有私钥和公钥。

用makecert.exe能够创建公钥证书和私钥证书,详细看

http://msdn.microsoft.com/zh-cn/library/bfsktky3(v=vs.110).aspx

http://blog.csdn.net/hacode/article/details/4240238

这里使用程序的方法来创建。參考了http://www.cnblogs.com/luminji/archive/2010/10/28/1863179.html

以下的代码封装了一个类,能够在store里面创建一个认证,而且导出到cer,pfx,然后从store,cer,pfx读取信息

public sealed class DataCertificate
{
#region 生成证书
/// <summary>
/// 依据指定的证书名和makecert全路径生成证书(包括公钥和私钥,并保存在MY存储区)
/// </summary>
/// <param name="subjectName"></param>
/// <param name="makecertPath"></param>
/// <returns></returns>
public static bool CreateCertWithPrivateKey(string subjectName, string makecertPath)
{
subjectName = "CN=" + subjectName;
string param = " -pe -ss my -n \"" + subjectName + "\" ";
try
{
Process p = Process.Start(makecertPath, param);
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
return false;
}
return true;
}
#endregion #region 文件导入导出
/// <summary>
/// 从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书,
/// 并导出为pfx文件,同一时候为其指定一个密码
/// 并将证书从个人区删除(假设isDelFromstor为true)
/// </summary>
/// <param name="subjectName">证书主题,不包括CN=</param>
/// <param name="pfxFileName">pfx文件名称</param>
/// <param name="password">pfx文件密码</param>
/// <param name="isDelFromStore">是否从存储区删除</param>
/// <returns></returns>
public static bool ExportToPfxFile(string subjectName, string pfxFileName,
string password, bool isDelFromStore)
{
subjectName = "CN=" + subjectName;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
foreach (X509Certificate2 x509 in storecollection)
{
if (x509.Subject == subjectName)
{
Debug.Print(string.Format("certificate name: {0}", x509.Subject)); byte[] pfxByte = x509.Export(X509ContentType.Pfx, password);
using (FileStream fileStream = new FileStream(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;
}
/// <summary>
/// 从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书,
/// 并导出为CER文件(即,仅仅含公钥的)
/// </summary>
/// <param name="subjectName"></param>
/// <param name="cerFileName"></param>
/// <returns></returns>
public static bool ExportToCerFile(string subjectName, string cerFileName)
{
subjectName = "CN=" + subjectName;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
foreach (X509Certificate2 x509 in storecollection)
{
if (x509.Subject == subjectName)
{
Debug.Print(string.Format("certificate name: {0}", x509.Subject));
//byte[] pfxByte = x509.Export(X509ContentType.Pfx, password);
byte[] cerByte = x509.Export(X509ContentType.Cert);
using (FileStream fileStream = new FileStream(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();
return false;
}
}
fileStream.Close();
}
}
}
store.Close();
store = null;
storecollection = null;
return true;
}
#endregion #region 从证书中获取信息
/// <summary>
/// 依据私钥证书得到证书实体,得到实体后能够依据其公钥和私钥进行加解密
/// 加解密函数使用DEncrypt的RSACryption类
/// </summary>
/// <param name="pfxFileName"></param>
/// <param name="password"></param>
/// <returns></returns>
public static X509Certificate2 GetCertificateFromPfxFile(string pfxFileName,
string password)
{
try
{
return new X509Certificate2(pfxFileName, password, X509KeyStorageFlags.Exportable);
}
catch (Exception e)
{
return null;
}
}
/// <summary>
/// 到存储区获取证书
/// </summary>
/// <param name="subjectName"></param>
/// <returns></returns>
public static X509Certificate2 GetCertificateFromStore(string subjectName)
{
subjectName = "CN=" + subjectName;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
foreach (X509Certificate2 x509 in storecollection)
{
if (x509.Subject == subjectName)
{
return x509;
}
}
store.Close();
store = null;
storecollection = null;
return null;
}
/// <summary>
/// 依据公钥证书,返回证书实体
/// </summary>
/// <param name="cerPath"></param>
public static X509Certificate2 GetCertFromCerFile(string cerPath)
{
try
{
return new X509Certificate2(cerPath);
}
catch (Exception e)
{
return null;
}
}
#endregion
}

两个RSA加解密辅助函数:

        static string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(xmlPrivateKey);
byte[] rgb = Convert.FromBase64String(m_strDecryptString);
byte[] bytes = provider.Decrypt(rgb, false);
return new UnicodeEncoding().GetString(bytes);
}
/// <summary>
/// RSA加密
/// </summary>
/// <param name="xmlPublicKey"></param>
/// <param name="m_strEncryptString"></param>
/// <returns></returns>
static string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(xmlPublicKey);
byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString);
return Convert.ToBase64String(provider.Encrypt(bytes, false));
}

使用样例,以下的代码做了几个事情

1. 在个人store里面创建了一个认证, 从认证里面读取信息得到一个X509Certificate2的对象,这个对象内部包括公钥和私钥,然后做了次rsa加解密測试。

2. 从store里面导出一个cer文件,由于cer文件并没有私钥,仅仅有公钥。測试代码就是用公钥加密然后用前面得到的私钥解密。

3. 导出一个pfx文件,pfx包含公钥和私钥,能够自己加解密。

这是个非常easy的样例,可是对于理解cer文件和pfx文件已经公钥私钥应该有帮助。

            // 在personal(个人)里面创建一个foo的证书
DataCertificate.CreateCertWithPrivateKey("foo", "C:\\Program Files (x86)\\Windows Kits\\8.1\\bin\\x64\\makecert.exe"); // 获取证书
X509Certificate2 c1 = DataCertificate.GetCertificateFromStore("foo"); string keyPublic = c1.PublicKey.Key.ToXmlString(false); // 公钥
string keyPrivate = c1.PrivateKey.ToXmlString(true); // 私钥 string cypher = RSAEncrypt(keyPublic, "程序猿"); // 加密
string plain = RSADecrypt(keyPrivate, cypher); // 解密 Debug.Assert(plain == "程序猿"); // 生成一个cert文件
DataCertificate.ExportToCerFile("foo", "d:\\mycert\\foo.cer"); X509Certificate2 c2 = DataCertificate.GetCertFromCerFile("d:\\mycert\\foo.cer"); string keyPublic2 = c2.PublicKey.Key.ToXmlString(false); bool b = keyPublic2 == keyPublic;
string cypher2 = RSAEncrypt(keyPublic2, "程序猿2"); // 加密
string plain2 = RSADecrypt(keyPrivate, cypher2); // 解密, cer里面并没有私钥,所以这里使用前面得到的私钥来解密 Debug.Assert(plain2 == "程序猿2"); // 生成一个pfx, 而且从store里面删除
DataCertificate.ExportToPfxFile("foo", "d:\\mycert\\foo.pfx", "111", true); X509Certificate2 c3 = DataCertificate.GetCertificateFromPfxFile("d:\\mycert\\foo.pfx", "111"); string keyPublic3 = c3.PublicKey.Key.ToXmlString(false); // 公钥
string keyPrivate3 = c3.PrivateKey.ToXmlString(true); // 私钥 string cypher3 = RSAEncrypt(keyPublic3, "程序猿3"); // 加密
string plain3 = RSADecrypt(keyPrivate3, cypher3); // 解密 Debug.Assert(plain3 == "程序猿3");

附:完整代码

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text; namespace ConsoleApplication1
{
public sealed class DataCertificate
{
#region 生成证书
/// <summary>
/// 依据指定的证书名和makecert全路径生成证书(包括公钥和私钥,并保存在MY存储区)
/// </summary>
/// <param name="subjectName"></param>
/// <param name="makecertPath"></param>
/// <returns></returns>
public static bool CreateCertWithPrivateKey(string subjectName, string makecertPath)
{
subjectName = "CN=" + subjectName;
string param = " -pe -ss my -n \"" + subjectName + "\" ";
try
{
Process p = Process.Start(makecertPath, param);
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
return false;
}
return true;
}
#endregion #region 文件导入导出
/// <summary>
/// 从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书,
/// 并导出为pfx文件,同一时候为其指定一个密码
/// 并将证书从个人区删除(假设isDelFromstor为true)
/// </summary>
/// <param name="subjectName">证书主题,不包括CN=</param>
/// <param name="pfxFileName">pfx文件名称</param>
/// <param name="password">pfx文件密码</param>
/// <param name="isDelFromStore">是否从存储区删除</param>
/// <returns></returns>
public static bool ExportToPfxFile(string subjectName, string pfxFileName,
string password, bool isDelFromStore)
{
subjectName = "CN=" + subjectName;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
foreach (X509Certificate2 x509 in storecollection)
{
if (x509.Subject == subjectName)
{
Debug.Print(string.Format("certificate name: {0}", x509.Subject)); byte[] pfxByte = x509.Export(X509ContentType.Pfx, password);
using (FileStream fileStream = new FileStream(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;
}
/// <summary>
/// 从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书,
/// 并导出为CER文件(即,仅仅含公钥的)
/// </summary>
/// <param name="subjectName"></param>
/// <param name="cerFileName"></param>
/// <returns></returns>
public static bool ExportToCerFile(string subjectName, string cerFileName)
{
subjectName = "CN=" + subjectName;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
foreach (X509Certificate2 x509 in storecollection)
{
if (x509.Subject == subjectName)
{
Debug.Print(string.Format("certificate name: {0}", x509.Subject));
//byte[] pfxByte = x509.Export(X509ContentType.Pfx, password);
byte[] cerByte = x509.Export(X509ContentType.Cert);
using (FileStream fileStream = new FileStream(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();
return false;
}
}
fileStream.Close();
}
}
}
store.Close();
store = null;
storecollection = null;
return true;
}
#endregion #region 从证书中获取信息
/// <summary>
/// 依据私钥证书得到证书实体,得到实体后能够依据其公钥和私钥进行加解密
/// 加解密函数使用DEncrypt的RSACryption类
/// </summary>
/// <param name="pfxFileName"></param>
/// <param name="password"></param>
/// <returns></returns>
public static X509Certificate2 GetCertificateFromPfxFile(string pfxFileName,
string password)
{
try
{
return new X509Certificate2(pfxFileName, password, X509KeyStorageFlags.Exportable);
}
catch (Exception e)
{
return null;
}
}
/// <summary>
/// 到存储区获取证书
/// </summary>
/// <param name="subjectName"></param>
/// <returns></returns>
public static X509Certificate2 GetCertificateFromStore(string subjectName)
{
subjectName = "CN=" + subjectName;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
foreach (X509Certificate2 x509 in storecollection)
{
if (x509.Subject == subjectName)
{
return x509;
}
}
store.Close();
store = null;
storecollection = null;
return null;
}
/// <summary>
/// 依据公钥证书,返回证书实体
/// </summary>
/// <param name="cerPath"></param>
public static X509Certificate2 GetCertFromCerFile(string cerPath)
{
try
{
return new X509Certificate2(cerPath);
}
catch (Exception e)
{
return null;
}
}
#endregion
} class Program
{
static string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(xmlPrivateKey);
byte[] rgb = Convert.FromBase64String(m_strDecryptString);
byte[] bytes = provider.Decrypt(rgb, false);
return new UnicodeEncoding().GetString(bytes);
}
/// <summary>
/// RSA加密
/// </summary>
/// <param name="xmlPublicKey"></param>
/// <param name="m_strEncryptString"></param>
/// <returns></returns>
static string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(xmlPublicKey);
byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString);
return Convert.ToBase64String(provider.Encrypt(bytes, false));
} static void Main(string[] args)
{
// 在personal(个人)里面创建一个foo的证书
DataCertificate.CreateCertWithPrivateKey("foo", "C:\\Program Files (x86)\\Windows Kits\\8.1\\bin\\x64\\makecert.exe"); // 获取证书
X509Certificate2 c1 = DataCertificate.GetCertificateFromStore("foo"); string keyPublic = c1.PublicKey.Key.ToXmlString(false); // 公钥
string keyPrivate = c1.PrivateKey.ToXmlString(true); // 私钥 string cypher = RSAEncrypt(keyPublic, "程序猿"); // 加密
string plain = RSADecrypt(keyPrivate, cypher); // 解密 Debug.Assert(plain == "程序猿"); // 生成一个cert文件
DataCertificate.ExportToCerFile("foo", "d:\\mycert\\foo.cer"); X509Certificate2 c2 = DataCertificate.GetCertFromCerFile("d:\\mycert\\foo.cer"); string keyPublic2 = c2.PublicKey.Key.ToXmlString(false); bool b = keyPublic2 == keyPublic;
string cypher2 = RSAEncrypt(keyPublic2, "程序猿2"); // 加密
string plain2 = RSADecrypt(keyPrivate, cypher2); // 解密, cer里面并没有私钥,所以这里使用前面得到的私钥来解密 Debug.Assert(plain2 == "程序猿2"); // 生成一个pfx, 而且从store里面删除
DataCertificate.ExportToPfxFile("foo", "d:\\mycert\\foo.pfx", "111", true); X509Certificate2 c3 = DataCertificate.GetCertificateFromPfxFile("d:\\mycert\\foo.pfx", "111"); string keyPublic3 = c3.PublicKey.Key.ToXmlString(false); // 公钥
string keyPrivate3 = c3.PrivateKey.ToXmlString(true); // 私钥 string cypher3 = RSAEncrypt(keyPublic3, "程序猿3"); // 加密
string plain3 = RSADecrypt(keyPrivate3, cypher3); // 解密 Debug.Assert(plain3 == "程序猿3");
}
}
}



cer, pfx 创建,而且读取公钥/密钥,加解密 (C#程序实现)的更多相关文章

  1. cer, pfx 创建,并且读取公钥/密钥,加解密 (C#程序实现)

    PKI技术(public key infrastructure)里面,cer文件和pfx文件是很常见的.通常cer文件里面保存着公钥以及用户的一些信息,pfx里面则含有私钥和公钥. 用makecert ...

  2. Java中RSA非对称密钥加解密使用示例

    一.简介: RSA加密算法是最常用的非对称加密算法,CFCA在证书服务中离不了它.RSA是第一个比较完善的公开密钥算法,它既能用于加密,也能用于数字签名.这个算法经受住了多年深入的密码分析,虽然密码分 ...

  3. DH密钥加解密

    一.概述 1.与对称加密算法的主要差别在于,加密和解密的密钥不相同,一个公开(公钥),一个保密(私钥).主要解决了对称加密算法密钥分配管理的问题,提高了算法安全性. 2.非对称加密算法的加密.解密的效 ...

  4. nodejs加解密

    加密分类 可逆加密和不可逆加密 不可逆加密: 加密后不可解密,只能通过碰撞密文以极小的概率解密; 可逆加密: 加密后可以解密;包括对称加密与非对称加密; 对称加密双方采用共同密钥; 非对称加密: 这种 ...

  5. AES的256位密钥加解密报 java.security.InvalidKeyException: Illegal key size or default parameters 异常的处理及处理工具

    一.出现的现象为了数据代码在传输过程中的安全,很多时候我们都会将要传输的数据进行加密,然后等对方拿到后再解密使用.我们在使用AES加解密的时候,在遇到128位密钥加解密的时候,没有进行什么特殊处理:然 ...

  6. Java实现RSA密钥对并在加解密、加签验签中应用的实例

    一.项目结构 二.代码具体实现 1.密钥对生成的两种方式:一种生成公钥私文件,一种生成公钥私串 KeyPairGenUtil.java package com.wangjinxiang.genkey. ...

  7. 加解密、PKI与CA基础

    介绍 这门知识如果以前尝过的各位想必都知道:枯燥无比!因此在文中我会尽量讲的生动些,举一些例子,并试图以一个完整的例子来贯穿整个讲述过程.今年又恰逢莎翁逝世400周年,一方面也为了纪念这位伟大的作家. ...

  8. 介绍一个很好用的Rsa加解密的.Net库 Kalix.ApiCrypto

    Rsa非对称加密技术 这个就不说了,大家上网搜索都知道,公钥加密,私钥解密.当然大家也可以自己实现.这里就懒了,就去找一个现成的库,Nuget上搜索,GitHub上搜索,发现.Net的加解密库,下载量 ...

  9. CTF中编码与加解密总结

    CTF中那些脑洞大开的编码和加密 转自:https://www.cnblogs.com/mq0036/p/6544055.html 0x00 前言 正文开始之前先闲扯几句吧,玩CTF的小伙伴也许会遇到 ...

随机推荐

  1. 程序缩小到托盘后系统就无法关机(解决方案)——处理WM_QUERYENDSESSION消息,并把它标识为处理过了

    程序缩小到托盘后系统就无法关机(解决方案)                       老帅    程序最小化到托盘后,会出现系统无法关闭的问题,常见于WinXP系统中,这里提供一个解决方案!一.解决 ...

  2. dokcer 运行和进入容器

    <pre name="code" class="html">docker:/root# docker run -itd --name zjtest8 ...

  3. Vim -&gt; 边确认边查找替换

    进行简单的全局查找替换的时候,能够使用ex的例如以下命令 :%s/which/what/g 它的使用方法比較简单 % 表示每一行(everyline) s 替换(substitue) which 查找 ...

  4. make工具与Makefile文件

    make工具与Makefile文件 阅读目录 1. make工具 2. Makefile文件 3. Makefile的简单示例 4. 伪目标 5. Makefile 自动化变量 6. 编译生成多个可执 ...

  5. java过滤html标签函数

    public static String Html2Text(String inputString) {              String htmlStr = inputString; //含h ...

  6. 智能生活 “视”不可挡——首届TCL杯HTML5智能电视开发大赛等你来挑战

    http://www.csdn.net/article/2014-06-04/2820063-TCL-Smart-TV-Innovation-Competation

  7. C++ 中获取 可变形參函数中的參数

    #include <iostream> #include <stdarg.h> using namespace std; int ArgFunc(const char * st ...

  8. 约瑟夫问题--list模拟循环链表

    约瑟夫问题 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 n个人想玩残酷的死亡游戏,游戏规则例如以下: n个人进行编号,分别 ...

  9. android学习---SeekBar和RatingBar

    SeekBar 拖动条:拖动条和滚动栏类似,当是拖动条能够拖动滑块改变进度 RatingBar 星级评分条:星级评分条与拖动条相似 SeekBar特有的xml属性 android:thumb    指 ...

  10. BC 2015在百度之星程序设计大赛 - 预赛(1)(KPI-树董事长)

    KPI Accepts: 517 Submissions: 2185 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...