Java如何读取PFX密钥文件?

Java如何读取PFX密钥文件?,第1张

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=0n<b.lengthn++) {

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-1i>-1i--){

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/p12文件中提取RSA密钥长度及其他相关信息

在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

.cer/.crt是用于存放证书,它晌带铅是2进制形式存放的,不含私钥。

.pem跟crt/cer的区别是它以Ascii来表示。

pfx/p12用于存放个人证书/私钥,他通常包含保护密码宴好,2进制方式

p10是证书请求

p7r是CA对证书请求的回复,只用于导入

p7b以树状展示证书链(certificate

chain),同时也支持单个证书,不含私钥。

其中,我介绍如何从p12/pfx文件中提取密钥对及其长度:

1,首先,读取pfx/p12文件(需要提供保护密码)

2,通过别名(Alias,注意,所有证书中的信息项都是通过Alias来提取的)提取你想要分析的证书链

3,再将其转换为一个以X509证书结构体

4,提取里面的项,如果那你的证书项放在第一位(单一证书),直接读取

x509Certs[0](见下面的代码)这个X509Certificate对象

5,X509Certificate对象有很多方法,tain198127网友希望读取RSA密钥(公私钥)及其长度 ,那真是太行樱Easy了,

X509Certificate keyPairCert =

x509Certs[0]

int iKeySize =

X509CertUtil.getCertificateKeyLength(keyPairCert)

System.out.println("证书密钥算法="+keyPairCert.getPublicKey().getAlgorithm())

System.out.println("证书密钥长度="+iKeySize)

提取了他所需要的信息。

这个我刚弄完,趁着热乎

1.生成证书 C:\Program Files\Microsoft SDKs\Windows\嫌竖v7.0A\bin下 Makecert.exe

makecert -r -pe -$ individual -n "CN=MccnPurvewSecurityClient" -sky exchange -sr currentuser -ss my MccnPurvewSecurityClient.cer

makecert -r -pe -$ individual -n "CN=MccnPurvewSecurityServer" -sky exchange -sr currentuser -ss my MccnPurvewSecurityServer.cer

2.查看证书 C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin下 certmgr.exe

安全证书序号

Client:‎16 e8 b0 e7 4d f0 4c 89 45 bf 8b 05 76 e6 f4 bc

Server:‎e4 c8 60 7b 7b 14 1d b5 44 c3 3c 01 70 b5 7a 16

System.ArgumentException: 没有能够进行密钥交换的私钥,或者进程可能没有访问私钥的权限。

winhttpcertcfg -g -c LOCAL_MACHINE\My -s MccnPurvewSecurityServer -a "NETWORKSERVICE"

解决进入MMC.EXE添加证书-导出证书MccnPurvewSecurityClient.PFX,再导入进本地计算机

findprivatekey.exe My LocalMachine -t "17 fc 38 26 80 92 9d 14 b9 8b 91 27 4e 19 e5 12 0c 8e 01 29" -a

证书密码 gold

证书生成后,导出pfx证书是要有密钥的。没有密钥生成不了pfx文件

解决WCF部署到IIS出现“证书必须具有能够进行密钥交换的私钥,该进程必须具有访问私钥的权滚者闹限”2012-03-20 14:02解决WCF部署到IIS出现“证书必须具有能够进行密钥交换的私钥,该进程必须具有访问私钥的权限”

访问WCF服务时,出现异常详细信息: System.Security.Cryptography.CryptographicException: 密钥集不存在。ArgumentException: 证书“CN=MyServerCert”必须具有能够进行密钥交换的私钥。该进程必须具有访问私钥的权大罩限。这个问题是因为 WCF 所使用的帐户(NETWORK SERVICE/ASPNET)对证书私钥文件的读访问权限造成的。

造成上面的错误主要是Network Service(Server)用户没有访问证书权限,要解决该错误,

WCF远程调试http://blog.csdn.net/mathieuxiao/article/details/7490577

wcf从另一方收到未进行安全处理或安全处理不正确的错误:服务器时间和客户端时间相差》5分钟。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12132624.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存