Java - 使用私钥将数据发送到外部Web服务的数字签名

问题描述 投票:0回答:2

我需要从运行在Tomcat 6上的Java应用程序连接到外部Web服务。我在我的服务器上购买并安装了我的域的SSL证书。现在,我需要连接到外部服务并使用我的证书私钥对使用SHA-256哈希和128位盐长度的服务数据进行数字签名。如何使用私钥创建此签名?我可以选择盐的任何值吗?他们是否可以使用SSL证书中的公钥解密它?

我可以使用Bouncy Castle库吗?任何关于该主题的代码或教程将不胜感激。

java tomcat ssl digital-signature
2个回答
0
投票

JCA文档提供了使用Signature的示例(在Generating and Verifying a Signature Using Generated Keys下。你使用SHA256withRSA而不是SHA1withDSA,因为它得到了SunRsaSignProvider的支持(假设它是一个RSA密钥)。你不应该为此需要BouncyCastle。

如果你想使用BouncyCastle,你需要在这些方面做一些事情(我还没有尝试过这个特定的代码):

AsymmetricKeyParameter keyParam = PrivateKeyFactory.createKey(...);
// You might need to cast to private key to RSAPrivateKey 
// and get its attributes manually here.

SHA256Digest digest = new SHA256Digest();
RSADigestSigner signer = new RSADigestSigner(digest);
signer.init(true, keyParam);
signer.update(... data to sign, start, length, ...);
byte[] signature = signer.generatedSignature();

(如果您是在webapp中执行此操作,则还需要Web应用程序才能访问此私钥,如果Web应用程序遭到入侵,这可能会带来安全风险。可能值得考虑使用不同的密钥/证书,即使是自签名的,如果远程方愿意接受它。)


0
投票

我强烈建议使用Web服务堆栈:例如。使用Apache CXF的WS-Security客户端的方法 - qazxsw poi

一个更好的参考:http://cxf.apache.org/docs/ws-security.html

© www.soinside.com 2019 - 2024. All rights reserved.