「java引用pfx」java引用变量

博主:adminadmin 2023-01-14 01:12:09 394

本篇文章给大家谈谈java引用pfx,以及java引用变量对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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;nb.length;n++) {

stmp=(java.lang.Integer.toHexString(b[n] 0XFF));

if (stmp.length()==1) hs=hs+"0"+stmp;

else hs=hs+stmp;

//if (nb.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(temp0xff).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;

}

}

java客户端怎么访问带有pfx格式证书的https网站(服务器)呢,

使用HttpURLConnection 访问https地址。

以下是导入JKS证书的方式,可以参考。

String keystorefile = "file";

String keystorepw = "password";

String keypw = "password";

    

KeyStore keystore = KeyStore.getInstance("JKS");

keystore.load( new FileInputStream(keystorefile), keystorepw.toCharArray());

KeyManagerFactory keymanagerfactory = KeyManagerFactory.getInstance("SunX509");

keymanagerfactory.init(keystore, keypw.toCharArray());

KeyManager akeymanager[] = keymanagerfactory.getKeyManagers();

TrustManagerFactory trustmanagerfactory = TrustManagerFactory.getInstance("SunX509");

trustmanagerfactory.init(keystore);

TrustManager atrustmanager[] = trustmanagerfactory.getTrustManagers();

sslcontext = SSLContext.getInstance("TLS");

sslcontext.init(akeymanager, atrustmanager, null);

sslSocketFactory = sslcontext.getSocketFactory();

String url = "asdfdf";

URL testURL = new URL(url);

HttpURLConnection urlConnection = (HttpURLConnection) testURL.openConnection();

if (urlConnection instanceof HttpsURLConnection) {

    HttpsURLConnection conn = (HttpsURLConnection) urlConnection;

    conn.setSSLSocketFactory(sslSocketFactory);

}

如何在Java处理PFX格式证书

开发人员通常需要将PFX文件转换为某些不同的格式,如PEM或JKS,以便可以为使用SSL通信的独立Java客户端或WebLogic Server使用

在Security编程中,有几种典型的密码交换信息文件格式:

DER-encoded certificate: .cer, .crt

PEM-encoded message: .pem

PKCS#12 Personal Information Exchange: .pfx, .p12

PKCS#10 Certification Request: .p10

PKCS#7 cert request response: .p7r

PKCS#7 binary message: .p7b

关于java引用pfx和java引用变量的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。