# Public-Key Ciphers: Core Functions
> [!Note]
> A **public-key cipher** rests on three mutually dependent algorithms—**key generation**, **encryption** and **decryption**—so that anyone can encrypt with the public key while only the private key holder can decrypt.
In a public-key scheme, the **key-generation algorithm** outputs an asymmetric pair, **kpub** and **kpriv**. Possession is asymmetric: the public component may be broadcast to the world, whereas the private component must remain confidential. Once the keys exist, the **encryption algorithm** takes an arbitrary message _m_ together with **kpub** and produces the **ciphertext** _c_. Only someone holding **kpriv** can feed _c_ into the **decryption algorithm** and retrieve the original message, thereby guaranteeing confidentiality even over an open channel.
> [!Example]
>
> ```
> [k_pub, k_priv] = KeyGen()
> c = E(k_pub, m)
> m = D(c, k_priv)
> ```
The elegance of this triad is that it removes the need for a pre-shared secret: encryption can occur anywhere, yet successful decryption is mathematically bound to the private key. These primitives underpin protocols such as *TLS, PGP* and modern cryptocurrency wallets, where the combination of openness and exclusivity is indispensable.
```mermaid
sequenceDiagram
participant KG as Key Generation
participant Sender
participant Receiver
%% Fase de generació de claus
KG->>KG: [k_pub, k_priv]
KG-->>Sender: k_pub
KG-->>Receiver: k_priv (guardada en privat)
%% Fase d'enviament
Sender->>Sender: c = Encrypt(m, k_pub)
Sender-->>Receiver: c
%% Fase de recepció
Receiver->>Receiver: m = Decrypt(c, k_priv)
```
---
## References