Blog about Web Programming and Electronics
💬 👤
back Back to blog
WEB (HTML, CSS, PHP)

What is Encryption and Why It Matters

Encryption is a process that transforms data into an unreadable form, accessible only to those with the correct decryption key. In a world where data is worth more than gold, encryption represents the foundation of digital privacy and security. Every time you perform online banking, send an email, or store sensitive files in the cloud - encryption works in the background to protect you.

What is Encryption?

Encryption is a method of protecting information where original data (called "plain text") is converted into encrypted form (called "ciphertext"), which is completely incomprehensible without a key. The reverse process of converting ciphertext back to plain text is called decryption.

Key terms:

  1. Plain text: Original, unencrypted data
  2. Ciphertext: Encrypted, unreadable data
  3. Key: Secret value that enables encryption or decryption
  4. Algorithm: Mathematical method of encryption

Types of Encryption

There are two main types of encryption used today:

Type Base Use Case Examples
Symmetric One key for encrypt and decrypt Fast, for large amounts of data AES, ChaCha20
Asymmetric Two keys: public and private Key exchange, digital signatures RSA, ECC

Symmetric Encryption

In symmetric encryption, the same key is used for both locking and unlocking data. This is similar to a physical key for doors - the same key locks and unlocks.

Advantages:

  • Fast and efficient for large files
  • Smaller keys (lower resource usage)

Disadvantages:

  • Key must be secret and secure transmission is challenging
  • Difficulty sharing data with multiple people

Most common algorithms:

  • AES (Advanced Encryption Standard): Gold standard, used by military and governments
  • ChaCha20: Modern alternative to AES, faster on mobile devices
// Example of AES-256 usage in PHP:

$plaintext = "Secret data";
$key = random_bytes(32); // 256-bit key
$iv = random_bytes(16);  // Initialization vector

$encrypted = openssl_encrypt(
    $plaintext,
    'AES-256-CBC',
    $key,
    OPENSSL_RAW_DATA,
    $iv
);

// Store $encrypted and $iv securely
// For decryption you need $key and $iv

Asymmetric Encryption

Asymmetric encryption uses a pair of keys: a public key and a private key. Whatever one key locks, only the other key in the pair can unlock.

How it works:

  1. Generate a key pair: public and private
  2. Give your public key to everyone - people lock messages with it
  3. Keep your private key secret - only you can unlock messages with it

Use cases:

  • HTTPS certificates for secure websites
  • Digital signatures for documents
  • Sending encrypted emails (PGP/GPG)
  • Cryptocurrencies (Bitcoin, Ethereum)
// Example of RSA usage in PHP:

// Generating key pair
$config = [
    "digest_alg" => "sha256",
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
];

$res = openssl_pkey_new($config);
openssl_pkey_export($res, $privateKey);
$publicKey = openssl_pkey_get_details($res)["key"];

// Encrypting with public key
$plaintext = "Confidential message";
openssl_public_encrypt($plaintext, $encrypted, $publicKey);

// Decrypting with private key
openssl_private_decrypt($encrypted, $decrypted, $privateKey);

End-to-End Encryption (E2EE)

End-to-End encryption is a method where data is encrypted on the sender's device and decrypted only on the recipient's device. The server transferring the data has no access to the keys and cannot read the content.

Examples:

  • WhatsApp, Signal (messaging)
  • Proton Mail, Tutanota (email)
  • Proton Drive (storage)

Unlike regular encryption where the server stores decrypted data, E2EE ensures only the sender and recipient can see the content. Even if someone breaches the server, they would only get unreadable encrypted data.

Encryption vs. Hash - What's the Difference?

This is often confused, so it's important to distinguish clearly:

Property Encryption Hash
Direction Two-way (can return) One-way (no return)
Purpose Hide data, display later Verify integrity, not display
Key Requires key No key needed
Output Ciphertext (can be decrypted) Hash (should never be reversible)
Use Case Email, files, communication Passwords, checksums, blockchain

Which Algorithm to Choose?

If you're building an application, here are recommendations based on purpose:

Purpose Recommendation Notes
Passwords bcrypt, Argon2, scrypt Never MD5 or SHA-1 for passwords!
Files AES-256 Use CBC or GCM mode
Email PGP/GPG (RSA or ECC) Standard for encrypted communication
Web (HTTPS) Let's Encrypt, TLS 1.3 Necessary for every website
Mobile apps AES-256 + ChaCha20 ChaCha20 works better on Android

Common Mistakes in Encryption

  1. Using outdated algorithms: MD5 and SHA-1 are not secure for any purpose. Do not use them!
  2. Weak random generators: Using `rand()` or `mt_rand()` for generating keys. Always use cryptographically secure functions like `random_bytes()` in PHP
  3. Hardcoded keys: Never store encryption keys directly in code. Use environment variables or dedicated secure storage
  4. Repeating IV (Initialization Vector): For AES-CBC you must use a new, unique IV for each encryption
  5. Ignoring authentication: Some encryption modes (like AES-GCM) also provide authentication. Don't forget to verify the integrity of decrypted data

Why Does Encryption Matter?

Encryption is the foundation of digital freedom. It enables you to:

  • Transmit sensitive data over the internet without fear of interception
  • Store passwords and personal information in the cloud without breach risk
  • Communicate with anyone without third parties reading your messages
  • Protect your business from data breaches

In today's world, where data breaches have become routine, encryption is no longer optional - it's necessary. Every developer should understand the basics of encryption and use it correctly in their projects.

For better understanding of password hashing and encryption practices, we recommend viewing the example in my tutorial: PHP Forms with Secure Processing and Password Protection.

back Back to blog