I’ve been trying to get my head around this and I’ve watched a few videos but they don’t seem to specifically answer my question.

According to what I’ve found online, messages encrypted with a public key can only be decrypted with a private key. But in practice, how is that possible?

Surely a public key contains a set of instructions, and anyone could just run those instructions in reverse to decrypt a message? If everything you need to encrypt a message is stored within a public key, then how is it a one-way process?

It’s likely that I’m misunderstanding a core element of this!

  • TheBananaKing@lemmy.world
    link
    fedilink
    arrow-up
    98
    ·
    7 months ago

    If you turn a sausage machine backwards, you don’t get a pig coming out the top.

    If I add a dozen numbers together, there’s only one total.

    But if I only have that total, there’s no way to tell what the original dozen numbers were.

    Same kind of principle.

    You can brute-force it, but when it comes to the product of stupendously large primes, it would take until the heat death of the universe to do so, by which time you probably don’t care any more.

  • friend_of_satan@lemmy.world
    link
    fedilink
    English
    arrow-up
    41
    arrow-down
    2
    ·
    edit-2
    7 months ago

    Public key cryptography is a bit like one of those blue public mailboxes. There is an input slot where anybody can drop something in, but only people with the private key can open the door to get things out. You can’t run that input in reverse because gravity and the mechanics of the slot do not allow that to happen. The same thing exists mathematically for public key cryptography.

  • Treczoks@lemmy.world
    link
    fedilink
    arrow-up
    31
    ·
    7 months ago

    Basically, asymmetric key cryptography is based on the idea that some algorithms are one-way roads. Or trap doors - falling down is easy, climbing up is much, much harder.

    For a simple example, take adding numbers. I’ve got a thousand numbers, I add them up, and hand you the sum. Will you be able to find the thousand numbers I have from that sum? Probably not. The math involved in the actual cryptography is a bit more complex, but the principle holds.

  • GamingChairModel@lemmy.world
    link
    fedilink
    arrow-up
    20
    ·
    7 months ago

    Look at the Diffie Helman scheme, with the example used in the Wikipedia page.

    • Alice and Bob agree in public, for everyone to see, that they’re gonna start with p=23 and g=5.
    • Alice has a secret key 4, and doesn’t tell anyone (not even Bob). She plugs her secret into the formula g^secret mod p, or 5^4 mod 23. 5^4 is 625, and dividing 625 into 23 gives a remainder of 4. So she tells Bob in public that she derived the number 4 from her secret.
    • Bob has a secret key of 3, does the same thing, and calculates 5^3 mod 23, which results in the number of 10, tells Alice.

    The magic of this scheme is that taking each side’s result and applying the same secret gets to the same final result. 10^4 mod 23 turns out to be the exact same number as 4^3 mod 23. So both sides get to the secret shared key 18, without disclosing that their secret numbers were 4 and 3, respectively.

    But if you try to drive the secret key from the information publicly exchanged, you’ll basically have to try each number until you get to the right one. It’s inefficient, and basically impossible to do once you’re using very large integers (300+ digits long).

  • Successful_Try543@feddit.de
    link
    fedilink
    arrow-up
    22
    arrow-down
    2
    ·
    edit-2
    7 months ago

    AfaIk, yes, in principle one could construct the private key from the public key. However, given the current computational power, it would in practice take forever and a day, if the encryption algorithm is sufficiently strong, see e.g. the Wikipedia article on RSA

    • OmegaMouse@pawb.socialOP
      link
      fedilink
      arrow-up
      3
      ·
      7 months ago

      But say (simplying greatly) the public key tells my computer to multiply my text by a prime number

      If the prime number is already known from the public key, then why is any computation required? To decrypt it can’t I (or anyone else) just divide by the prime? Even with a significantly more complex calculation, can’t you just work the steps back in reverse using the instructions from the public key?

      • philoko@ani.social
        link
        fedilink
        arrow-up
        24
        ·
        7 months ago

        As an oversimplification, RSA works by taking two huge prime numbers (the private key) and multiplying them together (the public key). You could get the private key by factoring the public key but the best way to factor a prime number is to basically just try every combination with a few tricks sprinkled in until something works which could take millions of years with modern computers. You can hoard everyone’s public key until computers can crack it in a reasonable amount of time but everyone involved will be long gone by then.

        The whole point of private and public keys is to have an operation that’s easy to do one way but would take so long to reverse that it’s virtually impossible. There’s nothing stopping you trying except the time and effort involved.

        • OmegaMouse@pawb.socialOP
          link
          fedilink
          arrow-up
          6
          ·
          edit-2
          7 months ago

          Ah I think of sort of get it!

          The public key is used within a function by the person sending the message, and even someone that knew the function and the public key wouldn’t be able to decrypt the message, because doing so would require knowledge of the original prime numbers which they couldn’t work out unless a computer spend years factoring the public key.

          My only other bit of confusion:

          • If someone used a public key to encrypt the message “Hello”, maybe it would spit out something like Gh5bsKjbi4
          • If someone else sent the exact same message I assume the outcome would also be identical, and therefore it would be possible by using common phrases to work out what was sent? I could type messages like Hi, Goodbye, Hola until I got to ‘Hello’ and realised it was the same output.
          • However I assume that a message like ‘Hello, how are you?’ would result in a completely different output (despite Hello appearing in both) and thus trying to work out any messages in a brute force way like this would be pointless.
          • hikaru755@feddit.de
            link
            fedilink
            arrow-up
            6
            ·
            7 months ago

            Yup, you got it. Even the solution to your confusion. Good encryption algorithms are set up so that even the smallest possible change in the input (a single flipped bit) will produce a completely different result. So yeah, if you have just a small set of exact possible messages that could be sent, you can find out which one it was by encrypting it yourself and comparing your result to what was sent. But there is a super easy protection against this - just add some random data to the end of the message before encrypting it. The more, the harder it will be to crack.

          • mumblerfish@lemmy.world
            link
            fedilink
            arrow-up
            5
            ·
            7 months ago

            Well, you use “padding” to solve those things. Like if you type “hello”, your implementation of the whole algorithm should do something like: take the string, add some random string that is tagged in some way, and then encrypt. At decryption, you get a string with some random stuff in it, but you filter the tag and return only the message. Like “hello” -> “[trash]kfkidkeb[/trash] hello”, add and remove the “[trash]” block, before encryption and after decryption, respectively

          • howrar@lemmy.ca
            link
            fedilink
            arrow-up
            4
            arrow-down
            1
            ·
            7 months ago

            The same word will be encrypted the same way each time, and that’s actually the basis of one of the known attacks on LLMs like ChatGPT. They send responses back one “word” at a time, so someone who’s snooping can easily figure out what it’s saying from the encrypted messages. The exploit doesn’t affect Bard because it sends larger chunks of text at a time.

      • Thorry84@feddit.nl
        link
        fedilink
        arrow-up
        11
        ·
        7 months ago

        Encryption uses pairs of primes, where you know the resulting number, but not the primes used to comprise that number. You can calculate the result given the primes very quickly, but given the result it’s very slow to figure out what the primes were. This asymmetry is the key to this kind of encryption.

        Given enough computational power you can do it, that’s why we’ve moved on to more complex algorithms and bigger keys throughout the years to keep up.

      • TowardsTheFuture@lemmy.zip
        link
        fedilink
        English
        arrow-up
        6
        ·
        edit-2
        7 months ago

        Look at it more like this. Say I tell you to pick any number, square it, and then add 5. Say I did this and my answer was 41. What was my original number? 90% of people will answer 6, but my answer was -6. Now only doing that once isn’t hard, but basically it’s because the inverse of the function is not a function that I can’t always get back to it. Now do that a few thousand different ways in the same problem and good fucking luck.

        While it’s not what is happening as it’s using pairs of primes, it’s an easy way to explain there’s not always a way to get to something from an equation.

  • NeoNachtwaechter@lemmy.world
    link
    fedilink
    arrow-up
    16
    arrow-down
    1
    ·
    edit-2
    7 months ago

    Surely a public key contains a set of instructions

    Not exactly. But that is not the point.

    and anyone could just run those instructions in reverse to decrypt a message?

    No, you can’t, and that’s exactly the great thing about the invention of public key crypto (or asymmetric encryption in general).

    You can encrypt something with a private key, and then you need the public key to decrypt it. That is used for a digital signature.

    You can encrypt something with a public key, and then you need the private key to decrypt it. That is used for a private message to one person.

  • rufus@discuss.tchncs.de
    link
    fedilink
    arrow-up
    13
    arrow-down
    1
    ·
    edit-2
    7 months ago

    It think the common analogy is a padlock and a key. One party gets the padlock and the other one the key. Now with the padlock you can just lock the box but not open it. And with the key you can just unlock and open the box. That’s assymetric. You hand out padlocks (your public key) to everyone, but keep the key (your private key).

    The maths behind that is a bit difficult to explain but not that difficult. I think it’s about one-way functions that are easy to calculate in one direction and impossible to solve the other way around. There are several ways to do it. Like the old approaches with prime numbers. It’s easy to multiply two prime numbers. But given an arbitrary large number, it’s difficult to tell which prime numbers it consists of.

    https://www.baeldung.com/cs/prime-numbers-cryptography

    • OmegaMouse@pawb.socialOP
      link
      fedilink
      arrow-up
      3
      ·
      7 months ago

      So using the formula in that guide, you get a numerical value for O. But surely someone else could follow the same process and also get the same answer? Unless the primes change each time? But then how would the sender and receiver know the way in which the values change?

      • rufus@discuss.tchncs.de
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        7 months ago

        I’m not sure I get your question… Sure other people can also follow the same process and encrypt stuff to you. They can also do the calculations with your private key and arrive at the same result, sure. But the calculation involves your private key. Your secret. If that’s known to someone, they can do the calculations. In the example you need to keep the “53” a secret and give the “221” to other people. Everyone with the “53” can decrypt. Everyone with the “221” can encrypt. It’s just with the “221” you can’t decrypt. That part of the calculation needs people to put in the other number.

        • OmegaMouse@pawb.socialOP
          link
          fedilink
          arrow-up
          3
          ·
          7 months ago

          I explained it poorly - what I mean to say is, two people trying to send the message ‘Hello’ for example both using the same public key would get the same output. So if you had a simple message like that, someone could work out by checking every word in the dictionary what your message was by checking if the output matched.

          But I guess it’s a bit of a moot point - it’s unlikely that an encrypted message would ever be so simple. It could just as easily be much longer, and therefore basically impossible to guess the plaintext.

          • mumblerfish@lemmy.world
            link
            fedilink
            arrow-up
            5
            ·
            7 months ago

            No, it can be very important. As I answered in another comment, its called padding https://en.m.wikipedia.org/wiki/Padding_(cryptography). And to see that it is imortant say you encrypt your easy to remember password in an encrypted file. Now if your attack was possible, having your public key, you could just generate the passwords and encrypt them to figure out your password. Much easier than trying to find your key. Using forms of padding, that does not work.

          • rufus@discuss.tchncs.de
            link
            fedilink
            arrow-up
            4
            ·
            edit-2
            7 months ago

            Ah, that is a really good question. These things happen. People have entire harddisks filled with “rainbow tables” which do these kind of attacks against hash-functions which are supposed to be one-way functions. This way they have terabytes worth of pre-computed hashes for the most common passwords and can immediately tell if one of those passwords is in a database leak.

            For this it needs additional measures. Passwords are augmented with additional random data so people can’t pre-compute the hashes. So it wouldn’t be just ‘Hello’, but ‘Hello’ plus an additional (random) “salt” that gets fed into the one-way function so it can’t be brute forced.

            PGP for example uses both symmetric cryptography and asymmetric cryptography. The actual message is encrypted with symmetric encryption and the key to that is encrypted with asymmetric encryption. Unfortunately it’s been a while since I last read a book on cryptography. I think they did that because symmetric cryptography is way faster. But things like that could also prevent such attacks. If you use the asymmetric encryption just for decrypting the other randomly chosen key which encrypts the actual message… There is no way to guess that. You’d have to check not just a small dictionary or know the plaintext, but check every random number in existence.

            It’s not always obvious to the layman what kinds of attacks are possible with the crypto algorithms. They definitely need to protect against such scenarios or they’re worthless for that kind of use. There are “known plaintext attacks”. Usually people don’t want anyone even able to prove that you sent a certain message. And an algorithm also isn’t good if you can learn something about the secret key if you have access to both a ciphertext and plaintext (the other variables in the equation). I think this was part of how they cracked the supposedly secure enigma machines of the Nazis. A proper modern cryptography algorithm protects against any of that. Sometimes by combining several things.

            Edit: Sure and I forgot padding which @mumblerfish said. And appending data additionally helps if you don’t want someone to know the exact length of a message. Or an algorithm sometimes can’t encrypt arbitrary amounts of plaintext but does it in fixed block lengths…

            • OmegaMouse@pawb.socialOP
              link
              fedilink
              arrow-up
              4
              ·
              7 months ago

              Ah thanks for the useful links! Those articles are all quite fascinating. In the plaintext attacks article, I love the tactic mentioned here:

              At Bletchley Park in World War II, strenuous efforts were made to use (and even force the Germans to produce) messages with known plaintext. For example, when cribs were lacking, Bletchley Park would sometimes ask the Royal Air Force to “seed” a particular area in the North Sea with mines (a process that came to be known as gardening, by obvious reference). The Enigma messages that were soon sent out would most likely contain the name of the area or the harbour threatened by the mines

              • rufus@discuss.tchncs.de
                link
                fedilink
                arrow-up
                4
                ·
                edit-2
                7 months ago

                Both cryptography and that part of history are fascinating topics. I can also recommend watching “The Imitation Game” with Benedict Cumberbatch starring as Alan Turing… I mean it’s just a movie and skips lots of the interesting stuff and details. YMMV.

                It’s the beginning of computers. And I think especially that time has some interesting stories, discoveries/inventions and personas. There is also the history and role of women in computing which I think is something more people should know about and it’s related to that. After that we needed secrecy in the cold war. I think public key cryptography hasn’t been around until the 1970s. There had been export regulations on cryptography until after I was born. And modern encryption algorithms like AES are from the 1990s. Nowadays everyone and their grandma relies on the availability of secure communications.

                I think I spent some nights jumping from Wikipedia article to Wikipedia article and reading all of that.