Sonith Manakkat

Algorithms, part 11 of 1211 min read13 visuals

Encryption and digital signatures, the basics

How locks and keys explain encryption, why there are two families (shared-key and public-key), what digital signatures prove, and how HTTPS combines them. Concepts only, no recipes.

  • Encryption hides data with a key
  • signatures prove who sent it and that it was not changed
  • Shared-key encryption is fast, public-key encryption solves sharing keys, and real systems use both
  • Never build your own cryptography: use vetted libraries and standard protocols

Share on LinkedIn

Share
Reading level

Encryption is the part of computing where algorithms protect secrets. It is also the part where good intentions go most badly wrong, because a design that looks clever can be broken by someone who knows the field. This article explains the ideas with locks and keys, so you can read about security with understanding. It is deliberately about concepts: it does not teach you to build anything, and it should not be used that way.

Locks, keys and three words

PlaintextKey plusmethodCiphertextSame ormatching keyPlaintextagain
Encryption scrambles readable data into unreadable data, and a key reverses it.
Expert viewKerckhoffs's principle

A long-standing rule in the field, known as Kerckhoffs's principle, says a system should stay secure even if everything about it is public except the key. In practice this means the method is published and examined by many experts, and only the key is secret. Secret, home-made methods ("security through obscurity") have a poor record, because the people who most need to examine them never get the chance.

Symmetric encryption: one shared key

Symmetric (shared key)

  • One secret key locks and unlocks
  • Fast, good for large amounts of data
  • Both sides must somehow share the key safely

Public-key (key pair)

  • A public lock anyone can close
  • Only the owner's private key opens it
  • Slower, so used for small jobs such as protecting keys
Two families of encryption.
Expert viewAuthenticated encryption, in words

Hiding data is not enough: an attacker who cannot read a message might still change it. Modern designs therefore use authenticated encryption, which both hides the data and attaches a check so that any tampering is detected when the message is opened. This is one more reason to use a well-tested library mode and not assemble pieces by hand: the order and combination of the pieces matters, and mistakes are easy to make and hard to see.

Public-key encryption: a lock anyone can close

  1. Make a key pairYou keep the private key and publish the public key
  2. Others lock with itAnyone can snap the public padlock shut on a message
  3. Only you unlockThe private key never leaves your hands
  4. No shared secret neededThe key-sharing problem is solved
The padlock idea.
Expert viewWhy not use it for everything?

Public-key methods rest on mathematical problems believed to be hard to reverse, and they are much slower than symmetric methods. So they are typically used for small jobs: agreeing on a shared key, or protecting a short key, and for signatures. Standards in this area evolve, including work on methods designed for a future where much larger computers might exist. Follow current guidance from a trusted standards body and do not rely on summaries like this one for choices.

Hybrid: both together

The slow method is used briefly, to set up a secret that two sides share. The fast method then does the heavy lifting. Nearly every secure connection you use is built this way.

Hashing: a fingerprint, not a lock

Hash

  • Turns data into a short fixed-size fingerprint
  • Not meant to be reversed
  • Any small change gives a very different fingerprint

Encryption

  • Turns data into a scrambled form
  • Designed to be reversed with the key
  • The goal is secrecy
Hashing and encryption are different jobs.

Hashing was covered in the hashing part of this series. Here it matters for two reasons: it detects changes to data, and it is the right tool for storing passwords, because a service should never need to read your password back.

  1. 1Need to keep the content secret?Yes Encrypt it with a vetted libraryNo keep going
  2. 2Need to prove who made it and that it is unchanged?Yes Sign it with a private keyNo keep going
  3. 3Need to detect changes, or store a password you never read back?Yes Hash it (slow and salted for passwords)No keep going
  4. 4OtherwiseEncoding such as Base64 is none of these: it gives no secrecy

Takeaway. Encrypt to hide, sign to prove, hash to fingerprint.

Which tool for which goal (a rule of thumb).

Digital signatures: proof of who and what

MessageSign withprivate keyAnyonechecks withpublic keyWho sent it,unchanged
A signature is made with the private key and checked with the public key.
SignerCheckerhash messagesign hashmessage + sighash againverify12345
  1. Signer (to itself) The signer computes a hash of the message
  2. Signer (to itself) The private key turns it into a signature
  3. Signer to Checker The message and signature are sent together
  4. Checker (to itself) The checker hashes the message it received
  5. Checker (to itself) The public key checks the signature against that hash

Takeaway. A match proves the key holder signed it and the message is unchanged; it does not hide the message.

