由于 java.security.InvalidAlgorithmParameterException,SFTP 连接失败

SFTP connection Failed due to java.security.InvalidAlgorithmParameterException

我正在尝试使用 SFTP 连接到无密码配置的服务器。 Sftp 连接成功使用终端。 但是当我通过用户名和密码连接 JAVA(使用 Jsch 库)时,我无法连接。 我的 java 代码:-

try {
        try {
            jsch.addIdentity(ftp_Info.getSftpCertFile());
        } catch (Exception e) {
            // TODO: Add a log message
        }
        session = jsch.getSession(ftp_Info.getUserName(), ftp_Info.getHost(), ftp_Info.getPort());
        String pswd = (password_encypted) // password encryption
        session.setPassword(pswd);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setConfig("PreferredAuthentications", "password,hostbased,publickey");
        session.connect(); // exception occurred here
        session.setTimeout(connectionTimeOut);
        Channel channel = session.openChannel(SFTP);
        channel.connect();
        sftpChannel = (ChannelSftp) channel;

    } catch (Exception e) {
        log.error(e.getMessage(), e);//error logged here
    }

我遇到以下异常:-

com.jcraft.jsch.JSchException: Session.connect: java.security.InvalidAlgorithmParameterException: Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive) at com.jcraft.jsch.Session.connect(Session.java:485) at com.jcraft.jsch.Session.connect(Session.java:149)

请帮助排除故障或解决它。 除了第三方服务提供商,有什么方法可以让我的2048位密钥通过这个异常吗?

在 1.7 下,我假设您正在为您的项目使用 maven。我会将 bouncycastle 依赖项添加到您的 pom。

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk16</artifactId>
    <version>1.45</version>
</dependency>

jdk 7.

这应该可以正常工作

然后添加一行代码,将 BouncyCastle 提供程序添加为第一个提供程序。

Security.insertProviderAt(new BouncyCastleProvider(),1);

我会把它放在您的 getSftpCertFile() 调用之前和任何 SSL 相关代码之前。如果您不使用 Maven 或有不同的基础设施,请告诉我。您可以在 JRE 级别配置安全提供程序,但我总是希望尽可能在项目级别配置,以免影响其他项目。