Java - 使用私钥的数字签名将数据发送到外部 Web 服务
我需要从 Tomcat 6 上运行的 Java 应用程序连接到外部 Web 服务。我为我的域购买了 SSL 证书并将其安装在我的服务器上。现在,我需要连接到外部服务,并使用我的证书私钥,使用 SHA-256 哈希和 128 位盐长度对发送到该服务的任何数据进行数字签名。如何使用私钥来创建此签名?我可以为盐选择任何值吗?他们能够使用我的 SSL 证书中的公钥对其进行解密吗?
我可以使用 Bouncy Castle 库来实现此目的吗?有关该主题的任何代码或教程将不胜感激。
I need to connect to an external webservice from my Java application running on Tomcat 6. I have an SSL certificate for my domain purchased and installed on my server. Now I need to connect to an external service and use my certificate private key to digitally sign any data going to the service using SHA-256 hash and 128-bit salt length. How can I use the private key to create this signature? Can I pick any values for the salt? Will they be able to decrypt it using my public key from the SSL certificate?
Can I use the Bouncy Castle library for this? Any code or tutorials on the subject would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JCA 文档提供了使用
Signature
的示例(在 使用生成的密钥生成和验证签名。您可以使用SHA256withRSA
而不是SHA1withDSA
,因为它受 SunRsaSignProvider(假设它是 RSA 密钥)。您不需要为此使用 BouncyCastle。如果您想使用 BouncyCastle,您需要按照这些进行一些操作行(我还没有尝试过这个特定的代码):(
如果您在 Web 应用程序中执行此操作,您还需要 Web 应用程序能够访问此私钥,这可能存在安全风险如果网络应用程序受到损害,如果远程方愿意接受,可能值得考虑使用不同的密钥/证书,甚至是自签名的。)
The JCA documentation provides an example for using
Signature
(under Generating and Verifying a Signature Using Generated Keys. You'd useSHA256withRSA
instead ofSHA1withDSA
, as it's supported by the SunRsaSignProvider (assuming it's an RSA key). You shouldn't need BouncyCastle for this.If you want to use BouncyCastle, you'd need to do something along these lines (I've haven't tried this particular code):
(If you're doing this from within a webapp, you'd also need the webapp to be able to gain access to this private key, which may be a security risk should the webapp be compromised. It might be worth considering using a different key/certificate, even self-signed, if the remote party is willing to accept it.)
我强烈建议为此使用网络服务堆栈:
例如。使用 Apache CXF 的 WS-Security 客户端方法 - http://cxf.apache.org/ docs/ws-security.html
另一个很好的参考:http://www.jroller.com/gmazza/entry/cxf_x509_profile
I would highly recommend using a webservice stack for this:
For eg. an approach for WS-Security client using Apache CXF - http://cxf.apache.org/docs/ws-security.html
One more good reference: http://www.jroller.com/gmazza/entry/cxf_x509_profile