JSch cannot log in with a key that the ssh command accepts
com.jcraft.jsch.JSchException: Auth failShort answer
The same key can work in OpenSSH and fail in JSch because the two negotiate different signature algorithms. Servers on OpenSSH 8.8 or newer refuse RSA/SHA-1, and Ed25519 needs Java 15 or Bouncy Castle in the JSch fork. Move to the maintained fork and pick the matching fix.
Fix it fast
1Find out which cause you have
Run the OpenSSH client verbosely with the same key and look at which key type and signature algorithm the server accepts. This tells you which cause below applies.
ssh -vvv -i ~/.ssh/id_rsa deploy@203.0.113.10 true2Match what you see, then jump to its fix
- 1
Auth fail with an ssh-rsa key against a server on OpenSSH 8.8 or laterThe server turned off RSA/SHA-1 and the JSch version can only sign with itFix - 2
Auth fail or algorithm errors with an ssh-ed25519 keyThe key is Ed25519 and the runtime cannot use itFix - 3
The server is old, and the OpenSSH client needs +ssh-rsa to connect to it as wellA legacy server only accepts RSA/SHA-1 (stopgap)Fix
- 1
What does the error look like?
com.jcraft.jsch.JSchException: Auth failAlgorithm negotiation fail
Cause 1 of 3
The server turned off RSA/SHA-1 and the JSch version can only sign with it
1Check
OpenSSH 8.8 disabled RSA signatures using SHA-1 by default. OpenSSH itself switches to the stronger RSA/SHA-256/512 signatures for existing ssh-rsa keys. A JSch version that only offers SHA-1 signatures is therefore refused.
mvn dependency:tree | grep -i jsch2Fix
Replace the old com.jcraft jsch artifact with the maintained fork. Its docs say ssh-rsa keys keep working by default using the RSA/SHA-256 and RSA/SHA-512 signature algorithms.
<dependency>
<groupId>com.github.mwiede</groupId>
<artifactId>jsch</artifactId>
<version>0.2.x</version> <!-- use the current release -->
</dependency>3Verify
Run the connect code again; the session connects and the command runs.
# session.connect() returns without JSchExceptionConfirmed by openssh.org
Cause 2 of 3
The key is Ed25519 and the runtime cannot use it
1Check
The fork's README says ssh-ed25519 and ssh-ed448 need at least Java 15 or Bouncy Castle on the classpath. Check the Java version.
java -version2Fix
Run on Java 15 or newer, or add Bouncy Castle (bcprov-jdk18on) to the classpath.
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.x</version> <!-- use the current release -->
</dependency>3Verify
Connect again with the Ed25519 key.
# session.connect() succeedsConfirmed by github.com
Cause 3 of 3
A legacy server only accepts RSA/SHA-1 (stopgap)
1Check
OpenSSH's notes say RSA/SHA-1 can be re-enabled selectively through HostkeyAlgorithms and PubkeyAcceptedAlgorithms, as a stopgap until the server is upgraded.
ssh -o PubkeyAcceptedAlgorithms=+ssh-rsa -o HostkeyAlgorithms=+ssh-rsa deploy@legacy.example.com true2Fix
Enable ssh-rsa for that one JSch session only, then plan the server upgrade.
session.setConfig("server_host_key", session.getConfig("server_host_key") + ",ssh-rsa");
session.setConfig("PubkeyAcceptedAlgorithms", session.getConfig("PubkeyAcceptedAlgorithms") + ",ssh-rsa");3Verify
The session connects to the legacy host.
# session.connect() succeedsConfirmed by github.com
How do I stop it coming back?
- Use the maintained JSch fork and keep it updated.
- Prefer Ed25519 keys on a current Java, or RSA keys with SHA-2 signatures.
- Re-enable ssh-rsa per session only for a legacy host, never for the whole application.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
The AWS SDK for Java cannot find credentials, and AwsCredentials.properties does nothing
software.amazon.awssdk.core.exception.SdkClientException: Unable to load credentials from any of the providers in the chain
AwsCredentials.properties on the classpath is an old convention: the default provider chain of the AWS SDK for Java does not read it.
4 causes, each with a fixBack end
Gradle runs no Cucumber scenarios from my feature files
Feature files exist under src/test but nothing runs
Cucumber runs on the JUnit Platform, so Gradle must call useJUnitPlatform() and have the cucumber-junit-platform-engine on the test classpath.
4 causes, each with a fixBack end
A Gradle build fails on classes that use Lombok annotations
Error: Annotation processors must be explicitly declared now.
Lombok is two things for Gradle: a compile-time library and an annotation processor.
3 causes, each with a fixBack end
Gradle 8 fails on Report destination: switch to outputLocation
What went wrong: 'java.io.File org.gradle.api.reporting.ConfigurableReport.getDestination()'
Gradle 8.0 removed the deprecated destination and enabled properties of the Report type.
2 causes, each with a fixBack end
Apache HttpClient 5 has no setSSLSocketFactory on HttpClients.custom()
HttpClients.custom().setSSLSocketFactory() method not found
In Apache HttpClient 5 the TLS setup no longer sits on the client builder.
2 causes, each with a fix