Current location - Quotes Website - Signature design - How does Java read PFX key file?
How does Java read PFX key file?
Package com. Kinhill;

Import java.io. *;

Import java.util. *;

Import java.security. *;

Import java.security.cert.certificate;

Public class ReadPFX {

public ReadPFX (){

}

//Convert to hexadecimal string

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]& amp; 0x ff));

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

else hs = hs+stmp;

//if(n & lt; b . length- 1)hs = hs+":";

}

Returns hs.touppercase ();

}

Public static byte [] String byte (integer) {

int temp = number

Byte[]b = new byte [4];

for(int I = b . length- 1; I>- 1; I-) {

B[i] = new integer (temp & amp0xff). byte value(); //Save the highest bit in the lowest bit.

temp = temp & gt& gt8; //Move 8 bits to the right

}

Return to b;

}

private key GetPvkformPfx(String strPfx,String strPassword){

Try {

KeyStore ks = KeyStore . getinstance(" pkcs 12 ");

file inputstream fis = new file inputstream(strPfx);

//If the keystore password is empty (""), then we must set it.

//to null, otherwise no! ! !

char[]n password = null;

if((str password = = null)| | str password . trim()。 Equal to ("") {

nPassword = null

}

other

{

nPassword = str password . tochararray();

}

ks.load(fis,nPassword);

fis . close();

system . out . println(" keystore type = "+ks . gettype());

//Now we cycle through all aliases, and we need aliases to get the key.

//This value seems to be the Friendly Name field in.

//details tab & lt; -Certificate window & lt- View & lt- Certificate

//Button & lt; -Content tab <- Internet options < -Tools menu

//In MS IE 6.

enumeration enumas = ks . aliases();

String keyAlias = null

If (enumas.hasMoreElements())// We only read one certificate.

{

key alias =(String)enumas . nextelement();

system . out . println(" alias =["+keyAlias+"]");

}

//Now once we know the alias, we can get the key.

system . out . println(" is key entry = "+ks . iskey entry(keyAlias));

private key prikey =(private key)ks . getkey(keyAlias,nPassword);

certificate cert = ks . get certificate(keyAlias);

public key pubkey = cert . get public key();

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 to prikey

}

Capture (exception e)

{

e . printstacktrace();

}

Returns null

}

}