Java8 + Jcraft =这个算法的密钥太长了

时间:2014-08-20 12:12:29

标签: java ssh sftp java-8 jsch

我们正在使用JCraft / Jsch进行sftp文件传输。 http://www.jcraft.com/jsch

使用Java6和Java 7已经有好几年了。 但后来我们升级到Java 8,它在我们的测试环境中运行良好,但在我们的QA环境中失败了。然后它对远程SFTP服务器失败了。 此SFTP服务器使用“SSH-2.0-4.2.0.21 SSH安全Shell Windows NT Server” 我们无法控制此服务器。

我google了一下,发现有些人认为问题的根源是我们没有安装“Java Cryptography Extension(JCE)Unlimited Strength”。 我知道这是必需的,它安装在Java6 / 7上。 在Java 8上安装它,结果是一样的。

我已经在Windows 8和Linux上测试了Java 8 update 5和update 11。

我们使用Jcraft版本0.1.51(最新) 当我切换到Java7运行时,一切正常。

我得到的错误就是这个:

com.jcraft.jsch.JSchException: Session.connect: java.security.InvalidKeyException: Key is too long for this algorithm
at com.jcraft.jsch.Session.connect(Session.java:558)
at com.jcraft.jsch.Session.connect(Session.java:183)
at sb1.fip.backend.ssh2.sftp.SFTPClientImpl.connect(SFTPClientImpl.java:241)

我怀疑JCraft有问题。

我执行了这个程序: http://sourceforge.net/p/jsch/mailman/message/32660306/ 它确认我已经安装了Java Cryptography Extension。

这是失败的代码:

private void connect() {
        try {
            JSch.setLogger(new JschLogger());
            JSch jsch = new JSch();
            session = jsch.getSession(sftpConfig.getUsername(), sftpConfig.getHost(), sftpConfig.getPort());
            session.setUserInfo(new SFTPUserInfo(sftpConfig.getPassword()));
            session.setTimeout(150000);
            session.connect();
            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
        } catch (Exception e) {
            throw new SystemException(Ssh2ErrorCode.SFTP_CONNECT_ERROR, sftpConfig.getLoggableConfigInfo(), e);
        }
    }

多一点日志:

