导航:首页 > 源码编译 > 如何编译pfx文件

如何编译pfx文件

发布时间:2024-04-25 11:14:56

java如何读取PFX密钥文件

package com.Jinhill;
import java.io.*;
import java.util.*;
import java.security.*;
import java.security.cert.Certificate;

public class ReadPFX {
public ReadPFX (){
}
//转换成十六进制字符串
public static String Byte2String(byte[] b) {
String hs="";
String stmp="";

for (int n=0;n<b.length;n++) {
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
//if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}

public static byte[] StringToByte(int number) {
int temp = number;
byte[] b=new byte[4];
for (int i=b.length-1;i>-1;i--){
b[i] = new Integer(temp&0xff).byteValue();//将最高位保存在最低位
temp = temp >> 8; //向右移8位
}
return b;
}
private PrivateKey GetPvkformPfx(String strPfx, String strPassword){
try {
KeyStore ks = KeyStore.getInstance("PKCS12");
FileInputStream fis = new FileInputStream(strPfx);
// If the keystore password is empty(""), then we have to set
// to null, otherwise it won't work!!!
char[] nPassword = null;
if ((strPassword == null) || strPassword.trim().equals("")){
nPassword = null;
}
else
{
nPassword = strPassword.toCharArray();
}
ks.load(fis, nPassword);
fis.close();
System.out.println("keystore type=" + ks.getType());
// Now we loop all the aliases, we need the alias to get keys.
// It seems that this value is the "Friendly name" field in the
// detals tab <-- Certificate window <-- view <-- Certificate
// Button <-- Content tab <-- Internet Options <-- Tools menu
// In MS IE 6.
Enumeration enumas = ks.aliases();
String keyAlias = null;
if (enumas.hasMoreElements())// we are readin just one certificate.
{
keyAlias = (String)enumas.nextElement();
System.out.println("alias=[" + keyAlias + "]");
}
// Now once we know the alias, we could get the keys.
System.out.println("is key entry=" + ks.isKeyEntry(keyAlias));
PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword);
Certificate cert = ks.getCertificate(keyAlias);
PublicKey pubkey = cert.getPublicKey();
System.out.println("cert class = " + cert.getClass().getName());
System.out.println("cert = " + cert);
System.out.println("public key = " + pubkey);
System.out.println("private key = " + prikey);
return prikey;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}

阅读全文

与如何编译pfx文件相关的资料

热点内容
于丹pdf 浏览:713
反编译ref 浏览:546
鸿蒙智能文件夹怎么弄 浏览:544
grunt压缩html 浏览:787
macpdfword转换器 浏览:904
压缩面膜是什么材质 浏览:353
抖音发布作品怎么建文件夹 浏览:13
安卓如何更改谷歌地区 浏览:273
airpods为什么安卓连上声音很大 浏览:510
联想服务器按钮怎么开不了机 浏览:93
苹果xrapp程序库没有了怎么办 浏览:843
在上海程序员一个月多少工资 浏览:326
k歌解压图片 浏览:579
cad中清理多余图层的命令 浏览:989
华为nova8xe是安卓什么 浏览:58
汤姆猫服务器为什么免费 浏览:274
safenet加密狗怎么用 浏览:41
phpsql查询变量 浏览:104
地磅仪表盘加密方法 浏览:480
brew安装php环境 浏览:829