Java 读取ecdsa-sha2-nistp256类型的密钥需要BouncyCastle

Java 读取ecdsa-sha2-nistp256类型的密钥需要BouncyCastle,java,scala,jar,bouncycastle,sshj,Java,Scala,Jar,Bouncycastle,Sshj,我正在使用sshj(0.19.1)开发一个带有SSH2连接的scala应用程序。我可以在eclipse中进行良好的连接,但是在胖罐子中运行应用程序时,我会遇到一些错误。我最初的错误是: Line 3: TransportException: null at net.schmizz.sshj.transport.TransportException$1.chain(33) at net.schmizz.sshj.transport.TransportException$1.chain(27)

我正在使用sshj(0.19.1)开发一个带有SSH2连接的scala应用程序。我可以在eclipse中进行良好的连接,但是在胖罐子中运行应用程序时,我会遇到一些错误。我最初的错误是:

Line 3: TransportException: null
 at net.schmizz.sshj.transport.TransportException$1.chain(33)
 at net.schmizz.sshj.transport.TransportException$1.chain(27)
 at net.schmizz.concurrent.Promise.deliverError(96)
 at net.schmizz.concurrent.Event.deliverError(74)
 at net.schmizz.concurrent.ErrorDeliveryUtil.alertEvents(34)
 at net.schmizz.sshj.transport.KeyExchanger.notifyError(386)
 at net.schmizz.sshj.transport.TransportImpl.die(596)
 at net.schmizz.sshj.transport.Reader.run(68)
我已经将US_export_policy.jar和local_policy.jar添加到$JAVA_HOME/lib/security中。我添加了以下内容以尝试将bouncycastle注册为提供商

import org.bouncycastle.jce.provider.BouncyCastleProvider
val bouncyCastle = new BouncyCastleProvider()
java.security.Security.addProvider(bouncyCastle)
net.schmizz.sshj.common.SecurityUtils.registerSecurityProvider("org.bouncycastle.jce.provider.BouncyCastleProvider")
但我仍然得到以下错误:

INFO  n.schmizz.sshj.common.SecurityUtils - Registration of Security Provider 'org.bouncycastle.jce.provider.BouncyCastleProvider' unexpectedly failed
INFO  n.schmizz.sshj.common.SecurityUtils - BouncyCastle not registered, using the default JCE provider
INFO  n.s.sshj.transport.random.JCERandom - Creating new SecureRandom.
WARN  net.schmizz.sshj.DefaultConfig - Illegal key size
WARN  net.schmizz.sshj.DefaultConfig - Cannot find any provider supporting Twofish/CBC/NoPadding
...
WARN  net.schmizz.sshj.DefaultConfig - Illegal key size or default parameters
WARN  net.schmizz.sshj.DefaultConfig - Disabling high-strength ciphers: cipher strengths apparently limited by JCE policy
INFO  n.s.sshj.transport.TransportImpl - Client identity string: SSH-2.0-SSHJ_0.19.1
INFO  n.s.sshj.transport.TransportImpl - Server identity string: SSH-2.0-OpenSSH_6.6.1
ERROR n.s.sshj.transport.TransportImpl - Dying because - {}
net.schmizz.sshj.common.SSHRuntimeException: BouncyCastle is required to read a key of type ecdsa-sha2-nistp256
    at net.schmizz.sshj.common.Buffer.readPublicKey(Buffer.java:431)
    at net.schmizz.sshj.transport.kex.AbstractDHG.next(AbstractDHG.java:66)
    at net.schmizz.sshj.transport.KeyExchanger.handle(KeyExchanger.java:358)
    at net.schmizz.sshj.transport.TransportImpl.handle(TransportImpl.java:493)
    at net.schmizz.sshj.transport.Decoder.decode(Decoder.java:104)
    at net.schmizz.sshj.transport.Decoder.received(Decoder.java:172)
    at net.schmizz.sshj.transport.Reader.run(Reader.java:60)
Caused by: java.security.GeneralSecurityException: BouncyCastle is required to read a key of type ecdsa-sha2-nistp256
    at net.schmizz.sshj.common.KeyType$3.readPubKeyFromBuffer(KeyType.java:120)
    at net.schmizz.sshj.common.Buffer.readPublicKey(Buffer.java:429)
    ... 6 common frames omitted
