WitVault
WitVault

Reputation: 24140

Security Exception while connecting using sftp through JSCH

I am getting this error while creating SFTP with JSCH. I am trying to connect to a remote server via sftp to transfer files but while calling connect method below error occurs.

java.lang.ExceptionInInitializerError
at javax.crypto.Cipher.getInstance(DashoA13*..)
at com.jcraft.jsch.jce.AES256CTR.init(AES256CTR.java:56)
at com.jcraft.jsch.Session.checkCipher(Session.java:2072)
at com.jcraft.jsch.Session.checkCiphers(Session.java:2049)
at com.jcraft.jsch.Session.send_kexinit(Session.java:592)
at com.jcraft.jsch.Session.connect(Session.java:286)
at com.jcraft.jsch.Session.connect(Session.java:162)
at scb.frame.runner.ServerSetup.contactServer(ServerSetup.java:56)

Caused by: java.lang.SecurityException: Cannot set up certs for trusted CAs
at javax.crypto.SunJCE_b.<clinit>(DashoA13*..)
... 34 more
Caused by: java.lang.SecurityException: Cannot locate policy or framework files!
at javax.crypto.SunJCE_b.i(DashoA13*..)
at javax.crypto.SunJCE_b.g(DashoA13*..)
at javax.crypto.SunJCE_b$1.run(DashoA13*..)
at java.security.AccessController.doPrivileged(Native Method)
... 35 more

My Code to Connect :

    String host = serverProperties.getProperty("Host");
    String username = serverProperties.getProperty("Username");
    String password = serverProperties.getProperty("Password");
        JSch jsch = new JSch();
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        Session session;
        try {
            session = jsch.getSession(username, host, 22);

        session.setPassword(password);

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
        sftpChannel.connect();
        sftpChannel.get(sourceFile,destFile);

Command line version of Same Program I am able to run without any exception! also example of SFTP from Jcraft[link]http://www.jcraft.com/jsch/examples/Sftp.java.html website is running perfectly.

Upvotes: 1

Views: 5918

Answers (1)

Rens Verhage
Rens Verhage

Reputation: 5857

It has to do with Java's security restrictions. By default, there is a restricted key size of 128 bits. From your stacktrace, I see you're trying to use an AES 256 bit encryption.

To solve your problem, go to Java's website and download the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

(to be found at the bottom)

Install them in your JRE and you are good to go :) All thanks to US government...

Upvotes: 1

Related Questions