3

I'm trying to use JSch with a private key configuration. I've generated a public and private key using PuTTYgen but am unsure what to do with both of the files.

Which key (public/private) needs transferring to the server?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user3424480
  • 362
  • 2
  • 6
  • 19

2 Answers2

5

Code Snippet for connection using PuTTy private Key (.ppk)

JSch jsch=new JSch();
jsch.setKnownHosts("~\.ssh\know_hosts");
jsch.addIdentity("~\sshkey.ppk");
Session session=jsch.getSession("ec2-user", "54.12.11.90", 22);
session.setConfig("PreferredAuthentications", "publickey");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel=session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(3*1000);

Have used 0.1.54 version of Jsch

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>
Abhimanyu Garg
  • 416
  • 4
  • 10
  • 6
    Please do not suggest anyone to use `StrictHostKeyChecking=no` without explaining security consequences. You are losing a protection against [MIMT attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) by doing so! – Martin Prikryl Sep 05 '18 at 04:26
4

Make sure you use the latest version of JSch, as older versions do not support the .ppk format natively.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992