Search code examples
javasshpublic-key-encryptionjschprivate-key

addIdentity in JSch is private key of calling program's Server


"Server 1" is reading from and writing to "Server 2" using SSH.

  • "Server 1" is having "Server 1's" private key and public key of "Server 2".
  • "Server 2" is having "Server 2's" private key and public key of "Server 1".

In JSch, when I use a Java program to communicate from "Server 1" to "Server 2", the program is using addIdentity method. This method is using "Server 1's" private key and JSch will not send this to anybody ("Server 2"), am I right?

Am confused with mentioning private key in this in addIdentity method. If possible please give me some JSch related doc which enlighten me more.


Solution

  • Your understanding is correct.

    The .addIdentity is used to specify what private key should be used to locally encrypt the data before sending them to the remote server (and to decrypt the incoming data). The private key itself is not sent anywhere. Only a public key is.

    Note that the .addIdentity loads a key pair (both the public and the private key). Even if you use an overload that takes prvkey path only, the JSch assumes that the public key file has the same name, just .pub extension; or that the "private key" file actually contains both public and private key.


    For details, read about Public-key/asymmetric cryptography.


    Though note that the key is a private key of the account you are connecting with to the remote server. It's not (should not be) a private key of the local machine (even if the machine happens to be a [SSH] server).