IPsec Explained: How the Internet’s Traffic Is Protected

IPsec explained in plain terms means understanding the security built into the internet itself, which protects traffic before applications even see it. The central question is how two networks can talk across the public internet as if they were connected by a private cable, and the answer is a set of protocols that encrypt and authenticate every packet at the network layer.

IPsec, short for Internet Protocol Security, is a suite of protocols that secures IP traffic, defined in RFC 4301 and its companions. It works at the network layer, which means it protects all traffic between two machines, whatever application produces it, without the applications needing to know anything about it.

The idea is simple: agree on keys, then protect packets. Two gateways negotiate a security association, which is an agreement on algorithms and keys, and then every packet between them is encrypted, authenticated, or both, so that anyone watching the wire sees only scrambled data. This article explains what IPsec is, how it works, and where it is used.

Key takeaways

  • IPsec protects IP traffic at the network layer, for all applications.
  • ESP encrypts packets; AH authenticates them without encrypting.
  • IKE negotiates keys and security associations between peers.
  • Tunnel mode protects whole packets; transport mode protects only the payload.
  • It is the foundation of most site-to-site VPNs.

What IPsec is

IPsec has two main protocols. ESP, the Encapsulating Security Payload, encrypts the packet and checks that it has not been changed, and it is the protocol used in almost every deployment. AH, the Authentication Header, only proves the packet is genuine and unaltered, without encrypting it, and it is rarely used today, since encryption is almost always wanted as well.

The other half of IPsec is IKE, the Internet Key Exchange, now in its second version, IKEv2. IKE is the protocol that lets two peers authenticate each other, agree on encryption algorithms, and exchange keys, using certificates or a pre-shared key, over UDP ports 500 and 4500. Without IKE, keys would have to be configured by hand on both sides.

The agreement IKE produces is called a security association, or SA. An SA records the algorithms, the keys, the lifetime, and an identifier called the SPI, the Security Parameters Index, and each direction of traffic has its own SA. When an SA expires, IKE negotiates a fresh one, so keys are regularly replaced without interrupting traffic. Our guide to TLS explains the encryption that protects web traffic at a higher layer, and our guide to WireGuard explains the simpler, newer VPN protocol often compared with IPsec.

How IPsec works

A connection starts with IKE. In the first phase, the two peers authenticate each other and agree on how to protect the IKE conversation itself, and in the second phase they negotiate the SAs that will protect the real traffic, including which encryption algorithm, such as AES, and which integrity algorithm to use.

Once the SAs exist, data flows through ESP. In tunnel mode, used between gateways, the whole original packet is encrypted and wrapped in a new IP packet addressed to the far gateway, which unwraps it and forwards the original packet inside its own network. In transport mode, used between two hosts, only the packet’s payload is encrypted, and the original IP header stays visible.

Every protected packet carries a sequence number, which stops replay attacks: an attacker who records a packet and sends it again is detected, because the number has already been seen. The receiving side also checks the integrity code on each packet, and silently drops any packet that fails, whether it was altered, corrupted, or forged.

NAT traversal is one of IPsec’s practical challenges. Because IPsec protects the packet headers that network address translation changes, the protocols can break when a NAT device sits in the path. The usual fix is to wrap ESP packets inside UDP, on port 4500, so that NAT devices can translate the outer packet without touching the protected one inside.

Keys and lifetimes are managed automatically. IKE rekeys the SAs before they expire, typically after hours of use or gigabytes of traffic, and dead peer detection notices when the far side has vanished, so that a failed tunnel is torn down and rebuilt rather than silently carrying no traffic.

A laptop on a dark desk with its screen showing a glowing green tunnel carrying encrypted packets between two networks
A laptop showing a glowing green tunnel carrying encrypted packets between two networks shows how IPsec works: ordinary packets in, protected packets out.

Comparing IPsec with other VPN security

IPsec is one of several ways to protect traffic, and each suits a different need. TLS protects web traffic at the application layer, WireGuard is a simpler VPN protocol, and OpenVPN is the older flexible option, but each has its own trade-offs.

The table below sets out the main contrasts between the leading approaches.

