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

发布于 2024-11-06 10:53:02 字数 309 浏览 0 评论 0原文

我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-11-13 10:53:02

JCA 文档提供了使用 Signature 的示例(在 使用生成的密钥生成和验证签名。您可以使用 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();

如果您在 Web 应用程序中执行此操作,您还需要 Web 应用程序能够访问此私钥,这可能存在安全风险如果网络应用程序受到损害,如果远程方愿意接受,可能值得考虑使用不同的密钥/证书,甚至是自签名的。)

The JCA documentation provides an example for using Signature (under Generating and Verifying a Signature Using Generated Keys. You'd use SHA256withRSA instead of SHA1withDSA, 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):

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();

(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.)

我也只是我 2024-11-13 10:53:02

我强烈建议为此使用网络服务堆栈:
例如。使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文