Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am new to SFTP and need help understanding following. I research half a day on net but could not found relevant ans.

What is role of keys in SFTP? Is it just for authentication or also encrypting data?

My assumption is Lets say I am using computer name "SOURCE" and I need to send some file to another computer name "DESTINATION".

Now I think the computer named "SOURCE" will generate pair of public and private keys. It send public key to computer "DESTINATION" and keep private key secure.

Now I am confused because if SFTP uses keys to encrypt data then if computer "SOURCE" encrypt the data using public key and send to computer name "DESTINATION", The Destination computer does not have private key to decrypt the file, then how is this all working?

On another note if KEYS are only use to authenticate then how this works. so computer "SOURCE" has both private and public key and computer "DESTINATION" has only the public key. Now my understanding is anyone can have a public key so if man in middle has a public key and if he install it on his PC, how security works then?

Before I start correcting misconceptions let's put down some foundation-level information.

The keys are a key pair consisting of a public key and private key. They are as they sound; the private key should always remain private. If you ever find yourself transmitting a private key you are doing something wrong . To be honest, it almost doesn't matter what you do with the public key, except for deleting it.

Information is encrypted using the public key and decrypted using the private key.

This is not symmetric encryption. The public key cannot even decrypt it's own encrypted messages.

Lets say I am using computer name "SOURCE" and I need to send some file to another computer name "DESTINATION"

Let's not. Let's rename those to SOURCE client and DESTINATION server

What is role of keys in SFTP? Is it just for authentication or also encrypting data?

Both. However, they are not the same key pairs. The initial key pair is used for authentication, then the client and server work together to generate a symmetric key which is used for encrypting the connection.

Now I think the computer named "SOURCE" client will generate pair of public and private keys. It send public key to computer "DESTINATION" the server and keep private key secure.

Yes, this is the best and only practice.

Now I am confused because if SFTP uses keys to encrypt data then ... The Destination computer server does not have private key to decrypt the file, then how is this all working?

Part of the connection process involves both of the client and server generating another key pair each , which are used to create a symmetric key which encrypts data sent during the session. This is the reason why SFTP is so secure, the negotiation and symmetric key generation process is protected by the same key pair technology and even better, there is no opportunity for a human entity to even try to transmit his private key

... Now my understanding is anyone can have a public key so if man in middle has a public key and if he install it on his PC, how security works then?

This is the reason why I said that it basically does not matter what you do with the public key in the beginning of this post, with the only exception being deleting it (without sending it to anyone). Whoever holds the private key is the initiator . In theory, there could be a Man-in-the-Middle which intercepts your initial request, blocks it from reaching it's destination, then pretends to be the server, but this is unlikely*. If you are a high-value target, you can also further authenticate a certificate to confirm identity.

I went on a little tangent there but if someone else was trying to initiate connection to you , they would need a separate key pair because the encryption is one-way . (by the way for that connection you would be considered the server and they would be the client). Essentially, all the MitM has done is given you the ability to connect to him via SSH, if you choose to

*especially if you are a low-value target. Nobody is going to MitM your SSH connection so they can hack your rasberry pi and flick your lights on and off using your homebrew home automation. A much better and easier target is social engineering your banks customer service department into giving your password to them. Scary, isn't it?

  • data encryption which use symmetric-key algorithms such as 3DES, AES,..
  • public key authentication which use asymmetric algorithms such as RSA, ECDSA,..
  • From the sftp man page :

    sftp is an interactive file transfer program, similar to ftp, which performs all operations over an encrypted ssh transport . It may also use many features of ssh, such as public key authentication [...]

    From the ssh man page :

    Public key authentication works as follows: The scheme is based on public-key cryptography, using cryptosystems where encryption and decryption are done using separate keys, and it is unfeasible to derive the decryption key from the encryption key. The idea is that each user creates a public/private key pair for authentication purposes. The server knows the public key, and only the user knows the private key.

    the data encryption algorithm can be selected with the -c option :

    -c cipher_spec Selects the cipher specification for encrypting the session .

    The supported ciphers are: - 3des-cbc - aes128-cbc - aes192-cbc - aes256-cbc [...]

  • The authentication keys (assymetric) are stored in the ~/.ssh/ directory and are not used for data encryption .
  • The data encryption keys (symmetric) are created per session through a key exchange algorithm and are never communicate between the client and the server even if the same key is present on both side of the communication.
  • For more details, you can read this article from Digital Ocean : Understanding the SSH Encryption and Connection Process

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question . Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers .