There’s a server, a client, and a hacker in a network. For encryption, the client and the server need to share their private keys. Wouldn’t the hacker be able to grab those during their transmission and decrypt further messages as they please?

  • RegalPotoo@lemmy.world
    link
    fedilink
    English
    arrow-up
    25
    ·
    8 months ago

    You’ve missed a key detail in how asymmetric encryption works:

    • For asymmetric encryption algorithms, you essentially have two keys - a “private” key, and a “public” key
    • If you know the private key it is trivial to calculate the public key, but the reverse isn’t true - just given the public key, it is essentially impossible to calculate the private key in a reasonable amount of time
    • If you encrypt something with the public key you must use the private key to decrypt it, and if you encrypt with the private key you can only use the public key for decryption
    • This means that my server can advertise a public key, and you can use that to encrypt the traffic so that only the server that knows the private key can decrypt it
    • lad@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      8 months ago

      I used to know that and still struggle to understand how a handshake wouldn’t allow MitM. Later I found out that it requires a third party with a trusted and known certificate for signing handshake exchange messages in order to ensure there’s no man in the middle: https://stackoverflow.com/a/10496684

      • RegalPotoo@lemmy.world
        link
        fedilink
        English
        arrow-up
        4
        ·
        8 months ago

        A big “It Depends” on that - plenty of applications of asymmetric crypto where you just hard-code the servers public key into the client and call it a day, and GPG has its own PKI scheme that is just kinda weird.

        You also don’t have to use Diffie-Hellman - early versions of SSL just sent the ephemeral key (the symmetric key used for the actual AES session) directly. This works, but using DH also gives you “forward secrecy” - even if a malicious third party has captured the entire encrypted session, then later steals (or factors) your private key they still won’t be able to read the encrypted traffic because they can’t recover the ephemeral key because it wasn’t sent over the wire in the first place

      • Turun@feddit.de
        link
        fedilink
        arrow-up
        2
        ·
        8 months ago

        Yes, that’s why https needs certificates (and sometimes shows a broken lock) and why you need to accept the fingerprint when first connecting to a server via ssh.

        • mitchty@lemmy.sdf.org
          link
          fedilink
          arrow-up
          3
          ·
          edit-2
          8 months ago

          Accepting ssh key fingerprints on first ssh is a bad practice. Ssh ca’s and or sshfp are around and have been for decades. Accepting random host keys is like trusting random self signed ssl certificates.

          Use ssh ca’s for user and host keys so you can revoke and rekey hosts without having to update authorized keys. And then you can revoke access to hosts for users as well and much more.

    • atzanteol@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      3
      arrow-down
      1
      ·
      8 months ago

      If you know the private key it is trivial to calculate the public key, but the reverse isn’t true

      I didn’t believe this is true… In public/private key crypto there is no such thing as “private” and “public”. They’re just a key pair. You choose to make one public by sharing it and the other private by not sharing it.

      • kevincox@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        8 months ago

        It depends on the cryptosystem. The private and public halves of the pair are often not symmetrical and often have overlap.

        The parent is likely confused because in most situations the “private key file” will also contain all of the public key. Whether by necessity or for convenience.

      • RegalPotoo@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        ·
        8 months ago

        Ok, semantics - for RSA you generate a private key, then derive the public key from that private key, and you could publicly post your private key if you wanted to. “Public” and “private” are just names.

    • Turun@feddit.de
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      8 months ago

      Just a nitpick:

      If you know the private key it is trivial to calculate the public key, but the reverse isn’t true

      The public key and the private key are just two big prime numbers. The “trivial to compute” part only works once more information has been shared over the network, like it happens during key exchange. If you were to swap the prime number before initiating any contact it would work the same way.

      Edit: I probably confused different encryption concepts

      • RegalPotoo@lemmy.world
        link
        fedilink
        English
        arrow-up
        2
        ·
        8 months ago

        In RSA, the private key is a pair of big semi-primes, and the public key is derived from those numbers. I think you are confusing DHKE and RSA with your other points, the private key is never transmitted over the network. For TLS you typically use an asymmetric crypto system to validate identities and encrypt the key exchange to prevent person-in-the-middle, but the key that is agreed using that process is a symmetric key for AES or similar, but that is specific to TLS.

        Also, there are other asymmetric systems that don’t use primes at all - eliptic curve crypto is based on completely different math

    • Scubus@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      8 months ago

      But how does the encryption work if you have the public key? Since your computer knows how to encrypt the data with the public key, couldn’t you use that same public key to run that algorithm in reverse? If not, since the public and private keys are not the same, how does the private key go about decrypting that data?

      • RegalPotoo@lemmy.world
        link
        fedilink
        English
        arrow-up
        2
        ·
        8 months ago

        The actual math is way beyond me, but the algorithm is “one way” - it exploits the fact that given two prime numbers (ie, the private key) it is trivial to multiply them together, but if you only know the result (ie, the public key) it is computationally very expensive to determine the original prime factors. If you pick big enough numbers, it becomes effectively impossible to undo the multiplication

      • slazer2au@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        ·
        8 months ago

        No. The encryption methods are designed in a way that using a public key will not decrypt the message.

  • slazer2au@lemmy.world
    link
    fedilink
    English
    arrow-up
    25
    ·
    8 months ago

    Why are they sharing private keys?

    The point of the system is you share public key so others can encrypt data and the you use your private key do decrypt it.

  • CyberSeeker
    link
    fedilink
    arrow-up
    19
    ·
    8 months ago

    For encryption, the client and server need to share their private keys.

    This is incorrect, for asymmetric (public-private) encryption. You never, ever share the private key, hence the name.

    The private key is only used on your system for local decryption (someone sent a message encrypted with your public key) or for digital signature (you sign a document with your private key, which can be validated by anyone with your public key).

    For the server, they are signing their handshake request with a certificate issued by a known certificate authority (aka, CA, a trusted third party). This prevents a man-in-the-middle attack, as long as you trust the CA.

    The current gap is in inconsistent implementation of Organization Validation/Extended Validation (OV/EV), where an issuer will first validate that domains are legitimate for a registered business. This is to help prevent phishing domains, who will be operating with TLS, but on a near-name match domain (www.app1e.com or www.apple.zip instead of www.apple.com). Even this isn’t perfect, as business names are typically only unique within the country/province/state that issues the business license, or needed to be enforced by trademark, so at the end of the day, you still need to put some trust in the CA.

  • nomad@infosec.pub
    link
    fedilink
    arrow-up
    12
    ·
    8 months ago

    Great question: it’s called asymmetric cryptography for a reason.

    The key pair is built by the person wanting to receive private messages. The public part is made public an can be used to encrypt messages to the owner of the private key. Without it you cant read the contents.

    As this method is not really performance friendly, thus method is usually used to establish a symmetric encryption key for both parties that can’t be intercepted this way.

    In fact: if the attacker can switch out the public key for his own during transfer, he can use it to decrypt the messages and relay them to the first party, thereby revealing all the messages. This is called a man-in-the-middle attack. This is why authentication of the key is so important. This is usually done via a trusted third party.

    Sorry to be so short, I hope I have given you enough words to Google for further reading.

  • kevincox@lemmy.ml
    link
    fedilink
    arrow-up
    10
    ·
    edit-2
    8 months ago

    Great question. Modern encryption schemes are usually composed of a handful of primitives. If we consider something like HTTPS it uses both asymmetric and symmetric parts.

    Asymmetric encryption is the “magic” that you are missing. Asymmetric encryption algorithms create a keypair, one half of this is the “public key” which can be used to encrypt messages that can only be decrypted by the “private key”. This means that even if the public key is public (as the name suggests) the messages can’t be decrypted without the private key.

    You can think of this as giving someone an open padlock. They can put something inside a box and lock it using the padlock, but they can’t open it without your key.

    Using this you could come up with a very simple protocol for establishing a secure channel:

    1. The server sends you their public key, along with a certificate that proves that it belongs to them.
    2. The client then uses this public key to encrypt a key for symmetric encryption.
    3. The client sends this encrypted key to the server.
    4. The server decrypts the key.
    5. Now the client and server can both use the shared key to communicate.

    (Note: There are many missing features from this system, but I think it illustrates the point. Don’t design your own crypto.)

    • pivot_root@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      8 months ago

      Adding on to your TLDR: There’s also asymmetric cryptography based on elliptic curves, so it’s not always an exponent of two massive primes.

  • Sagar Acharya@sopuli.xyz
    link
    fedilink
    arrow-up
    1
    arrow-down
    5
    ·
    8 months ago

    Certifying Authority ensures secure connection establishment. If CA is corrupt, your system will work.

      • lad@programming.dev
        link
        fedilink
        arrow-up
        1
        arrow-down
        2
        ·
        8 months ago

        A bit relevant, because even without sharing private keys Diffie-Hellman without prior knowledge will allow for a MitM attack.

        But the answer is too short to make sense of without knowing that, I think