Chapter 32 of 41

Cryptography Basics

Whenever we send sensitive information over a network, we don't want everyone who can access the communication to understand it.

For example, imagine sending:

My password is 123456

over a network. If the information is not properly protected, an attacker may be able to read it.

Cryptography provides mathematical techniques for protecting information and communication.

Cryptography is the practice of using mathematical algorithms and keys to protect information from unauthorized access or modification.

In simple words:

Cryptography helps us keep data private, authentic, and protected from tampering.


Why Do We Need Cryptography?

Cryptography is used to provide important security properties such as:

  • Confidentiality → Prevent unauthorized people from reading data

  • Integrity → Detect unauthorized changes to data

  • Authentication → Verify the identity of a party or the origin of data

  • Non-repudiation → In certain systems, provide evidence supporting who signed data

For example, when you visit a website using HTTPS, cryptographic techniques help protect the communication between your browser and the server.


Basic Cryptography Terms

Before learning different cryptographic techniques, let's understand a few basic terms.

Term

Meaning

Plaintext

Original readable data

Ciphertext

Data after encryption

Encryption

Converting plaintext into protected ciphertext

Decryption

Recovering plaintext from ciphertext

Key

Secret or controlled value used by a cryptographic algorithm

Cipher

Algorithm used to transform data

The basic process is:

Plaintext
    ↓
Encryption + Key
    ↓
Ciphertext
    ↓
Decryption + Key
    ↓
Plaintext

Encryption

Encryption converts readable information into ciphertext that should be unintelligible to anyone who does not have the required key.

For example:

Plaintext:
HELLO

      ↓ Encryption

Ciphertext:
X7@p9#L

The actual ciphertext produced by a modern algorithm will depend on the algorithm, key, and other inputs.

The important idea is that someone who intercepts the ciphertext should not be able to practically recover the plaintext without the necessary key.


Decryption

Decryption is the reverse process.

It converts ciphertext back into the original plaintext using the appropriate cryptographic key.

Ciphertext
    ↓
Decryption + Key
    ↓
Plaintext

For example:

Encrypted Data
      ↓
Correct Key
      ↓
Original Data

Symmetric Encryption

In symmetric encryption, the same secret key is used for both encryption and decryption.

             Same Secret Key
                  ↓
Plaintext → Encryption → Ciphertext
                         ↓
                    Decryption
                         ↓
                     Plaintext

The sender and receiver must both have access to the secret key.

Example

Suppose Alice and Bob share a secret key.

Alice
  │
  │ Plaintext
  ↓
Encryption + Secret Key
  ↓
Ciphertext
  ↓
Bob
  ↓
Decryption + Secret Key
  ↓
Plaintext

Common modern symmetric algorithms include:

  • AES

  • ChaCha20

Advantage

Symmetric encryption is generally fast and efficient, making it suitable for protecting large amounts of data.

Challenge

The key must be shared securely. If an attacker obtains the secret key, they may be able to decrypt protected data.


Asymmetric Cryptography

Asymmetric cryptography, also called public-key cryptography, uses a pair of mathematically related keys:

  • Public key

  • Private key

The public key can generally be shared, while the private key must be kept secret.

Public Key  → Can be shared
Private Key → Must remain secret

The two keys have different roles depending on the cryptographic operation.


Encryption With Public-Key Cryptography

A simplified example:

Alice
  ↓
Encrypt using Bob's Public Key
  ↓
Ciphertext
  ↓
Bob
  ↓
Decrypt using Bob's Private Key
  ↓
Original Message

Bob can share his public key with others, but only Bob should possess the corresponding private key.

In practice, modern systems generally use public-key techniques to establish keys or protect small pieces of information rather than encrypting large amounts of application data directly.


Digital Signatures

Asymmetric cryptography can also be used for digital signatures.

A digital signature helps provide:

  • Authentication of the signer

  • Integrity of the signed data

  • Evidence that a particular private key was used to create the signature

A simplified process is:

Message
   ↓
Create Hash
   ↓
Sign with Private Key
   ↓
Digital Signature

The recipient can use the corresponding public key to verify the signature.

Message + Signature
        ↓
Verify with Public Key
        ↓
Valid / Invalid

Digital signatures are widely used in software distribution, certificates, secure communication, and many other systems.


Hashing

Hashing is another important cryptographic concept.

A cryptographic hash function takes input data of arbitrary length and produces a fixed-size output called a hash or digest.

Input Data
    ↓
Hash Function
    ↓
Fixed-Size Hash

For example:

"Hello"
   ↓
Hash Function
   ↓
Some fixed-length digest

A good cryptographic hash function is designed so that it is computationally difficult to find another input that produces the same hash.

Common modern cryptographic hash functions include:

  • SHA-256

  • SHA-3


Hashing vs Encryption

These two are often confused.

Hashing

Encryption

Produces a digest

Produces ciphertext

Designed to be one-way

Designed to be reversible with the correct key

No decryption process

Can be decrypted

Used for integrity and other applications

Used mainly for confidentiality

Example: SHA-256

Example: AES

For example:

Encryption:
Data → Encryption → Ciphertext → Decryption → Data

Hashing:
Data → Hash Function → Digest

You don't normally "decrypt" a hash.


Cryptography in HTTPS

When you visit a website using HTTPS, cryptography is working behind the scenes.

A simplified view is:

Browser
   ↓
TLS Handshake
   ↓
Authentication + Key Establishment
   ↓
Secure Session
   ↓
Encrypted HTTP Data

Modern TLS uses a combination of cryptographic techniques.

For example:

  • Public-key cryptography helps authenticate the server and establish cryptographic parameters.

  • Symmetric encryption efficiently protects application data after the secure session is established.

  • Hash-based mechanisms help provide integrity and are used in various parts of the protocol.

This combination gives us both security and efficiency.


Real-Life Example

Suppose you log in to an online banking website.

Your browser establishes an HTTPS connection with the server.

Your Browser
      ↓
      TLS
      ↓
Encrypted Communication
      ↓
Bank Server

The cryptographic mechanisms used by TLS help protect your communication against eavesdropping and unauthorized modification while it travels across the network.

This is why cryptography is such an important part of modern network security.


Encryption vs Encoding

Another common confusion is between encryption and encoding.

Encryption

Designed to protect information using cryptographic keys.

Data → Encryption → Protected Ciphertext

Encoding

Changes data into another representation so it can be stored or transmitted in a particular format.

For example:

Text → Base64 → Encoded Text

Base64 is not encryption. Anyone can decode Base64 without a secret key.


Cryptography vs Cryptanalysis

Cryptography focuses on designing and using techniques to protect information.

Cryptanalysis focuses on analyzing cryptographic systems and attempting to recover protected information or find weaknesses without the intended secret.

Together, these areas form an important part of modern cryptology.

Conclusion

Cryptography is the foundation of many modern security systems. It uses mathematical algorithms and keys to protect information and communication.

The most important concepts to remember are:

  • Encryption → Protects confidentiality

  • Decryption → Recovers encrypted data

  • Symmetric Cryptography → Uses a shared secret key

  • Asymmetric Cryptography → Uses public and private keys

  • Digital Signature → Helps provide authenticity and integrity

  • Hashing → Produces a fixed-size cryptographic digest

  • TLS → Uses cryptography to protect network communication

The easiest way to remember the basics is:

Symmetric → Same secret key

Asymmetric → Public key + Private key

Hashing → One-way digest

Digital Signature → Sign with private key, verify with public key

Once these concepts are clear, understanding HTTPS, TLS, digital certificates, authentication, and secure network communication becomes much easier.