在 Java 中创建 SSL 连接

发布于 2024-11-16 13:20:58 字数 1374 浏览 5 评论 0 原文

我环顾四周,没有看到任何问题能够完全回答我想要的问题,但如果这是重复的,请向我指出该问题,我将继续前进。

现在我正在尝试编写一个 Java 服务器,它将从 SSLServerSocket 接收数据,现在只需将其打印出来。我最终希望这些数据来自 Android,但现在它在开始侦听数据之前会抛出 SSLException

代码:

System.setProperty("javax.net.ssl.keyStore","C:\\ProgramFiles\\jre6\\bin\\server.jks");
System.setProperty("javax.net.ssl.keyStorePassword","password");
SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
ServerSocket ss = factory.createServerSocket(6543);
Socket s = ss.accept();

之后还有更多代码来处理它,但它被挂起并在那里抛出异常,所以我不确定发布它是否会有所帮助,但如果有的话,只需对其进行评论。

我按照 Ubuntu 上的 openssl 教程创建了证书,并将其转移并使用以下命令创建了我的密钥库:

keytool -import -file "C:\Documents and Settings\matt\Desktop\server.crt" -keystore server.jks

我可以轻松地承认我不完全理解其中很大一部分是如何工作的,因此我们将不胜感激。另外,我想我将把它排除在这个问题的范围之外,因为我觉得这本身就是一个很大的问题,但我还想了解一些关于如何连接客户端的见解(如果可能的话)。很抱歉给您带来了麻烦,并提前感谢您提供的所有帮助。

编辑:

我遵循的教程在这里: http://www.akadia.com/services/ssh_test_certificate.html

再次感谢!

编辑:

抛出的异常是:

javax.net.ssl.SSLException: No available certificate or key corresponds to the SSL cipher suites which are enabled

我尝试谷歌搜索异常,大多数内容都是描述如何创建密钥库的教程(我印象中我已经拥有了)。我将继续筛选这些搜索结果。

谢谢!

I looked around and did not see any questions that fully answered what I wanted, though if this is a duplicate, point me to the question and I will be on my way.

Right now I am trying to write a Java server that will receive data from an SSLServerSocket and for now, just print it out. I would eventually like to have this data come from an Android, but right now it throws an SSLException before it even starts listening for data.

code:

System.setProperty("javax.net.ssl.keyStore","C:\\ProgramFiles\\jre6\\bin\\server.jks");
System.setProperty("javax.net.ssl.keyStorePassword","password");
SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
ServerSocket ss = factory.createServerSocket(6543);
Socket s = ss.accept();

There is more code after that to process it, but it gets hung up and throws the exception there, so I'm not sure posting it will help any, but if it will, just comment for it.

I created the certificate following a tutorial for openssl on Ubuntu and transferred it over and created my keystore using:

keytool -import -file "C:\Documents and Settings\matt\Desktop\server.crt" -keystore server.jks

I can easily admit that I don't fully understand how a large portion of this works, so any help would be appreciated. Also, I suppose I am going to leave it outside the scope of this question becauseI feel like this is a pretty big question on its own, butI would also like some insight as to how to connect the client if possible. Sorry for all the trouble and thanks ahead of time for all the help.

EDIT:

the tutorial I followed is here:
http://www.akadia.com/services/ssh_test_certificate.html

Thanks again!

EDIT:

The Exception being throw is:

javax.net.ssl.SSLException: No available certificate or key corresponds to the SSL cipher suites which are enabled

I tried to Google the exception and most everything was a tutorial describing how to create a keystore (which I am under the impression that I already have). I will continue to sift through these search results.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

樱&纷飞 2024-11-23 13:20:58

当您创建这样的密钥库时,您只需将证书放入密钥库中:

keytool -import -file "server.crt" -keystore server.jks

您需要的是拥有私钥+证书。

如果您已经拥有证书颁发机构颁发的证书,则可以从其他地方导入它们;如果用途有限,则可以创建自签名证书。

如果您使用 OpenSSL 创建的证书是自签名的(或者来自供您自己使用的迷你 CA,例如使用 CA.pl),则可能不值得进行转换。您不妨直接使用 keytool 生成自签名证书。请参阅“生成密钥对”示例 在官方 keytool 文档中:

keytool -genkeypair -dname "cn=Mark Jones, ou=JavaSoft, o=Sun, c=US"
     -别名业务 -keypass kpi135 -keystore /working/mykeystore
     -storepass ab987c -有效期 180

确保使用 cn=your.fqdn.host.name(如果仅用于本地测试,则使用 cn=localhost)。 (我认为 Java 7 提供的 keytool 也支持主题备用名称,这会更好。)

如果您已经有一个私钥 + 证书,您想以 PKCS#12 格式重复使用(通常是.p12文件),您可以使用这个问题

如果您使用 OpenSSL 生成的内容是 PEM 格式,则将它们与 OpenSSL 捆绑在 PKCS#12 文件中,然后按上述方式导入可能会更容易。这可以通过以下方式完成:

openssl pkcs12 -export -in cert.pem -inkey key.pem -out creds.p12

When you create a keystore like this, you only put a certificate in your keystore:

keytool -import -file "server.crt" -keystore server.jks

What you need is to have a private key + a certificate.

Either you import them from somewhere else if you already have a certificate issued by a Certification Authority, or you can create a self-signed certificate if it's for limited use.

If the certificate you've created with OpenSSL is self-signed (or from a mini CA for your own use, e.g. with CA.pl), it's probably not worth the trouble of doing the conversion. You might as well generate a self-signed certificate directly with keytool. See the "Generating Your Key Pair" example in the official keytool documentation:

keytool -genkeypair -dname "cn=Mark Jones, ou=JavaSoft, o=Sun, c=US"
     -alias business -keypass kpi135 -keystore /working/mykeystore
     -storepass ab987c -validity 180

Make sure you use cn=your.fqdn.host.name (or cn=localhost if it's for local tests only). (I think keytool provided with Java 7 also has support for subject alternative names, which would be better.)

If you already have a private key + certificate you want to re-use in PKCS#12 format (usually .p12 file), you can import it using the method described in this question.

If what you've produced with OpenSSL is in PEM format, it might be easier to bundle them in a PKCS#12 file with OpenSSL and then import them as above. This can be done with this:

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