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
java x509certificate2_使用X509Certificate2类操作证书文件相关推荐
- java探针之修改类字节码文件
java探针利用了javaAgent + ASM字节码注入工具实现了动态修改类文件的功能.像skywalking和arthas都使用到了这个技术. 具体原理为: jdk1.5以后引入了javaAgen ...
- java引用微信支付的p12证书文件
最近对接微信支付的退款功能,其中涉及到引用证书文件. 1. 绝对路径: // windows:public static String PATH1 = "E:\\project_ceshi\ ...
- java文件头工具类_判断文件类型工具类
package com.huawei.fileExerise; import java.io.FileInputStream; import java.io.IOException; import j ...
- java工具keytool生成p12数字证书文件
Keytool是用于管理**和证书的工具,位于%JAVA_HOME%/bin目录. 使用JDK的keytool工具 1.keytool在jdk的bin目录下 查找jdk目录参考https://blog ...
- Java基础篇--工具类操作之计算并输出21世纪的闰年,计算程序的执行时间
计算并输出21世纪的闰年,计算程序的执行时间. 实现方法不唯一,此方法仅作初学者(自己)参考..... 所用类API介绍 所用方法API介绍 GregorianCalendar类中直接给出了判断是否为 ...
- 使用java中的String类操作复杂的字符串
今天自己出了一道题差点把自己难住-- 题目是这样的: 已知有三条查询的sql语句,小明不小心写一块了,你帮他拆分开.sql语句为:"select * from books where bid ...
- php ini文件操作类,操作.ini文件的好PHP类
您可以尝试从此开始,它会读取ini文件,并在写入时保留设置,您必须扩展它以支持添加新条目: class ini { protected $lines; public function read($fi ...
- android平台使用java动态生成公私钥,并导出证书文件
不依赖keytool工具,指令生成证书库,而是java代码生成,且导出到证书文件中.直接上代码: 证书工具类: package com.daobo.security.utilsimport com.d ...
- 【Java】(二十四)File类(文件操作,文件夹操作,FileFilter接口);Properties属性集合
继续上一章,学习JavaIO框架 [Java](二十三)IO框架:流的概念与分类,字节流(对象流[序列化.反序列化]),编码方式,字符流(打印流,转换流) 上一节的学习(字节流,字符流)都是对文件内容 ...
最新文章
- 6kyu Build a pile of Cubes
- Json.Net系列教程 3.Json.Net序列化和反序列化设置
- Maven如何将别人的项目导入到自己的eclipse并使用
- 问题:虚拟环境名字相同遇到bug,please specify a different sdk name?
- 微信小程地图片未加载成功的情况 Failed to load local image resource
- Bootstrap 导航/滑动门(nav)
- 分享 25 个有用的 JS 单行代码
- win10 uwp 使用 Border 布局
- 企业信息与网络通信安全 团队成员简历-叶俊
- Mybatis如果存在该条数据则修改,否则新增
- 阿里云DNS专家,手把手教你定位域名解析不生效
- C# 使用Salt+Hash来为密码加密
- PowerMockito 简介
- 理解Spring Security中permitAll()和anonymous()的区别
- iOS中下划线字体的实现
- 说到修图这件事,你还真是比不上AI
- 模型效果差?我建议你掌握这些机器学习模型的超参数优化方法
- Bloxorz I POJ - 3322 bfs
- Node框架thinkjs开发后台API
- 扫拖一体洗地机哪个品牌好、家用洗地机品牌介绍