获取Java中的证书和签名值

获取Java中的证书和签名值,java,certificate,digital-signature,Java,Certificate,Digital Signature,我有一个需要调用Web服务的需求。为了调用该webservice,我需要在SOAP请求中从Java提供证书链和签名值(webservice中的数据类型为String) 我已经使用wsimport从wsdl生成了源代码,并在Java程序中使用了它。我有一个数字签名证书文件(certificateFile.pfx)(密码对我可用) 最后的SOAP消息示例应该如下 <soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/

我有一个需要调用Web服务的需求。为了调用该webservice,我需要在SOAP请求中从Java提供证书链和签名值(webservice中的数据类型为String)

我已经使用wsimport从wsdl生成了源代码,并在Java程序中使用了它。我有一个数字签名证书文件(certificateFile.pfx)(密码对我可用)

最后的SOAP消息示例应该如下

<soapenv:Envelope
    xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:v = "http://www.something.com">
    <soapenv:Header/>
    <soapenv:Body>
        <v:Auth>
            <v:userID>xxxxxxxxxx</v:userID>
            <v:password>xxxxxxxxxx</v:password>
            <v:certChain>xxxxxxxxxx</v:certChain>
            <v:signature>xxxxxxxxxx</v:signature>
        </v:Auth>
    </soapenv:Body>
</soapenv:Envelope>
在这里,我没有得到什么设置证书链和签名

因为我有一个证书文件,所以我尝试了这个

XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
// Load the KeyStore and get the signing key and certificate.
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(new File(certificateName)),
                            certificatePassword.toCharArray());
KeyStore.PrivateKeyEntry keyEntry =
    (KeyStore.PrivateKeyEntry) ks.getEntry(
        certificateAlias,
        new KeyStore.PasswordProtection(
            certificatePassword.toCharArray()));
X509Certificate cert =
    (X509Certificate) keyEntry.getCertificate();
使用

authInfo.setSignature(new String(cert.getSignature()));
但都不管用
cert.getSignature()
返回二进制数据,而不是真正的字符串

我不知道该为这两个领域设置什么。如何使用我的证书文件在Java中获取
certificateChain
值和签名值?不幸的是,我无法联系网络服务提供商以获取更多信息。我想,如果我在字节数组中获得
证书chain
和签名,我可以创建
新字符串(byte[])
并在SOAP字段中使用该值。如何获取
证书的
字节[]
值和签名


我如何解决这个问题?有什么建议吗?

您可能需要将签名编码为编码。Base64是处理二进制数据(如签名)最广泛使用的方法,这些数据应通过基于文本的协议(如WDSL)传输。有很多很好的库可以对Base64进行编码和解码,例如或。

如果使用cert.getEncoded->这将返回证书值,而不是签名。您希望看到您的签名,因此需要使用

byte[] signature = cert.getSignature();
 System.out.println(Base64.encode(encoded));

Alexander,Base64类存在于许多库(包)中,就像您提到的apache commons、Guava以及com.ibm.xml.crypto.util(ibm JDK)、com.sun.org.apache.xml.internal.security.utils等等。如何确定要使用哪个库?当我使用多个库运行时,我看到的编码数据对于每个库都是不同的。我知道这取决于实现库对数据进行编码的方式。但哪种是通用编码方法?另外,如何从证书中获取证书链的Base64编码数据?
byte[] signature = cert.getSignature();
 System.out.println(Base64.encode(encoded));