使用BouncyCastle进行简单的HTTPS查询 [英] Using BouncyCastle for a simple HTTPS query

查看:792
本文介绍了使用BouncyCastle进行简单的HTTPS查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用于执行简单HTTPS请求的代码的简化版本:

Here's a simplified version of the code I'm using to perform simple HTTPS requests:

// Assume the variables host, file and postData have valid String values

final URL url = new URL("https", host, file);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-length", String.valueOf(postData.length()));

final DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(postData);
output.close();

final InputStream input = new DataInputStream(connection.getInputStream());

for (int c = input.read(); c != -1; c = input.read()) {
  System.out.print((char) c);
}

System.out.println();

input.close();

这曾经很好地连接到我们的服务器(如果我使用http作为协议仍然可以)直到最近才进行了一些安全升级。

This used to work well for connecting to our server (and still does if I use http as the protocol) until recently when some security upgrades were done.

现在它给了我无法生成DH密钥对和Prime大小必须是64的倍数,并且只能范围从512到1024(含)此问题中提到的错误:

Now it's giving me the "Could not generate DH keypair" and "Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)" errors mentioned in this question:

Java:为什么SSL握手会给'无法生成DH密钥对'例外?

原来这是Java中的已知错误,建议使用BouncyCastle的JCE实现。

Turns out it's a known bug in Java and the recommendation is to use BouncyCastle's JCE implementation.

我的问题是......我该如何使用BouncyCastle这样的事情?或者还有更多选择吗?

My question is... how do I use BouncyCastle for something like this? Or are there more alternatives?

免责声明:我对密码学以及使HTTPS查询成为可能的基础技术知之甚少。相反,我更喜欢专注于我的应用程序逻辑,并让各种库处理低级问题。

Disclaimer: I have very little knowledge of and interest in cryptology and the underlying technology that makes HTTPS queries possible. Rather, I'd prefer to focus on my application logic and let various libraries take care of low level issues.

我检查了BouncyCastle网站和文档并用Google搜索查找关于JCE等的更多信息,但总而言之,这是非常压倒性的,我无法找到任何简单的代码示例来执行上述代码。

I checked out the BouncyCastle website and documentation and Googled to find out more about JCE etc, but all in all it's quite overwhelming and I haven't been able to find any simple code examples for doing something like the above code.

推荐答案

我发现了一个不同的解决方案(尽管通过srkavin发布的链接)甚至不需要使用BouncyCastle:

I found a different solution (although via the link that srkavin posted) that doesn't even require using BouncyCastle:

下载后 Java密码术扩展(JCE)无限强度管辖权政策文件来自 Java下载站点并且替换了我的JRE中的文件,上面的代码开始工作而没有任何修改。

After downloading "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" from the Java download site and replacing the files in my JRE, the above code started working without any modifications.

这篇关于使用BouncyCastle进行简单的HTTPS查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