1. java客戶端怎麼訪問帶有pfx格式證書的https網站(伺服器)呢,
使用HttpURLConnection訪問https地址。
以下是導入JKS證書的方式,可以參考。
Stringkeystorefile="file";
Stringkeystorepw="password";
Stringkeypw="password";
KeyStorekeystore=KeyStore.getInstance("JKS");
keystore.load(newFileInputStream(keystorefile),keystorepw.toCharArray());
=KeyManagerFactory.getInstance("SunX509");
keymanagerfactory.init(keystore,keypw.toCharArray());
KeyManagerakeymanager[]=keymanagerfactory.getKeyManagers();
=TrustManagerFactory.getInstance("SunX509");
trustmanagerfactory.init(keystore);
TrustManageratrustmanager[]=trustmanagerfactory.getTrustManagers();
sslcontext=SSLContext.getInstance("TLS");
sslcontext.init(akeymanager,atrustmanager,null);
sslSocketFactory=sslcontext.getSocketFactory();
Stringurl="asdfdf";
URLtestURL=newURL(url);
=(HttpURLConnection)testURL.openConnection();
if(){
HttpsURLConnectionconn=(HttpsURLConnection)urlConnection;
conn.setSSLSocketFactory(sslSocketFactory);
}
2. 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;
}
}
3. pfx證書文件能直接使用嗎
不能直接使用pfx證書的,
需要去java網站上下一個keytools工具,導入pfx證書後才能使用。cer和pfx是成對的,你做了個cer的話必然會同時生成一個pfx,不曉得你的cer是怎麼做的。一般來說是這樣的,你先用一些工具,比如剛才說的java的keytools,或者IIS生成一個密鑰請求crs,然後把這個crs發送給密鑰發放的CA,你可以自己搭一個CA或者去買一個,CA會再發給你一個確認後的證書,你再使用這個確認後的cer來結束證書申請過程,最終得到一個私鑰和一個密鑰
4. pfx格式文件如何打開如題 謝謝了
pfx可能是證書的備份文件,這時可雙擊相關文件即可導入證書,如果要求輸入密碼,則輸入當初設定的密碼即可。
5. 數字證書文件格式cer和pfx的區別 / 藍訊
Windows IIS下的數字證書格式一般為。pfx
Java tomcat 下的數字證書格式一般為.jks或.store
Apache和nginx一般是.pem
證書請求文件一般是.csr
證書公鑰文件一般是.cer或.crt
證書私鑰文件一般是.key
可以到易維信【evtrust】申請數字簽名證書或者代碼簽名證書