Signing and checking: the private key signs, anyone with the public key checks.
Expert viewWhat a signature does and does not give

In outline, the signer computes a hash of the message and uses the private key to produce a signature over it. A verifier recomputes the hash and uses the public key to check the signature. Two properties follow: authenticity (it was made by the holder of the private key) and integrity (the message has not changed since signing). A signature does not hide the message, since signing and encrypting are separate operations, and it only proves that the key was used, not that the key's owner is who you assume. That last gap is why certificates exist.

HTTPS in one picture

BrowserWebsitehellocertificatecheck itagree keysfast and private12345
  1. Browser to Website Browser and site agree on how to talk
  2. Website to Browser (reply) The site shows a certificate as proof of its name
  3. Browser (to itself) The browser checks the signature, the name and the expiry date
  4. Browser to Website Public-key methods set up a shared secret
  5. Browser to Website Data now flows under symmetric encryption

Takeaway. Slow public-key methods set up the secret; fast symmetric encryption carries the data.

A secure connection in outline: prove the name, agree a secret, then use the fast method.
  1. HelloBrowser and site agree on how to talk
  2. CertificateThe site shows proof of its identity
  3. Agree keysPublic-key methods set up a shared secret
  4. Fast and privateData flows under symmetric encryption
What happens when a browser opens a secure page, simplified.
Expert viewForward secrecy, in words

Some key agreement methods use fresh, temporary values for each connection. The benefit, called forward secrecy, is that if a long-term key is stolen later, old recorded conversations still cannot be unlocked with it. Current standards for secure connections favour designs with this property.

Certificates and authorities

CertificateWebsite namePublic keyAuthoritysignatureExpiry date
A certificate ties a public key to a name, vouched for by someone trusted.
Expert viewTrust is a chain

Authorities can sign certificates for other authorities, forming a chain from the site up to a trusted root. A certificate is checked for a valid signature, a matching name and whether it has expired or been revoked. The weakness is human and organisational: you are trusting the authorities, so the system's safety depends on their care and on the software's checks.

Common mistakes

Also avoid hard-coded keys (they leak with the source code) and storing passwords unhashed (keep only slow, salted hashes).

Myth

Base64 hides my data, so it is encrypted

Fact

Base64 is an encoding anyone can reverse, with no key and no secrecy

Myth

I made up my own cipher, so nobody knows how to break it

Fact

Secret methods are weak. Public, widely examined standards are the safe choice

Two ideas that cause real damage.
Expert viewKey management is the hard part

In most real incidents the mathematics held and the handling failed: keys were left in code repositories, shared by email, never rotated or kept next to the data they protected. Good practice means generating keys with trusted tools, storing them in purpose-built secret stores or hardware, limiting who can use them, rotating them, and planning for loss. Treat key handling as the main design problem, not an afterthought.

What encryption does not protect

Contentscrambled while stored or travellingMetadatawho talked to whom, when and how muchThe endpointsmalware or a person at either end can read itHuman behaviourphishing, weak passwords and reuse
Encryption covers the content in transit or at rest, and little else.
Expert viewLimits worth remembering

Encryption protects data from people who cannot get the key. It does not hide the fact that a conversation happened, it does not help when an attacker controls the device that holds the decrypted data, and it does not fix tricking a person into giving access away. Security is a chain of such links, and a strong cipher is only one of them.

Try it by hand: a toy shift

Pick the key 3 and move each letter three places along the alphabet, wrapping from Z to A. The word CAT becomes FDW. To decrypt, shift back by 3.

  1. Choose a short wordFor example DOG
  2. Shift each letter by 3D to G, O to R, G to J
  3. Write the ciphertextGRJ
  4. Hand over only the keyYour friend shifts back by 3
  5. Try breaking itWith only 25 possible keys you can try them all in minutes
A five-minute exercise.

That last step is the lesson: a method with few possible keys fails at once to trial and error, and clever tricks can break far bigger designs. This is why experts design, review and publish the real methods.

Everything can be public except the key, so protect the key, and never invent the lock.

Frequently asked questions

Is a signature the same as encryption?

No. A signature proves who made a message and that it is unchanged; it does not hide the message.

Is hashing a way to hide passwords?

Hashing is used to store passwords without keeping them, but it must be a slow, salted method from a trusted library, not a quick hash.

Can I write my own encryption if I keep it secret?

No. Secret designs are the weakest. Use vetted libraries and standard protocols.

Does the padlock in the browser mean a site is safe?

It means the connection is encrypted and the site proved its name. It does not mean the site's owner is trustworthy.

Back to all articles