在文件中读取私钥和公钥

在做项目中,碰到是给到定的私钥和公钥,我们得把公钥和私钥读出来才可以使用,私钥一般还会有密码!

	/**
	 * 获取公钥
	 */
	public PublicKey readPublicKey(String cerFileName) {

		CertificateFactory cf;
		FileInputStream in;
		Certificate c;
		PublicKey pk = null;
		try {
			cf = CertificateFactory.getInstance("X.509");
			in = new FileInputStream(cerFileName);
			c = cf.generateCertificate(in);
			pk = c.getPublicKey();

		} catch (CertificateException e) {
			log.error("获取公钥错误!!!");
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			log.error("获取公钥错误!!!");
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return pk;
	}

	/**
	 * 获取私钥
	 *
	 * @return
	 */
	public PrivateKey readPrivateKey(String privateKeyFileName,
			String privatepass) {

		KeyStore ks;
		PrivateKey prk = null;
		try {
			ks = KeyStore.getInstance("pkcs12");
			FileInputStream fis = new FileInputStream(privateKeyFileName);
			ks.load(fis, null);
			String pwd = privatepass;
			String alias = ks.aliases().nextElement().toString();
			prk = (PrivateKey) ks.getKey(alias, pwd.toCharArray());
		} catch (Exception e) {
			log.error("获取私钥错误!!!");
			log.error(e.getMessage());
			e.printStackTrace();
		}

		return prk;
	}

  

时间: 2024-08-30 14:19:37

在文件中读取私钥和公钥的相关文章

JMeter接口测试——参数化(从文件中读取参数)

从文件中读取,三个步骤 1.读取文件 2.取文件内容里面的参数,给它一个名字 3.使用值 从文件读取的话,需要在线程组里面添加一个CSV Data Set Config,它就是做前面两步的操作的 参数说明: Filename: 文件的完整路径 Variable Names(comma-delimited):储存参数的变量名 Delimiter(use '\t' for tab): 分隔多个参数的分隔符 Recycle on EOF ?:文件读取完后是否继续读取 Stop thread on EO

文件_ _android从资源文件中读取文件流并显示的方法

======== 1   android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private void doRaw(){ InputStream is = this.getResources().openRawResource(R.raw.ziliao); try{ doRead(is); }catch(IOException e){ e.printStackTrace(); } } pri

从plist文件中读取数据

//从plist文件中读取数据- (void)readDataFromPlist{    //1.先获取文件路径    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Book" ofType:@"plist"];    //2.根据路径初始化字典对象    self.dic = [NSMutableDictionary dictionaryWithContentsOfFile:fileP

Mean and Standard Deviation-从文件中读取数据计算其平均数和标准差

Meanand Standard Deviation-从文件中读取数据计算其平均数和标准差 //Meanand Standard Deviation-从文件中读取数据计算其平均数和标准差 #include<iostream> #include<fstream> #include<cstdlib> #include<cmath>   int main() {     usingnamespace std;     ifstream fin;     ofstr

从Matlab .fig文件中读取数据,并重新绘图

Matlab提供了强大的函数集合,可以从.fig文件中读取图中的数据,并重新绘制图形.如果原始数据丢失,我们可以从.fig文件中恢复原始数据,并基于原始数据做进一步的处理. 以下是一个从两个不同文件中读取原始数据,并重新绘制图形的例子. h1 = openfig('1.fig','reuse'); % open figure D1=get(gca,'Children'); %get the handle of the line object XData1=get(D1,'XData'); %ge

在文件中读取、存储Json格式的字符串

public class Weather { static readonly string FilePath = System.Environment.CurrentDirectory + @"\Area.txt"; public static Models.Area GetCurrentArea() { var file = new FileInfo(FilePath); Models.Area result; if (!file.Exists) { //文件不存在就返回一个默认值,

java如何在文件中读取一个字符串并创建以这个字符为名字的类的对象

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">我们一般用properties或者XML文件作为资源存储的文件,现在主要介绍一下对properties的操作</span> 1.我们在src文件下新建一个名称为config的包 2.在config下新建一个file,把它命名为demo.properties 3. 把下面的几段

VB.NET 从XML文件中读取内容到JSON

一.关于本文 本文承接了上一篇博客的内容.在上篇博客中,通过函数WriteJsonToXml将一个JSON格式的文件写入了一个对应的XML文件中.本文中则给出了函数RecoverJsonFromXml的代码,可以将XML恢复成JSON格式的文本. 运行本文中代码的环境与上篇博客相同. 待读取的XML文档内容如下: <?xml version="1.0" encoding="gb2312"?> <!--这个XML文档中存储了一个JSON格式的信息--

【Python】从文件中读取数据

从文件中读取数据 1.1 读取整个文件 要读取文件,需要一个包含几行文本的文件(文件PI_DESC.txt与file_reader.py在同一目录下) PI_DESC.txt 3.1415926535 8979323846 2643383279 5028841971 file_reader.py with open("PI_DESC.txt") as file_object: contents = file_object.read() print(contents) 我们可以看出,读取