Aspect IPsec TLS WireGuard
Layer Network, IP layer Transport, above TCP Network, in the kernel
Protects All traffic between hosts One application at a time All traffic in the tunnel
Setup Complex, many options Simple, certificates Very simple, few options
Speed Fast, hardware support Fast Very fast, small code
Best for Site-to-site VPNs Web and app traffic Simple, modern VPNs

The practical result is that IPsec is the right choice for connecting whole networks across the internet, TLS is the choice for protecting a single application such as a website, and WireGuard wins where simplicity and speed matter most.

Where IPsec is used

Site-to-site VPNs are the largest use. A company with offices in several cities connects them with IPsec tunnels, so that traffic between offices is encrypted as it crosses the internet, and each office network behaves as if it were part of one private network.

Remote access VPNs use IPsec to connect employees to the office. A worker’s laptop establishes an IPsec tunnel to the company gateway, and all of the laptop’s office traffic travels through it, protected from anyone watching the hotel or cafe network.

Cloud connections use IPsec to link a data centre to a public cloud. A tunnel between the company’s gateway and the cloud provider’s gateway extends the private network into the cloud, so that virtual machines there are reachable as if they were local.

Telecoms use IPsec to protect the traffic between their own systems. Mobile networks encrypt the links between base stations and the core, and between operators, so that calls and data are protected even though they travel over shared infrastructure.

Routers and firewalls use IPsec to protect management traffic. Administrators connect to devices over IPsec, or devices send logs and backups through tunnels, so that the management plane is not exposed to the open internet.

Strengths and limits

The greatest strength of IPsec is its position in the stack. Because it works at the network layer, it protects every application at once, with no changes to the software, and it is supported by every operating system, router, and firewall, which is why it became the standard for site-to-site VPNs.

It is also a strength that IPsec is a mature, well-tested standard. Its algorithms, such as AES, are widely trusted, its key exchange is flexible, and decades of deployment have found and fixed its weaknesses, which is why regulated industries still rely on it.

The limits are real, though. IPsec is complex, with many options that must match on both sides, and a misconfiguration can leave a tunnel down or, worse, up but unprotected. Its complexity is also why simpler protocols such as WireGuard have grown popular for new deployments.

IPsec checklist

  • Use IKEv2, not the older IKEv1, for new tunnels.
  • Use AES encryption and a strong integrity algorithm.
  • Authenticate with certificates where you can, not pre-shared keys.
  • Keep pre-shared keys long, random, and different per tunnel.
  • Enable NAT traversal where a NAT device sits in the path.
  • Set SA lifetimes short enough to rekey regularly.
  • Log tunnel failures, since a down tunnel can look like a network outage.
  • Test failover by stopping one gateway, and check traffic recovers.
  • Document each tunnel’s purpose, peers, and networks.

Frequently asked questions

What is IPsec in simple terms?

IPsec is a set of rules that encrypts internet traffic between two points. Two gateways agree on keys, and then every packet between them is scrambled, so that anyone watching the internet in between sees only unreadable data.

What is the difference between AH and ESP?

AH proves a packet is genuine and unaltered, but does not encrypt it, while ESP both encrypts the packet and proves it is genuine. ESP is used in almost every deployment, since encryption is nearly always wanted.

What is IKE?

IKE is the protocol that sets up IPsec. It authenticates the two peers, agrees on encryption algorithms, and exchanges the keys that protect the traffic, and it rekeys them automatically before they expire.

What is tunnel mode?

It is the mode where the whole original packet is encrypted and wrapped in a new packet addressed to the far gateway. It is used between networks, so that traffic inside each network stays private as it crosses the internet.

Is IPsec the same as a VPN?

Not quite. A VPN is the private network that results, and IPsec is the most common way to build one. Other protocols, such as WireGuard and OpenVPN, can build VPNs too, using different methods.

Why is IPsec considered complex?

Because it has many options for encryption, authentication, key exchange, and modes, and both sides must agree on all of them. Simpler protocols fix most of those choices, which makes them easier to set up, at the cost of flexibility.

The bottom line

IPsec explained in one line is this: a suite of protocols that encrypts and authenticates internet traffic at the network layer, so that whole networks can talk privately across the public internet.

It is the quiet foundation of corporate VPNs, cloud connections, and telecom security, and although simpler protocols are gaining ground, its maturity and universal support keep it at the centre of network security.

This article is general information about how IPsec works, not advice about securing a particular network.

Latest articles

Scroll to Top