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

BouncyCastle is required to read a key of type ecdsa-sha2-nistp256

我正在使用 sshj (0.19.1) 开发一个带有 SSH2 连接的 Scala 应用程序。我可以在 eclipse 中正常连接,但是 运行 来自 fat jar 的应用程序,我遇到了一些错误。我最初的错误是:

Line 3: TransportException: null
 at net.schmizz.sshj.transport.TransportException.chain(33)
 at net.schmizz.sshj.transport.TransportException.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.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

我是 运行 jar 作为 java -cp ../lib/bcprov-jdk15on-1.51.jar -jar <my jar>,因为据我了解,您不能将 bouncycastle 作为提供程序的 fat jar 的一部分。但我不确定我错过了什么,我无法将其注册为提供商。任何帮助将不胜感激。

java -jar 忽略来自命令行或环境变量的类路径,而是使用指定的 jar 加上其清单中的任何 class-path 项。

  • reference bcprov 在 jar 的清单中(但是不要 将像 bcprov 这样的提供者合并到你自己的 jar 中,你是对的)或

  • 将 bcprov 放入您的 JRE/lib/ext 以便 JVM 无需使用类路径即可找到它。

或 运行 与 java -cp myjar:bcprov mainclassname(windows 上的 ;)使用类路径。

我希望你的 new BouncyCastleProvider() 在你的代码中尝试使用提供者之前抛出错误,但我不知道 scala,也许这里有所不同。

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