2014-08-20 14:01:52,801 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: Connecting to sftp.il.stralfors.com port 22
2014-08-20 14:01:52,824 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: Connection established
2014-08-20 14:01:53,181 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: Remote version string: SSH-2.0-4.2.0.21 SSH Secure Shell Windows NT Server
2014-08-20 14:01:53,181 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: Local version string: SSH-2.0-JSCH-0.1.51
2014-08-20 14:01:53,181 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
2014-08-20 14:01:53,313 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: CheckKexes: diffie-hellman-group14-sha1
2014-08-20 14:01:53,345 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: SSH_MSG_KEXINIT sent
2014-08-20 14:01:53,345 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: SSH_MSG_KEXINIT received
2014-08-20 14:01:53,345 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: diffie-hellman-group1-sha1
2014-08-20 14:01:53,346 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: ssh-dss
2014-08-20 14:01:53,346 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: aes128-cbc,3des-cbc,twofish128-cbc,cast128-cbc,twofish-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,twofish192-cbc,twofish256-cbc,arcfour
2014-08-20 14:01:53,346 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: aes128-cbc,3des-cbc,twofish128-cbc,cast128-cbc,twofish-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,twofish192-cbc,twofish256-cbc,arcfour
2014-08-20 14:01:53,346 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96
2014-08-20 14:01:53,347 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96
2014-08-20 14:01:53,347 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: none,zlib
2014-08-20 14:01:53,347 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: none,zlib
2014-08-20 14:01:53,347 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: 
2014-08-20 14:01:53,348 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server: 
2014-08-20 14:01:53,348 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1
2014-08-20 14:01:53,348 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: ssh-rsa,ssh-dss
2014-08-20 14:01:53,348 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc
2014-08-20 14:01:53,348 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc
2014-08-20 14:01:53,349 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
2014-08-20 14:01:53,349 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
2014-08-20 14:01:53,349 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: none
2014-08-20 14:01:53,349 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: none
2014-08-20 14:01:53,349 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: 
2014-08-20 14:01:53,350 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client: 
2014-08-20 14:01:53,353 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: server->client aes128-cbc hmac-md5 none
2014-08-20 14:01:53,354 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: kex: client->server aes128-cbc hmac-md5 none
2014-08-20 14:01:53,360 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: SSH_MSG_KEXDH_INIT sent
2014-08-20 14:01:53,360 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: expecting SSH_MSG_KEXDH_REPLY
2014-08-20 14:01:53,419 [INFO ] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [JschLogger     ]: Disconnecting from sftp.il.stralfors.com port 22
2014-08-20 14:01:53,450 [ERROR] [HTY50006] [main ] [9.13.0] [666349519712388] [JUNIT-TEST] [] [StraalforsBackendNoStubTest]: SSH error!
sb1.fip.infra.core.error.SystemException: ErrorCode:FIP-3802 ErrorId:AHWK-9LKI ErrorMsg:Feil i ssh2-backend. Connect failed. SFTPConfig[STRAALFORS, Sb1Ltest@sftp.il.stralfors.com:22, remoteDownloadDir:/tmp, remoteUploadDir:/tmp, localDownloadDir:C:\Users\a11632u\AppData\Local\Temp\/, localUploadDir:C:\Users\a11632u\AppData\Local\Temp\/, uploadPattern:.*, downloadPattern:.*] RootCause:com.jcraft.jsch.JSchException:Session.connect: java.security.InvalidKeyException: Key is too long for this algorithm
    at sb1.fip.backend.ssh2.sftp.SFTPClientImpl.connect(SFTPClientImpl.java:243) ~[classes/:na]
    at sb1.fip.backend.ssh2.sftp.SFTPClientImpl.listDirs(SFTPClientImpl.java:100) ~[classes/:na]
    at sb1.fip.backend.straalfors.StraalforsBackend.listDirs(StraalforsBackend.java:42) ~[classes/:na]
    at sb1.fip.backend.straalfors.StraalforsBackendNoStubTest.testListDirs(StraalforsBackendNoStubTest.java:22) ~[test-classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_11]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_11]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_11]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_11]
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) [junit-4.11.jar:na]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:na]
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) [junit-4.11.jar:na]
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.11.jar:na]
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) [junit-4.11.jar:na]
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) [spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) [junit-4.11.jar:na]
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) [spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) [spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233) [spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87) [spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [junit-4.11.jar:na]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) [spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [junit-4.11.jar:na]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176) [spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160) [junit-4.11.jar:na]
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) [junit-rt.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_11]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_11]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_11]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_11]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) [idea_rt.jar:na]
Caused by: com.jcraft.jsch.JSchException: Session.connect: java.security.InvalidKeyException: Key is too long for this algorithm
    at com.jcraft.jsch.Session.connect(Session.java:558) ~[jsch-0.1.51.jar:na]
    at com.jcraft.jsch.Session.connect(Session.java:183) ~[jsch-0.1.51.jar:na]
    at sb1.fip.backend.ssh2.sftp.SFTPClientImpl.connect(SFTPClientImpl.java:239) ~[classes/:na]
    ... 36 common frames omitted

1 个答案:

答案 0 :(得分:2)

有类似的问题。 一些带有SSH的遗留系统(开放式VMS),它实现了带有2048位密钥的SHA-1。 不确定它是否是一个jsch错误,或者是一个错误的ssh实现。 我也必须使用java 8,因为我使用javafx 8功能。 无论如何,为了解决这个问题,我得到了openjdk代码,滥用了代码,如下:

sun.security.provider.DSA.java ...
@Override
    protected void checkKey(DSAParams params) throws InvalidKeyException {
        int valueL = params.getP().bitLength();
     // comment out this check since it only 
     // enforces the protocol and doesn't break the functionality
     // if (valueL > 1024) {
     //    throw new InvalidKeyException("Key is too long for this algorithm");
     // }
    }

编译它并使用-Xbootclasspath / p选项来激活它。 我知道这是不好的做法,但修复服务器不是一种选择,修复jsch可能是一项艰巨的任务。 欢迎你为此谴责我。