java ssl tcp连接正常,但接收到服务器的数据不正确
我的问题是,我在JavaISO客户机和测试服务器之间有ssl连接。连接正常,握手没有问题,但服务器接收到不正确的数据 正如我在日志中看到的,服务器在一次读取操作中只接收部分数据,而inputstream中也存在一些奇怪的数据——可能它与证书信息有关 请注意,如果没有ssl,此客户端和服务器工作时不会出现任何问题。同样,当我用java密钥工具创建jks标准证书simple时,一切都正常,问题不会发生 但当我使用x509时,无论我是通过从cmd生成jks来发送证书作为服务器jvm的参数,还是使用下面的代码在代码中生成jks,都会出现问题。连接和握手始终正常,但inputstream中的数据已中断 这是我如何使用boucycastle库获取证书的。客户端和服务器几乎相同java ssl tcp连接正常,但接收到服务器的数据不正确,java,ssl,tcp,x509,Java,Ssl,Tcp,X509,我的问题是,我在JavaISO客户机和测试服务器之间有ssl连接。连接正常,握手没有问题,但服务器接收到不正确的数据 正如我在日志中看到的,服务器在一次读取操作中只接收部分数据,而inputstream中也存在一些奇怪的数据——可能它与证书信息有关 请注意,如果没有ssl,此客户端和服务器工作时不会出现任何问题。同样,当我用java密钥工具创建jks标准证书simple时,一切都正常,问题不会发生 但当我使用x509时,无论我是通过从cmd生成jks来发送证书作为服务器jvm的参数,还是使用下面
private SSLServerSocketFactory handleCertificate() throws KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
Security.addProvider(new BouncyCastleProvider());
PEMReader pr = new PEMReader(new FileReader("p.pem"));
X509CertificateObject cert = (X509CertificateObject) pr.readObject();
PEMReader pr2 = new PEMReader(new FileReader("klient.cer"));
X509CertificateObject cert2 = (X509CertificateObject) pr2.readObject();
PEMReader kr = new PEMReader(new FileReader("001.key"),
new PasswordFinder() {
public char[] getPassword() {
return "password".toCharArray();
}
});
KeyStore trustKeys = KeyStore.getInstance("JKS");
trustKeys.load(null, "".toCharArray());
trustKeys.setCertificateEntry("1", cert);
KeyStore ksKeys = KeyStore.getInstance("JKS");
ksKeys.load(null, "password".toCharArray());
ksKeys.setCertificateEntry("1", cert2);
org.bouncycastle.jce.provider.JCERSAPrivateCrtKey key;
Object PK = kr.readObject();
if (PK instanceof KeyPair) {
key = (JCERSAPrivateCrtKey) ((KeyPair) PK).getPrivate();
} else {
key = (JCERSAPrivateCrtKey) PK;
}
ksKeys.setKeyEntry("1", key, "password".toCharArray(), new Certificate[] { cert2 });
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ksKeys, "password".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(trustKeys);
SSLContext sslContext = SSLContext.getInstance("SSLv3");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom());
SSLServerSocketFactory factory = sslContext.getServerSocketFactory();
return factory;
}
服务器代码:
InputStream is = new BufferedInputStream(socket.getInputStream());
while (!Thread.currentThread().isInterrupted()) {
int size = 0;
String mess_length = "";
byte[] lenbuf = new byte[4];
if (socket != null && socket.isConnected()) {
socket.getInputStream().read(lenbuf);
mess_length = new String(lenbuf);
log.debug("Lenth of received message: " + mess_length);
}
int responseSize = 0;
try {
responseSize = Integer.valueOf(mess_length);
size = responseSize;
} catch (Exception int_e) {
log.debug("Error of message lenth numbering: ", int_e);
}
byte[] buf = new byte[size];
if (socket.isConnected() && socket.getInputStream().read(buf) == size) {
log.debug("Message received.");
}
// -----------------------------------------------------------------------------------------------------------
OutputStream out = new BufferedOutputStream(socket.getOutputStream());
if ("echo".equals(EnvironmentProperties.getMode())) {
log.info("responsing in echo mode");
log.debug("Data to send from server: {} in connection id={}", new String(buf,
"UTF-8"), uuid);
out.write(buf);
}
out.flush();
bytesSet.clear();
log.info("responded");
从服务器登录:
17:37:26.166 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Mess
age received.
17:37:26.166 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onsing in echo mode
17:37:26.166 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Data
to send from server: in connection id=ee7f73ac-6be9-4e7b-876f-35d31845d69e
17:37:26.166 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onded
17:37:26.166 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Lent
h of received message: 200r
17:37:26.167 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - :
java.lang.NumberFormatException: For input string: "200r"
at java.lang.NumberFormatException.forInputString(Unknown Source) ~[na:1
.8.0_05]
at java.lang.Integer.parseInt(Unknown Source) ~[na:1.8.0_05]
at java.lang.Integer.valueOf(Unknown Source) ~[na:1.8.0_05]
at ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:87) [TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
at ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:1) [TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_05]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na
:1.8.0_05]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [n
a:1.8.0_05]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_05]
17:37:26.167 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Mess
age received.
17:37:26.167 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onsing in echo mode
17:37:26.167 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Data
to send from server: in connection id=ee7f73ac-6be9-4e7b-876f-35d31845d69e
17:37:26.168 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onded
17:37:26.168 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Lent
h of received message: 4♦А
17:37:26.168 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - ╬°шс
ър яЁхюсЁрчютрэш фышэ√ ёююс∙хэш :
java.lang.NumberFormatException: For input string: "4♦А "
at java.lang.NumberFormatException.forInputString(Unknown Source) ~[na:1
.8.0_05]
at java.lang.Integer.parseInt(Unknown Source) ~[na:1.8.0_05]
at java.lang.Integer.valueOf(Unknown Source) ~[na:1.8.0_05]
at ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:87) [TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
at ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:1) [TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_05]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na
:1.8.0_05]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [n
a:1.8.0_05]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_05]
17:37:26.168 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Mess
age received.
17:37:26.168 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onsing in echo mode
17:37:26.168 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Data
to send from server: in connection id=ee7f73ac-6be9-4e7b-876f-35d31845d69e
17:37:26.168 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onded
17:37:26.168 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Lent
h of received message: ┴А 2
17:37:26.169 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - ╬°шс
ър яЁхюсЁрчютрэш фышэ√ ёююс∙хэш :
java.lang.NumberFormatException: For input string: "┴А 2"
at java.lang.NumberFormatException.forInputString(Unknown Source) ~[na:1
.8.0_05]
at java.lang.Integer.parseInt(Unknown Source) ~[na:1.8.0_05]
at java.lang.Integer.valueOf(Unknown Source) ~[na:1.8.0_05]
at ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:87) [TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
at ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:1) [TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_05]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na
:1.8.0_05]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [n
a:1.8.0_05]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_05]
17:37:26.169 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Mess
age received.
17:37:26.169 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onsing in echo mode
17:37:26.169 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Data
to send from server: in connection id=ee7f73ac-6be9-4e7b-876f-35d31845d69e
17:37:26.169 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onded
17:37:26.169 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Lent
h of received message: 1194
17:37:26.169 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - ╧Ёхю
сЁрчютрээр фышээр ёююс∙хэш : 1194
17:37:26.169 [pool-1-thread-13] INFO r.b.t.app.server.ConnectionCallable - resp
onsing in echo mode
17:37:26.169 [pool-1-thread-13] DEBUG r.b.t.app.server.ConnectionCallable - Data
to send from server: 6817121021052420000000000000000100000730173724000048001121
101581111310001749439 749138 00809203123643
in connection id=ee7f73ac-6be9-4e7b-876f-35d31845d69e
17:37:26.166[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-Mess
收到的年龄。
17:37:26.166[pool-1-thread-13]INFO r.b.t.app.server.ConnectionCallable-resp
回声模式下的onsing
17:37:26.166[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-数据
从服务器发送:连接id=ee7f73ac-6be9-4e7b-876f-35d31845d69e
17:37:26.166[pool-1-thread-13]INFO r.b.t.app.server.ConnectionCallable-resp
昂德
17:37:26.166[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-Lent
收到信息的时间:200r
17:37:26.167[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-:
java.lang.NumberFormatException:用于输入字符串:“200r”
在java.lang.NumberFormatException.forInputString(未知源)~[na:1
.8.0_05]
在java.lang.Integer.parseInt(未知源代码)~[na:1.8.0\u 05]
在java.lang.Integer.valueOf(未知源代码)~[na:1.8.0\u 05]
在ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:87)[TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
在ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:1)[TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
在java.util.concurrent.FutureTask.run(未知源代码)[na:1.8.0\u 05]
位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)[na]
:1.8.0_05]
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(未知源)[n
a:1.8.0_05]
在java.lang.Thread.run(未知源代码)[na:1.8.0_05]
17:37:26.167[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-Mess
收到的年龄。
17:37:26.167[pool-1-thread-13]INFO r.b.t.app.server.ConnectionCallable-resp
回声模式下的onsing
17:37:26.167[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-数据
从服务器发送:连接id=ee7f73ac-6be9-4e7b-876f-35d31845d69e
17:37:26.168[pool-1-thread-13]INFO r.b.t.app.server.ConnectionCallable-resp
昂德
17:37:26.168[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-Lent
h收到的信息数量:4♦А
17:37:26.168[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-╬°шс
ър яЁхюсЁрчютрэш фышэ√ ёююс∙хэш :
java.lang.NumberFormatException:对于输入字符串:“4”♦А "
在java.lang.NumberFormatException.forInputString(未知源)~[na:1
.8.0_05]
在java.lang.Integer.parseInt(未知源代码)~[na:1.8.0\u 05]
在java.lang.Integer.valueOf(未知源代码)~[na:1.8.0\u 05]
在ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:87)[TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
在ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:1)[TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
在java.util.concurrent.FutureTask.run(未知源代码)[na:1.8.0\u 05]
位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)[na]
:1.8.0_05]
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(未知源)[n
a:1.8.0_05]
在java.lang.Thread.run(未知源代码)[na:1.8.0_05]
17:37:26.168[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-Mess
收到的年龄。
17:37:26.168[pool-1-thread-13]INFO r.b.t.app.server.ConnectionCallable-resp
回声模式下的onsing
17:37:26.168[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-数据
从服务器发送:连接id=ee7f73ac-6be9-4e7b-876f-35d31845d69e
17:37:26.168[pool-1-thread-13]INFO r.b.t.app.server.ConnectionCallable-resp
昂德
17:37:26.168[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-Lent
h.收到的信息:┴А 2
17:37:26.169[pool-1-thread-13]调试r.b.t.app.server.ConnectionCallable-╬°шс
ър яЁхюсЁрчютрэш фышэ√ ёююс∙хэш :
java.lang.NumberFormatException:用于输入字符串:┴А 2"
在java.lang.NumberFormatException.forInputString(未知源)~[na:1
.8.0_05]
在java.lang.Integer.parseInt(未知源代码)~[na:1.8.0\u 05]
在java.lang.Integer.valueOf(未知源代码)~[na:1.8.0\u 05]
在ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:87)[TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
在ru.billing.tcpipServerDummy.app.server.ConnectionCallable.call(Connec
tionCallable.java:1)[TcpipServerDummy-0.0.1-SNAPSHOT.jar:na]
在java.util.concurrent.FutureTask.run(未知源代码)[na:1.8.0\u 05]
位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)[na]
:1.8.0_05]
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(未知源)[n
a:1.8.0_05]