INFO  n.s.sshj.transport.TransportImpl - Disconnected - UNKNOWN
ERROR net.schmizz.concurrent.Promise - <<kex done>> woke to: net.schmizz.sshj.transport.TransportException: BouncyCastle is required to read a key of type ecdsa-sha2-nistp256
INFO n.schmizz.sshj.common.SecurityUtils-安全提供程序“org.bouncycastle.jce.Provider.BouncyCastleProvider”的注册意外失败
信息n.schmizz.sshj.common.SecurityUtils-未使用默认JCE提供程序注册BouncyCastle
信息n.s.sshj.transport.random.JCERandom-创建新的SecureRandom。
WARN net.schmizz.sshj.DefaultConfig-密钥大小非法
WARN net.schmizz.sshj.DefaultConfig-找不到任何支持Twofish/CBC/nopAdd的提供程序
...
WARN net.schmizz.sshj.DefaultConfig-密钥大小或默认参数非法
WARN net.schmizz.sshj.DefaultConfig-禁用高强度密码:密码强度显然受到JCE策略的限制
INFO n.s.sshj.transport.TransportImpl-客户端标识字符串:SSH-2.0-sshj_0.19.1
INFO n.s.sshj.transport.TransportImpl-服务器标识字符串:SSH-2.0-OpenSSH_6.6.1
错误n.s.sshj.transport.TransportImpl-由于{}而死亡
net.schmizz.sshj.common.SSHRuntimeException:读取ecdsa-sha2-nistp256类型的密钥需要BouncyCastle
位于net.schmizz.sshj.common.Buffer.readPublicKey(Buffer.java:431)
位于net.schmizz.sshj.transport.kex.AbstractDHG.next(AbstractDHG.java:66)
在net.schmizz.sshj.transport.keyechanger.handle(keyechanger.java:358)
位于net.schmizz.sshj.transport.TransportImpl.handle(TransportImpl.java:493)
net.schmizz.sshj.transport.Decoder.decode(Decoder.java:104)
net.schmizz.sshj.transport.Decoder.received(Decoder.java:172)
net.schmizz.sshj.transport.Reader.run(Reader.java:60)
原因:java.security.GeneralSecurityException:读取ecdsa-sha2-nistp256类型的密钥需要BouncyCastle
net.schmizz.sshj.common.KeyType$3.readPubKeyFromBuffer(KeyType.java:120)
位于net.schmizz.sshj.common.Buffer.readPublicKey(Buffer.java:429)
... 省略了6个公共框架
信息n.s.sshj.transport.TransportImpl-已断开连接-未知
错误net.schmizz.concurrent.Promise-唤醒到:net.schmizz.sshj.transport.TransportException:读取ecdsa-sha2-nistp256类型的密钥需要BouncyCastle

我将jar作为
java-cp../lib/bcprov-jdk15on-1.51.jar-jar运行,因为据我所知,您不能将bouncycastle作为fat jar的一部分作为提供者。但我不确定我遗漏了什么,因为我无法将其注册为提供商。任何帮助都将不胜感激。

java-jar
忽略命令行或envvar中的类路径,而是使用指定的jar及其清单中的任何
类路径
项。或者

  • 在jar的清单中引用bcprov(但不要将像bcprov这样的提供者合并到您自己的jar中,这一点您是正确的),或者

  • 将bcprov放在JRE/lib/ext中,这样JVM就可以在不使用类路径的情况下找到它

或者使用使用类路径的
java-cp myjar:bcprov mainclassname
)运行

我希望您的
新的BouncyCastleProvider()
在您的代码中尝试使用提供程序之前抛出,但我不知道scala,这里可能有所不同


FWIW,如果这个项目升级到5年前(Java7),您就不需要BouncyforEC,包括ECDSA。对于Twofish,您仍然需要它,但我不知道是谁(其他人)实现了Twofish,如果有人需要它,我会感到惊讶;这里您的服务器标识为OpenSSH,而OpenSSH没有标识。

谢谢,我会尝试一下。当您提到java7时,这很有趣,因为我正在运行1.8.0_121。我没有使用gradle,所以我不是100%熟悉它,但我注意到在github上的sshj build.gradle中,它使用sourceCompatibility和targetCompatibility=1.6。如果这就是我为什么要跳过这些障碍的原因,我应该重新考虑使用那个图书馆。谢谢。将bcprov复制到JRE/lib/ext就成功了。我将研究是否是sshj中的sourceCompatibility=1.6首先使其成为必要。如果是这样的话,那么我可能会切换到另一个ssh库,以便更容易地向那些不需要将额外文件复制到java_home中的客户推出该库。作者的评论:)(我):SSHJ使用Java6,因为仍有许多用户需要它。例如,请参阅:。我实际上正在努力使安全提供程序(BC、JCE、SC等)更易于切换,并且不再对BC有任何硬性要求。